import { Router, Request, Response, NextFunction } from 'express';
import { agendaService } from './agenda.service';
import {
  createAgendaSchema,
  updateAgendaSchema,
  createMinutesSchema,
  updateMinutesSchema,
  listAgendasSchema,
} from './agenda.schemas';
import { validate } from '../../middleware/validate';
import { authenticate } from '../../middleware/auth.middleware';
import { requireRole } from '../../middleware/rbac.middleware';
import { EVENTS_ROLES } from '../../utils/roles';

const router = Router();
router.use(authenticate);
router.use(requireRole(...EVENTS_ROLES));

// List agendas
router.get('/', validate(listAgendasSchema, 'query'), async (req: Request, res: Response, next: NextFunction) => {
  try {
    const result = await agendaService.findAll(req.query as any);
    res.json(result);
  } catch (err) { next(err); }
});

// Get agenda detail
router.get('/:id', async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    const agenda = await agendaService.findById(id);
    res.json(agenda);
  } catch (err) { next(err); }
});

// Create agenda
router.post('/', validate(createAgendaSchema), async (req: Request, res: Response, next: NextFunction) => {
  try {
    const agenda = await agendaService.create(req.body, req.user!.id);
    res.status(201).json(agenda);
  } catch (err) { next(err); }
});

// Update agenda
router.put('/:id', validate(updateAgendaSchema), async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    const agenda = await agendaService.update(id, req.body);
    res.json(agenda);
  } catch (err) { next(err); }
});

// Delete agenda
router.delete('/:id', async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    await agendaService.delete(id);
    res.json({ message: 'Agenda deleted' });
  } catch (err) { next(err); }
});

// Create minutes for agenda
router.post('/:id/minutes', validate(createMinutesSchema), async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    req.body.agendaId = id;
    const minutes = await agendaService.createMinutes(req.body, req.user!.id);
    res.status(201).json(minutes);
  } catch (err) { next(err); }
});

// Update minutes for agenda
router.put('/:id/minutes', validate(updateMinutesSchema), async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    const agenda = await agendaService.findById(id);
    if (!agenda.minutes) {
      return res.status(404).json({ message: 'Minutes not found for this agenda' });
    }
    const minutes = await agendaService.updateMinutes(agenda.minutes.id, req.body);
    res.json(minutes);
  } catch (err) { next(err); }
});

// Approve minutes
router.post('/:id/minutes/approve', async (req: Request, res: Response, next: NextFunction) => {
  try {
    const id = req.params.id as string;
    const agenda = await agendaService.findById(id);
    if (!agenda.minutes) {
      return res.status(404).json({ message: 'Minutes not found for this agenda' });
    }
    const minutes = await agendaService.approveMinutes(agenda.minutes.id, req.user!.id);
    res.json(minutes);
  } catch (err) { next(err); }
});

export { router as agendaRouter };
