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

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

/**
 * @swagger
 * tags:
 *   name: Payroll
 *   description: Payroll management APIs for creating, updating, and managing employee payrolls
 */

/* ===========================
   ✅ CREATE PAYROLL
=========================== */

router.post(
  "/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Create a new payroll record'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          properties: {
            employee: { type: 'string', example: 'Ravi Kumar' },
            payGrade: { type: 'string', example: 'Grade A' },
            effectiveDate: { type: 'string', format: 'date', example: '2026-01-01' },
            basicSalary: { type: 'number', example: 30000 },
            hra: { type: 'number', example: 5000 },
            specialAllowance: { type: 'number', example: 2000 },
            conveyanceAllowance: { type: 'number', example: 1000 },
            medicalAllowance: { type: 'number', example: 1500 },
            otherAllowance: { type: 'number', example: 500 }
          }
        }
      }
    */

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

/* ===========================
   ✅ GET ALL PAYROLLS (with pagination & filters)
=========================== */

router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get all payroll records with pagination and filtering'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = {
        in: 'query',
        description: 'Page number',
        type: 'integer',
        example: 1
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        description: 'Records per page',
        type: 'integer',
        example: 10
      }
      #swagger.parameters['month'] = {
        in: 'query',
        description: 'Filter by month',
        type: 'string',
        example: 'January'
      }
      #swagger.parameters['year'] = {
        in: 'query',
        description: 'Filter by year',
        type: 'integer',
        example: 2026
      }
      #swagger.parameters['status'] = {
        in: 'query',
        description: 'Filter by status',
        type: 'string',
        example: 'Paid'
      }
    */

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

/* ===========================
   ✅ GET PAYROLL SUMMARY BY MONTH
=========================== */

router.get(
  "/summary",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payroll summary grouped by month'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['year'] = {
        in: 'query',
        description: 'Year for summary',
        type: 'integer',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET PAYROLLS BY MONTH (without benefits)
=========================== */

router.get(
  "/by-month",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payrolls filtered by month and year'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['month'] = {
        in: 'query',
        description: 'Month name',
        type: 'string',
        example: 'January'
      }
      #swagger.parameters['year'] = {
        in: 'query',
        description: 'Year',
        type: 'integer',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET PAYROLLS BY MONTH WITH BENEFITS
=========================== */

router.get(
  "/by-month-with-benefits",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payrolls filtered by month and year with employee benefits'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['month'] = {
        in: 'query',
        description: 'Month name',
        type: 'string',
        example: 'January'
      }
      #swagger.parameters['year'] = {
        in: 'query',
        description: 'Year',
        type: 'integer',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET PAYROLL WITH BENEFITS BY ID
=========================== */

router.get(
  "/with-benefits/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payroll record with employee benefits'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll record ID',
        required: true,
        type: 'integer'
      }
    */

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

/* ===========================
   ✅ GET MONTHLY SUMMARY FOR DASHBOARD
=========================== */

router.get(
  "/monthly-summary",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get monthly payroll summary for dashboard'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['year'] = {
        in: 'query',
        description: 'Year for dashboard summary',
        type: 'integer',
        example: 2026
      }
    */

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

/* ===========================
   ✅ GET PAYROLL BY ID
=========================== */

router.get(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Get payroll record by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll record ID',
        required: true,
        type: 'integer'
      }
    */

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

/* ===========================
   ✅ UPDATE PAYROLL
=========================== */

router.put(
  "/update/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Update payroll record by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll record ID',
        required: true,
        type: 'integer'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          properties: {
            employeeName: { type: 'string', example: 'Ravi Kumar' },
            title: { type: 'string', example: 'Software Engineer' },
            salary: { type: 'number', example: 30000 },
            overtimeHours: { type: 'number', example: 5 },
            overtimePay: { type: 'number', example: 1000 },
            totalSalary: { type: 'number', example: 41000 },
            payDate: { type: 'string', format: 'date', example: '2026-02-01' },
            paymentType: { type: 'string', example: 'Bank Transfer' },
            status: { type: 'string', example: 'Paid' }
          }
        }
      }
    */

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

/* ===========================
   ✅ UPDATE PAYROLL STATUS
=========================== */

router.patch(
  "/:id/status",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Update payroll status only'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll record ID',
        required: true,
        type: 'integer'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          type: 'object',
          properties: {
            status: { type: 'string', example: 'Paid' },
            paidDate: { type: 'string', format: 'date', example: '2026-02-01' }
          }
        }
      }
    */

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

/* ===========================
   ✅ DELETE PAYROLL (Soft Delete)
=========================== */

router.delete(
  "/delete/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Payroll']
      #swagger.summary = 'Soft delete payroll record by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Payroll record ID',
        required: true,
        type: 'integer'
      }
    */

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

module.exports = router;