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

const router = Router();

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

// ========== EMPLOYEES ==========

router.get('/employees', (req, res, next) => hrController.getEmployees(req, res, next));

router.get('/employees/:id', (req, res, next) => hrController.getEmployee(req, res, next));

router.get('/employees/:id/subordinates', (req, res, next) => hrController.getSubordinates(req, res, next));

router.post('/employees', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.createEmployee(req, res, next)
);

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

router.post('/employees/:id/terminate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.terminateEmployee(req, res, next)
);

router.post('/employees/:id/reactivate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.reactivateEmployee(req, res, next)
);

router.delete('/employees/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteEmployee(req, res, next)
);

// ========== DEPARTMENTS ==========

router.get('/departments', (req, res, next) => hrController.getDepartments(req, res, next));

router.get('/departments/:id', (req, res, next) => hrController.getDepartment(req, res, next));

router.post('/departments', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.createDepartment(req, res, next)
);

router.put('/departments/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.updateDepartment(req, res, next)
);

router.delete('/departments/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteDepartment(req, res, next)
);

// ========== JOB POSITIONS ==========

router.get('/positions', (req, res, next) => hrController.getJobPositions(req, res, next));

router.post('/positions', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.createJobPosition(req, res, next)
);

router.put('/positions/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.updateJobPosition(req, res, next)
);

router.delete('/positions/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteJobPosition(req, res, next)
);

// ========== CONTRACTS ==========

router.get('/contracts', (req, res, next) => hrController.getContracts(req, res, next));

router.get('/contracts/:id', (req, res, next) => hrController.getContract(req, res, next));

router.post('/contracts', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.createContract(req, res, next)
);

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

router.post('/contracts/:id/activate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.activateContract(req, res, next)
);

router.post('/contracts/:id/terminate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.terminateContract(req, res, next)
);

router.post('/contracts/:id/cancel', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.cancelContract(req, res, next)
);

router.delete('/contracts/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteContract(req, res, next)
);

// ========== LEAVE TYPES ==========

router.get('/leave-types', (req, res, next) => hrController.getLeaveTypes(req, res, next));

router.post('/leave-types', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.createLeaveType(req, res, next)
);

router.put('/leave-types/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.updateLeaveType(req, res, next)
);

router.delete('/leave-types/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteLeaveType(req, res, next)
);

// ========== LEAVES ==========

router.get('/leaves', (req, res, next) => hrController.getLeaves(req, res, next));

router.get('/leaves/:id', (req, res, next) => hrController.getLeave(req, res, next));

router.post('/leaves', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.createLeave(req, res, next)
);

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

router.post('/leaves/:id/submit', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.submitLeave(req, res, next)
);

router.post('/leaves/:id/approve', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.approveLeave(req, res, next)
);

router.post('/leaves/:id/reject', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.rejectLeave(req, res, next)
);

router.post('/leaves/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrController.cancelLeave(req, res, next)
);

router.delete('/leaves/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrController.deleteLeave(req, res, next)
);

export default router;
