
const express = require("express");
const router = express.Router();
const authenticate = require("../../middlewares/authenticate");

const {
  getLeaveTypes,
  getMyLeaveBalances,
  getLeaveRequests,
  applyLeave,
  assignCoverage,
  forwardToHR,
  forwardBulkToHR,
  approveRequest,
  rejectRequest,
  withdrawRequest,
  getTeamMembers,
  getLeaveBalanceByEmployee,
  getEmployeeLeaveBalance,
  getActiveLeaveCoverages,
} = require("../../controllers/hrandpayrollsController/leaveController");

/**
 * @swagger
 * tags:
 *   name: Leave Management
 *   description: Leave management APIs
 */

// ── Static routes ─────────────────────────────────────────────────────────

/**
 * @swagger
 * /leaves/types:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get leave types
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/types", authenticate, (req, res) => {
  getLeaveTypes(req, res);
});

/**
 * @swagger
 * /leaves/balances:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get my leave balances
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/balances", authenticate, (req, res) => {
  getMyLeaveBalances(req, res);
});

/**
 * @swagger
 * /leaves/team-members:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get team members
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/team-members", authenticate, (req, res) => {
  getTeamMembers(req, res);
});

/**
 * @swagger
 * /leaves/requests:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get leave requests
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/requests", authenticate, (req, res) => {
  getLeaveRequests(req, res);
});

/**
 * @swagger
 * /leaves/requests/apply:
 *   post:
 *     tags: [Leave Management]
 *     summary: Apply for leave
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       201:
 *         description: Created
 */
router.post("/requests/apply", authenticate, (req, res) => {
  applyLeave(req, res);
});

/**
 * @swagger
 * /leaves/requests/bulk-forward:
 *   post:
 *     tags: [Leave Management]
 *     summary: Bulk forward to HR
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.post("/requests/bulk-forward", authenticate, (req, res) => {
  forwardBulkToHR(req, res);
});

/**
 * @swagger
 * /leaves/active-coverages:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get active leave coverages
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/active-coverages", authenticate, (req, res) => {
  getActiveLeaveCoverages(req, res);
});

/**
 * @swagger
 * /leaves/balance/employee/{employeeId}:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get employee leave balance
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: employeeId
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/balance/employee/:employeeId", authenticate, (req, res) => {
  getEmployeeLeaveBalance(req, res);
});

/**
 * @swagger
 * /leaves/balance/{employeeId}:
 *   get:
 *     tags: [Leave Management]
 *     summary: Get leave balance by employee
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: employeeId
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.get("/balance/:employeeId", authenticate, (req, res) => { 
  getLeaveBalanceByEmployee(req, res);
});

// ── Parameterised routes ─────────────────────────────────────────────────

/**
 * @swagger
 * /leaves/requests/{id}/coverage:
 *   patch:
 *     tags: [Leave Management]
 *     summary: Assign coverage
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.patch("/requests/:id/coverage", authenticate, (req, res) => {
  assignCoverage(req, res);
});

/**
 * @swagger
 * /leaves/requests/{id}/forward:
 *   patch:
 *     tags: [Leave Management]
 *     summary: Forward to HR
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.patch("/requests/:id/forward", authenticate, (req, res) => {
  forwardToHR(req, res);
});

/**
 * @swagger
 * /leaves/requests/{id}/approve:
 *   patch:
 *     tags: [Leave Management]
 *     summary: Approve leave request
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.patch("/requests/:id/approve", authenticate, (req, res) => {
  approveRequest(req, res);
});

/**
 * @swagger
 * /leaves/requests/{id}/reject:
 *   patch:
 *     tags: [Leave Management]
 *     summary: Reject leave request
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.patch("/requests/:id/reject", authenticate, (req, res) => {
  rejectRequest(req, res);
});

/**
 * @swagger
 * /leaves/requests/{id}:
 *   delete:
 *     tags: [Leave Management]
 *     summary: Withdraw leave request
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 *     responses:
 *       200:
 *         description: Success
 */
router.delete("/requests/:id", authenticate, (req, res) => {
  withdrawRequest(req, res);
});

module.exports = router;