

const express = require("express");
const router = express.Router();

const payrollController = require("../../controllers/hrandpayrollsController/employeePayrollController");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Payroll
 *   description: Payroll generation, management and reporting APIs
 */

/* ===========================
   ✅ GET PAYROLL GROUPS
=========================== */
router.get(
  "/groups",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get all payroll groups'
      #swagger.description = 'Returns list of payroll groups with employee counts'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.responses[200] = {
        description: 'Groups fetched successfully',
        schema: {
          type: 'object',
          properties: {
            success: { type: 'boolean', example: true },
            data: {
              type: 'array',
              items: {
                type: 'object',
                properties: {
                  id: { type: 'integer', example: 1 },
                  group_name: { type: 'string', example: 'IT' },
                  employee_count: { type: 'integer', example: 25 }
                }
              }
            }
          }
        }
      }
    */

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

/* ===========================
   ✅ GET EMPLOYEES FOR PAYROLL
=========================== */
router.get(
  "/employees",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get employees for payroll preview'
      #swagger.description = 'Returns list of employees with computed payroll for a given month/year'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['month'] = {
        in: 'query',
        required: true,
        type: 'string',
        description: 'Month name (e.g., April)',
        example: 'April'
      }

      #swagger.parameters['year'] = {
        in: 'query',
        required: true,
        type: 'integer',
        description: 'Year',
        example: 2026
      }

      #swagger.parameters['group_id'] = {
        in: 'query',
        required: false,
        type: 'integer',
        description: 'Filter by group ID',
        example: 1
      }

      #swagger.responses[200] = {
        description: 'Employees fetched successfully',
        schema: {
          type: 'object',
          properties: {
            success: { type: 'boolean', example: true },
            data: { type: 'array' }
          }
        }
      }
    */

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

/* ===========================
   ✅ GENERATE PAYROLL
=========================== */
router.post(
  "/generate",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Generate payroll for selected employees'
      #swagger.description = 'Generates payroll records for the selected employees'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          required: ['employee_ids', 'month', 'year'],
          properties: {
            employee_ids: {
              type: 'array',
              items: { type: 'integer' },
              example: [1, 2, 3]
            },
            month: {
              type: 'string',
              example: 'April'
            },
            year: {
              type: 'integer',
              example: 2026
            },
            payment_type: {
              type: 'string',
              enum: ['Bank Transfer', 'UPI', 'Cash', 'Cheque'],
              example: 'Bank Transfer'
            }
          }
        }
      }

      #swagger.responses[200] = {
        description: 'Payroll generated successfully',
        schema: {
          type: 'object',
          properties: {
            success: { type: 'boolean', example: true },
            message: { type: 'string' },
            data: {
              type: 'object',
              properties: {
                generated: { type: 'array' },
                errors: { type: 'array' }
              }
            }
          }
        }
      }
    */

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

/* ===========================
   ✅ GET YEARLY SUMMARY
=========================== */
router.get(
  "/yearly-summary",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get yearly payroll summary'
      #swagger.description = 'Returns year-level payroll summary'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['year'] = {
        in: 'query',
        required: true,
        type: 'integer',
        description: 'Year',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET MONTHLY PAYROLL SUMMARY
=========================== */
router.get(
  "/monthly-summary",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get monthly payroll summary'
      #swagger.description = 'Returns month-by-month payroll summary for a year'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['year'] = {
        in: 'query',
        required: true,
        type: 'integer',
        description: 'Year',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET EMPLOYEE PAYROLL DETAILS
=========================== */
router.get(
  "/employee-details",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get employee payroll details'
      #swagger.description = 'Returns detailed payroll records for employees'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['month'] = {
        in: 'query',
        required: true,
        type: 'integer',
        description: 'Month number (1-12)',
        example: 4
      }

      #swagger.parameters['year'] = {
        in: 'query',
        required: true,
        type: 'integer',
        description: 'Year',
        example: 2026
      }

      #swagger.parameters['group_name'] = {
        in: 'query',
        required: false,
        type: 'string',
        description: 'Filter by group name',
        example: 'IT'
      }
    */

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

/* ===========================
   ✅ BULK UPDATE PAYROLL STATUS
=========================== */
router.patch(
  "/bulk-status",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Bulk update payroll status'
      #swagger.description = 'Updates status for multiple payroll records at once'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          required: ['ids', 'status'],
          properties: {
            ids: {
              type: 'array',
              items: { type: 'integer' },
              example: [1, 2, 3]
            },
            status: {
              type: 'string',
              enum: ['Pending', 'Generated', 'Processing', 'Paid', 'Cancelled', 'Hold'],
              example: 'Paid'
            },
            paid_date: {
              type: 'string',
              format: 'date',
              example: '2026-04-30'
            },
            payment_reference: {
              type: 'string',
              example: 'REF123456'
            }
          }
        }
      }
    */

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

/* ===========================
   ✅ GET EMPLOYEE PAYROLL HISTORY
=========================== */
router.get(
  "/employee/:employeeId/history",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get employee payroll history'
      #swagger.description = 'Returns payroll history for a specific employee'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['employeeId'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Employee ID',
        example: 1
      }
    */

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

/* ===========================
   ✅ GET PAYSLIP
=========================== */
router.get(
  "/:id/payslip",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payslip data'
      #swagger.description = 'Returns all data needed to render payslip'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Payroll record ID',
        example: 123
      }
    */

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

/* ===========================
   ✅ GET PAYROLL BY ID
=========================== */
router.get(
  "/:id",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payroll by ID'
      #swagger.description = 'Returns a single payroll record with details'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Payroll record ID',
        example: 123
      }
    */

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

/* ===========================
   ✅ UPDATE PAYROLL
=========================== */
router.put(
  "/:id",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Update payroll record'
      #swagger.description = 'Updates a payroll record (full update)'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Payroll record ID',
        example: 123
      }

      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          properties: {
            salary: { type: 'number', example: 50000 },
            overtimeHours: { type: 'number', example: 5 },
            overtimePay: { type: 'number', example: 2000 },
            grossSalary: { type: 'number', example: 52000 },
            totalSalary: { type: 'number', example: 52000 },
            payDate: { type: 'string', format: 'date', example: '2026-04-30' },
            paymentType: { type: 'string', example: 'Bank Transfer' },
            status: { type: 'string', example: 'Paid' },
            remarks: { type: 'string', example: 'Bonus added' },
            payment_reference: { type: 'string', example: 'REF123' }
          }
        }
      }
    */

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

/* ===========================
   ✅ UPDATE PAYROLL STATUS (PATCH)
=========================== */
router.patch(
  "/:id/status",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Update payroll status'
      #swagger.description = 'Quick-update the payment status of a payroll record'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Payroll record ID',
        example: 123
      }

      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          required: ['status'],
          properties: {
            status: {
              type: 'string',
              enum: ['Pending', 'Generated', 'Processing', 'Paid', 'Cancelled', 'Hold'],
              example: 'Paid'
            },
            paid_date: {
              type: 'string',
              format: 'date',
              example: '2026-04-30'
            },
            payment_method: {
              type: 'string',
              example: 'Bank Transfer'
            },
            payment_reference: {
              type: 'string',
              example: 'REF123'
            },
            remarks: {
              type: 'string',
              example: 'Payment completed'
            }
          }
        }
      }
    */

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

/* ===========================
   ✅ DELETE PAYROLL (SOFT DELETE)
=========================== */
router.delete(
  "/:id",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Delete payroll record'
      #swagger.description = 'Soft deletes a payroll record'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        type: 'integer',
        description: 'Payroll record ID',
        example: 123
      }

      #swagger.responses[200] = {
        description: 'Payroll record deleted successfully',
        schema: {
          type: 'object',
          properties: {
            success: { type: 'boolean', example: true },
            message: { type: 'string', example: 'Payroll record deleted' }
          }
        }
      }

      #swagger.responses[404] = {
        description: 'Payroll record not found',
        schema: {
          type: 'object',
          properties: {
            success: { type: 'boolean', example: false },
            message: { type: 'string', example: 'Payroll record not found' }
          }
        }
      }
    */

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

module.exports = router;