import { Router } from 'express';
import { hrController } from './hr.controller.js';
import { hrExtendedController } from './hr-extended.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)
);

// ========== SKILL TYPES ==========

router.get('/skill-types', (req, res, next) => hrExtendedController.getSkillTypes(req, res, next));

router.get('/skill-types/:id', (req, res, next) => hrExtendedController.getSkillType(req, res, next));

router.post('/skill-types', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.createSkillType(req, res, next)
);

router.put('/skill-types/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.updateSkillType(req, res, next)
);

router.delete('/skill-types/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteSkillType(req, res, next)
);

// ========== SKILLS ==========

router.get('/skills', (req, res, next) => hrExtendedController.getSkills(req, res, next));

router.get('/skills/:id', (req, res, next) => hrExtendedController.getSkill(req, res, next));

router.post('/skills', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.createSkill(req, res, next)
);

router.put('/skills/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.updateSkill(req, res, next)
);

router.delete('/skills/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteSkill(req, res, next)
);

// ========== SKILL LEVELS ==========

router.get('/skill-levels', (req, res, next) => hrExtendedController.getSkillLevels(req, res, next));

router.post('/skill-levels', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.createSkillLevel(req, res, next)
);

router.put('/skill-levels/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.updateSkillLevel(req, res, next)
);

router.delete('/skill-levels/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteSkillLevel(req, res, next)
);

// ========== EMPLOYEE SKILLS ==========

router.get('/employee-skills', (req, res, next) => hrExtendedController.getEmployeeSkills(req, res, next));

router.get('/employees/:employeeId/skills', (req, res, next) =>
  hrExtendedController.getEmployeeSkillsByEmployee(req, res, next)
);

router.post('/employee-skills', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.createEmployeeSkill(req, res, next)
);

router.put('/employee-skills/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.updateEmployeeSkill(req, res, next)
);

router.delete('/employee-skills/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteEmployeeSkill(req, res, next)
);

// ========== EXPENSE SHEETS ==========

router.get('/expense-sheets', (req, res, next) => hrExtendedController.getExpenseSheets(req, res, next));

router.get('/expense-sheets/:id', (req, res, next) => hrExtendedController.getExpenseSheet(req, res, next));

router.post('/expense-sheets', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.createExpenseSheet(req, res, next)
);

router.put('/expense-sheets/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.updateExpenseSheet(req, res, next)
);

router.post('/expense-sheets/:id/submit', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.submitExpenseSheet(req, res, next)
);

router.post('/expense-sheets/:id/approve', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.approveExpenseSheet(req, res, next)
);

router.post('/expense-sheets/:id/reject', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.rejectExpenseSheet(req, res, next)
);

router.delete('/expense-sheets/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteExpenseSheet(req, res, next)
);

// ========== EXPENSES ==========

router.get('/expenses', (req, res, next) => hrExtendedController.getExpenses(req, res, next));

router.get('/expenses/:id', (req, res, next) => hrExtendedController.getExpense(req, res, next));

router.post('/expenses', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.createExpense(req, res, next)
);

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

router.delete('/expenses/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deleteExpense(req, res, next)
);

// ========== PAYSLIP STRUCTURES ==========

router.get('/payslip-structures', (req, res, next) => hrExtendedController.getPayslipStructures(req, res, next));

router.get('/payslip-structures/:id', (req, res, next) => hrExtendedController.getPayslipStructure(req, res, next));

router.post('/payslip-structures', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.createPayslipStructure(req, res, next)
);

router.put('/payslip-structures/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.updatePayslipStructure(req, res, next)
);

router.delete('/payslip-structures/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deletePayslipStructure(req, res, next)
);

// ========== PAYSLIPS ==========

router.get('/payslips', (req, res, next) => hrExtendedController.getPayslips(req, res, next));

router.get('/payslips/:id', (req, res, next) => hrExtendedController.getPayslip(req, res, next));

router.get('/payslips/:id/lines', (req, res, next) => hrExtendedController.getPayslipLines(req, res, next));

router.post('/payslips', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.createPayslip(req, res, next)
);

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

router.post('/payslips/:id/verify', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.verifyPayslip(req, res, next)
);

router.post('/payslips/:id/confirm', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.confirmPayslip(req, res, next)
);

router.post('/payslips/:id/cancel', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.cancelPayslip(req, res, next)
);

router.delete('/payslips/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  hrExtendedController.deletePayslip(req, res, next)
);

// Payslip Lines
router.post('/payslips/:id/lines', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.addPayslipLine(req, res, next)
);

router.put('/payslips/:id/lines/:lineId', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.updatePayslipLine(req, res, next)
);

router.delete('/payslips/:id/lines/:lineId', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  hrExtendedController.removePayslipLine(req, res, next)
);

export default router;
