import express from 'express';
import * as timeTrackingController from '../controllers/time-tracking.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireTeamLeaderOrAdmin } from '../middlewares/auth/roleMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import {
  startTimeEntrySchema,
  stopTimeEntrySchema,
  updateTimeEntrySchema,
} from '../validators/crm.validators.js';
import { z } from 'zod';

const router = express.Router();

// All time tracking routes require authentication
router.use(authenticate);

/**
 * Time Tracking management
 */

// Start new time entry
router.post('/start', validateBody(startTimeEntrySchema), timeTrackingController.startTimeEntry);

// Stop running time entry
router.post(
  '/:entryId/stop',
  validateParams(z.object({ entryId: z.string().uuid() })),
  validateBody(stopTimeEntrySchema),
  timeTrackingController.stopTimeEntry
);

// Pause running time entry
router.post(
  '/:entryId/pause',
  validateParams(z.object({ entryId: z.string().uuid() })),
  timeTrackingController.pauseTimeEntry
);

// Resume paused time entry
router.post(
  '/:entryId/resume',
  validateParams(z.object({ entryId: z.string().uuid() })),
  timeTrackingController.resumeTimeEntry
);

// Get running time entry
router.get('/running', timeTrackingController.getRunningTimeEntry);

// Get all running time entries (for dashboard) - admin/team_leader
router.get('/running-all', requireTeamLeaderOrAdmin, timeTrackingController.getAllRunningTimeEntries);

// Get all time entries with filters
router.get('/', timeTrackingController.getAllTimeEntries);

// Get monthly time entries
router.get(
  '/month/:year/:month',
  validateParams(
    z.object({
      year: z.string().regex(/^\d{4}$/, 'Rok musí byť 4-ciferné číslo'),
      month: z.string().regex(/^(0?[1-9]|1[0-2])$/, 'Mesiac musí byť číslo 1-12'),
    })
  ),
  timeTrackingController.getMonthlyTimeEntries
);

// Generate monthly timesheet (XLSX)
router.post(
  '/month/:year/:month/generate',
  validateParams(
    z.object({
      year: z.string().regex(/^\d{4}$/, 'Rok musí byť 4-ciferné číslo'),
      month: z.string().regex(/^(0?[1-9]|1[0-2])$/, 'Mesiac musí byť číslo 1-12'),
    })
  ),
  timeTrackingController.generateMonthlyTimesheet
);

// Generate company timesheet (XLSX)
router.post(
  '/generate-company-timesheet',
  validateBody(
    z.object({
      year: z.number().int().min(2000).max(2100),
      month: z.number().int().min(1).max(12),
      companyId: z.string().uuid('Neplatný formát company ID'),
    })
  ),
  timeTrackingController.generateCompanyTimesheet
);

// Get monthly statistics
router.get(
  '/stats/monthly/:year/:month',
  validateParams(
    z.object({
      year: z.string().regex(/^\d{4}$/, 'Rok musí byť 4-ciferné číslo'),
      month: z.string().regex(/^(0?[1-9]|1[0-2])$/, 'Mesiac musí byť číslo 1-12'),
    })
  ),
  timeTrackingController.getMonthlyStats
);

// Get time entry by ID
router.get(
  '/:entryId',
  validateParams(z.object({ entryId: z.string().uuid() })),
  timeTrackingController.getTimeEntryById
);

// Get time entry with relations
router.get(
  '/:entryId/details',
  validateParams(z.object({ entryId: z.string().uuid() })),
  timeTrackingController.getTimeEntryWithRelations
);

// Update time entry
router.patch(
  '/:entryId',
  validateParams(z.object({ entryId: z.string().uuid() })),
  validateBody(updateTimeEntrySchema),
  timeTrackingController.updateTimeEntry
);

// Delete time entry
router.delete(
  '/:entryId',
  validateParams(z.object({ entryId: z.string().uuid() })),
  timeTrackingController.deleteTimeEntry
);

export default router;
