import express from 'express';
import * as eventController from '../controllers/event.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireAdmin } from '../middlewares/auth/roleMiddleware.js';
import { validateBody, validateParams, validateQuery } from '../middlewares/security/validateInput.js';
import { createEventSchema, updateEventSchema } from '../validators/crm.validators.js';
import { z } from 'zod';

const router = express.Router();

const monthQuerySchema = z.object({
  year: z.string().regex(/^\d{4}$/).optional(),
  month: z.string().regex(/^(1[0-2]|[1-9])$/).optional(),
});

const eventIdSchema = z.object({
  eventId: z.string().uuid(),
});

// Všetky routes vyžadujú autentifikáciu
router.use(authenticate);

/**
 * GET /api/events - Získať eventy a todos podľa mesiaca (filtrované podľa assigned users)
 */
router.get(
  '/',
  validateQuery(monthQuerySchema),
  eventController.getCalendarData
);

/**
 * GET /api/events/:eventId - Získať konkrétny event
 */
router.get(
  '/:eventId',
  validateParams(eventIdSchema),
  eventController.getEventById
);

/**
 * POST /api/events - Vytvoriť event (iba admin)
 */
router.post(
  '/',
  requireAdmin,
  validateBody(createEventSchema),
  eventController.createEvent
);

/**
 * PUT /api/events/:eventId - Upraviť event (iba admin)
 */
router.put(
  '/:eventId',
  requireAdmin,
  validateParams(eventIdSchema),
  validateBody(updateEventSchema),
  eventController.updateEvent
);

/**
 * DELETE /api/events/:eventId - Zmazať event (iba admin)
 */
router.delete(
  '/:eventId',
  requireAdmin,
  validateParams(eventIdSchema),
  eventController.deleteEvent
);

/**
 * POST /api/events/:eventId/notify - Odoslať notifikácie priradeným používateľom (iba admin)
 */
router.post(
  '/:eventId/notify',
  requireAdmin,
  validateParams(eventIdSchema),
  eventController.sendEventNotification
);

export default router;
