import { CourseAttendee, CourseEventDetails } from "@app/shared/types/course";
import { getAllCourses, getCourseEventAttendees, getCourseEventDetails, getCourseEventRoles, getCourseEvents, insertCourseEvent } from "../services/CourseSerivce";
import { Request, Response, Router } from "express";
import { requireLogin, requireMemberState } from "../middleware/auth";
import { MemberState } from "@app/shared/types/member";

const cr = Router();
const er = Router();

cr.use(requireLogin)
er.use(requireLogin)
cr.use(requireMemberState(MemberState.Member))
er.use(requireMemberState(MemberState.Member))

cr.get('/', async (req, res) => {
    try {
        const courses = await getAllCourses();
        res.status(200).json(courses);
    } catch (err) {
        console.error('failed to fetch courses', err);
        res.status(500).json('failed to fetch courses\n' + err);
    }
})

cr.get('/roles', async (req, res) => {
    try {
        const roles = await getCourseEventRoles();
        res.status(200).json(roles);
    } catch (err) {
        console.error('failed to fetch course roles', err);
        res.status(500).json('failed to fetch course roles\n' + err);
    }
})

er.get('/', async (req: Request, res: Response) => {
    const allowedSorts = new Map([
        ["ascending", "ASC"],
        ["descending", "DESC"]
    ]);

    const sort = String(req.query.sort || "").toLowerCase();
    const search = String(req.query.search || "").toLowerCase();
    if (!allowedSorts.has(sort)) {
        return res.status(400).json({
            message: `Invalid sort direction '${req.query.sort}'. Allowed values are 'ascending' or 'descending'.`
        });
    }

    const sortDir = allowedSorts.get(sort);

    try {
        let events = await getCourseEvents(sortDir, search);
        res.status(200).json(events);
    } catch (error) {
        console.error('failed to fetch reports', error);
        res.status(500).json(error);
    }
});

er.get('/:id', async (req: Request, res: Response) => {
    try {
        let out = await getCourseEventDetails(Number(req.params.id));
        res.status(200).json(out);
    } catch (error) {
        console.error('failed to fetch report', error);
        res.status(500).json(error);
    }
});

er.get('/attendees/:id', async (req: Request, res: Response) => {
    try {
        const attendees: CourseAttendee[] = await getCourseEventAttendees(Number(req.params.id));
        res.status(200).json(attendees);
    } catch (err) {
        console.error('failed to fetch attendees', err);
        res.status(500).json("failed to fetch attendees\n" + err);
    }
})

er.post('/', async (req: Request, res: Response) => {
    const posterID: number = req.user.id;
    try {
        console.log();
        let data: CourseEventDetails = req.body;
        data.created_by = posterID;
        data.event_date = new Date(data.event_date);
        const id = await insertCourseEvent(data);
        res.status(201).json(id);
    } catch (error) {
        console.error('failed to post training', error);
        res.status(500).json("failed to post training\n" + error)
    }
})

export const courseRouter = cr;
export const eventRouter = er;
