import { MemberState } from "@app/shared/types/member";
import { requireLogin, requireMemberState, requireRole } from "../middleware/auth";
import { getAllRanks, insertMemberRank } from "../services/rankService";

import express = require('express');
const r = express.Router();
const ur = express.Router();


r.use(requireLogin)
ur.use(requireLogin)

//insert a new latest rank for a user
ur.post('/', [requireRole(["17th Command", "17th Administrator", "17th HQ"]), requireMemberState(MemberState.Member)], async (req, res) => {
    3
    try {
        const change = req.body?.change;
        await insertMemberRank(change.member_id, change.rank_id, change.date);

        res.sendStatus(201);
    } catch (err) {
        console.error('Insert failed:', err);
        res.status(500).json({ error: 'Failed to update ranks' });
    }
});

//get all ranks
r.get('/', async (req, res) => {
    try {
        const ranks = await getAllRanks();
        res.json(ranks);
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: 'Internal server error' });
    }
});

export const ranks = r;
export const memberRanks = ur;