/**
 * Employee Routes
 */

const express = require("express");
const router = express.Router();
const { body, param } = require("express-validator");
const { validate } = require("../middleware/validationMiddleware");
const { authenticate, authorize } = require("../middleware/authMiddleware");
const employeeController = require("../controllers/employeeController");

// Validation rules
const createEmployeeValidation = [
  body("firstName").notEmpty().withMessage("First name is required"),
  body("lastName").notEmpty().withMessage("Last name is required"),
  body("nic").notEmpty().withMessage("NIC is required"),
  body("email").isEmail().withMessage("Valid email is required"),
  body("mobile").notEmpty().withMessage("Mobile number is required"),
  body("joinDate").isDate().withMessage("Valid join date is required"),
];

const updateEmployeeValidation = [
  body("email").optional().isEmail().withMessage("Valid email is required"),
  body("joinDate")
    .optional()
    .isDate()
    .withMessage("Valid join date is required"),
];

// Apply authentication to all routes
router.use(authenticate);

// Routes
router.get(
  "/statistics",
  authorize("Admin", "Manager"),
  employeeController.getEmployeeStatistics,
);
router.get(
  "/department/:departmentId",
  employeeController.getEmployeesByDepartment,
);
router.get("/", employeeController.getAllEmployees);
router.get("/:id", employeeController.getEmployeeById);
router.post(
  "/",
  authorize("Admin"),
  createEmployeeValidation,
  validate,
  employeeController.createEmployee,
);
router.put(
  "/:id",
  authorize("Admin", "Manager"),
  updateEmployeeValidation,
  validate,
  employeeController.updateEmployee,
);
router.delete("/:id", authorize("Admin"), employeeController.deleteEmployee);

module.exports = router;
