const express = require("express")
const router = express.Router();
const payrollController = require("../controllers/accounts/payrollController")
const authenticate = require("../middlewares/authenticate");
const authorize = require("../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Payroll
 *   description: Payroll management APIs
 */
// Create Payroll
router.post("/payroll/create", authenticate, authorize(["admin", "manager"]), (req, res) => {
  // #swagger.tags = ['Payroll']
  //  #swagger.summary = 'Create a new payroll'
  //  #swagger.security = [{ "bearerAuth": [] }]

  payrollController.createPayroll(req, res);
});

// fetchAllPayrolls
router.get("/fetchAllPayrolls", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Payroll']
    #swagger.summary = 'Get all payrolls with pagination'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['page'] = { description: 'Page number', in: 'query' }
    #swagger.parameters['limit'] = { description: 'Number of Payrolls per page', in: 'query' }
  */
  payrollController.fetchAllPayrolls(req, res)
})


// delete PayRoll
router.delete("/payroll/delete/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Payroll']
    #swagger.summary = 'Soft delete a payroll by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Payroll ID', required: true }
  */
  payrollController.deletePayroll(req, res)
})

// update PayRoll
router.put("/payroll/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Payroll']
    #swagger.summary = 'Soft delete a payroll by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Payroll ID', required: true }
  */
  payrollController.updatePayroll(req, res)
})

router.post(
  "/generate",
  authenticate,
  authorize(["admin", "superadmin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Generate payroll for all employees'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["month", "year"],
              properties: {
                month: {
                  type: "number",
                  example: 1
                },
                year: {
                  type: "number",
                  example: 2026
                }
              }
            }
          }
        }
      }
    */
    payrollController.generatePayroll(req, res);
  }
);

router.get(
  "/payroll",
  authenticate,
  authorize(["admin", "superadmin"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get all payroll records'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    payrollController.getAllPayrolls(req, res);
  }
);

router.get(
  "/payslip/:payrollId/:employeeId",
  authenticate,
  authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Fetch employee payslip'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['payrollId'] = {
        in: 'path',
        required: true,
        description: 'Payroll ID (e.g. PAY-2026-01)',
        type: 'string'
      }
      #swagger.parameters['employeeId'] = {
        in: 'path',
        required: true,
        description: 'Employee ID',
        type: 'string'
      }
    */
    payrollController.getEmployeePayslip(req, res);
  }
);

router.get(
  "/payslip/:payrollId/:employeeId/pdf",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Download employee payslip PDF'
      #swagger.description = 'Generates and downloads the payslip PDF for a specific employee.'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    payrollController.downloadPayslipPdf(req, res);
  }
);


router.get(
  "/payroll/preview",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Preview payroll for a given month and year'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    payrollController.previewPayroll(req, res);
  }
);

router.post(
  "/payroll/:id/mark-paid",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Mark payroll as paid'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll ID',
        required: true,
        type: 'string'
      }
    */
    payrollController.markPayrollAsPaid(req, res);
  }
);



module.exports = router;