import { Router } from 'express';
import { projectsController } from './projects.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

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

// ========== PROJECTS ==========
router.get('/', (req, res, next) => projectsController.getProjects(req, res, next));

router.get('/:id', (req, res, next) => projectsController.getProject(req, res, next));

router.post('/', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  projectsController.createProject(req, res, next)
);

router.put('/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  projectsController.updateProject(req, res, next)
);

router.delete('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  projectsController.deleteProject(req, res, next)
);

router.get('/:id/stats', (req, res, next) => projectsController.getProjectStats(req, res, next));

router.get('/:id/tasks', (req, res, next) => projectsController.getProjectTasks(req, res, next));

router.get('/:id/timesheets', (req, res, next) => projectsController.getProjectTimesheets(req, res, next));

// ========== TASKS ==========
router.get('/tasks/all', (req, res, next) => projectsController.getTasks(req, res, next));

router.get('/tasks/:id', (req, res, next) => projectsController.getTask(req, res, next));

router.post('/tasks', (req, res, next) => projectsController.createTask(req, res, next));

router.put('/tasks/:id', (req, res, next) => projectsController.updateTask(req, res, next));

router.delete('/tasks/:id', (req, res, next) => projectsController.deleteTask(req, res, next));

router.post('/tasks/:id/move', (req, res, next) => projectsController.moveTask(req, res, next));

router.post('/tasks/:id/assign', (req, res, next) => projectsController.assignTask(req, res, next));

// ========== TIMESHEETS ==========
router.get('/timesheets/all', (req, res, next) => projectsController.getTimesheets(req, res, next));

router.get('/timesheets/me', (req, res, next) => projectsController.getMyTimesheets(req, res, next));

router.get('/timesheets/pending', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  projectsController.getPendingApprovals(req, res, next)
);

router.get('/timesheets/:id', (req, res, next) => projectsController.getTimesheet(req, res, next));

router.post('/timesheets', (req, res, next) => projectsController.createTimesheet(req, res, next));

router.put('/timesheets/:id', (req, res, next) => projectsController.updateTimesheet(req, res, next));

router.delete('/timesheets/:id', (req, res, next) => projectsController.deleteTimesheet(req, res, next));

router.post('/timesheets/:id/submit', (req, res, next) => projectsController.submitTimesheet(req, res, next));

router.post('/timesheets/:id/approve', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  projectsController.approveTimesheet(req, res, next)
);

router.post('/timesheets/:id/reject', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  projectsController.rejectTimesheet(req, res, next)
);

export default router;
