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

import { MemberState } from '@app/shared/types/member';
import pool from '../db';
import { requireLogin, requireMemberState, requireRole } from '../middleware/auth';
import { assignUserGroup, createGroup, getAllRoles, getRole, getUsersWithRole } from '../services/rolesService';
import { Request, Response } from 'express';

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

//manually assign a member to a group
ur.post('/', [requireMemberState(MemberState.Member), requireRole("17th Administrator")], async (req, res) => {
    try {
        const body = req.body;

        assignUserGroup(body.member_id, body.role_id);

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

//manually remove member from group
ur.delete('/', [requireMemberState(MemberState.Member), requireRole("17th Administrator")], async (req, res) => {
    try {
        const body = req.body;

        const sql = 'DELETE FROM members_roles WHERE member_id = ? AND role_id = ?'
        await pool.query(sql, [body.member_id, body.role_id])

        res.sendStatus(200);
    }
    catch (err) {
        console.error("delete failed: ", err)
        res.status(500).json({ error: 'Failed to remove from group' });

    }
})

//get all roles
r.get('/', [requireMemberState(MemberState.Member)], async (req, res) => {
    try {
        const roles = await getAllRoles();
        res.status(200).json(roles);
    } catch (err) {
        console.error(err);
        res.sendStatus(500);
    }
});

r.get('/:id/members', [requireMemberState(MemberState.Member)], async (req: Request, res: Response) => {
    try {
        const members = await getUsersWithRole(Number(req.params.id));
        res.status(200).json(members);
    } catch (err) {
        console.error(err);
        res.sendStatus(500);
    }
})


r.get('/:id', [requireMemberState(MemberState.Member)], async (req: Request, res: Response) => {
    try {
        const role = await getRole(Number(req.params.id));
        res.status(200).json(role);
    } catch (err) {
        console.error(err);
        res.sendStatus(500);
    }
})



//create a new role
r.post('/', [requireMemberState(MemberState.Member), requireRole("dev")], async (req, res) => {
    try {
        const { name, color, description } = req.body;
        if (!name || !color) {
            return res.status(400).json({ error: 'Name and color are required' });
        }

        const hexColorRegex = /^#([0-9A-Fa-f]{6})$/;
        if (!hexColorRegex.test(color)) {
            return res.status(400).json({ error: 'Color must be a valid hex color (#ffffff)' });
        }

        await createGroup(name, color, description);

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

r.delete('/:id', [requireMemberState(MemberState.Member), requireRole("dev")], async (req, res) => {
    try {
        const id = req.params.id;

        const sql = 'DELETE FROM roles WHERE id = ?';
        const res = await pool.query(sql, [id]);
        res.sendStatus(200);
    } catch (error) {
        console.error(error);
        res.sendStatus(500);
    }
})

export const roles = r;
export const memberRoles = ur;
