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

const {
  createProvidentFund,
  getAllProvidentFunds,
  getProvidentFundById,
  getProvidentFundByEmployee,
   getProvidentFundByPFNumber, 
  updateProvidentFund,
  deleteProvidentFund,
  getProvidentFundSummary,
} = require("../../controllers/hrandpayrollsController/providentFundController");

/**
 * @swagger
 * tags:
 *   name: Provident Fund
 *   description: Provident fund management APIs
 */

/* ── NOTE: mount this router at /api/provident-fund in your app.js / index.js ── */

/* ── Summary (must come before /:id so Express doesn't treat "summary" as an id) ── */

router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund summary statistics'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = {
      description: 'Summary retrieved successfully'
    }
  */
  getProvidentFundSummary(req, res);
});

/* ── By employee (used by Benefits form Step 2 auto-fill) ── */

router.get("/employee/:employee_id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund by employee ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['employee_id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Employee ID'
    }
  */
  getProvidentFundByEmployee(req, res);
});

/* ── Standard CRUD ── */

router.post("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Create new provident fund record'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              employee_id: { type: "string" },
              pf_number: { type: "string" },
              uan_number: { type: "string" },
              employee_contribution: { type: "number" },
              employer_contribution: { type: "number" },
              enrollment_date: { type: "string", format: "date" },
              nomination_details: { type: "string" }
            },
            required: ["employee_id", "pf_number", "enrollment_date"]
          }
        }
      }
    }
  */
  createProvidentFund(req, res);
});

router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get all provident fund records with filters'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['page'] = { in: 'query', type: 'integer' }
    #swagger.parameters['limit'] = { in: 'query', type: 'integer' }
    #swagger.parameters['employee_id'] = { in: 'query', type: 'string' }
    #swagger.parameters['is_active'] = { in: 'query', type: 'boolean' }
  */
  getAllProvidentFunds(req, res);
});

router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Provident fund record ID'
    }
  */
  getProvidentFundById(req, res);
});


/* ── Summary ── */
router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund summary statistics'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = {
      description: 'Summary retrieved successfully'
    }
  */
  getProvidentFundSummary(req, res);
});

/* ── By PF number (must be before /:id) ── */
router.get("/pf/:pf_number", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund by PF number (used by Benefits Step 2 auto-fill)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['pf_number'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'PF number e.g. MHBAN00123456789001'
    }
  */
  getProvidentFundByPFNumber(req, res);
});

/* ── By employee ── */
router.get("/employee/:employee_id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund by employee ID (used by Benefits Step 2 auto-fill)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['employee_id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Employee ID'
    }
  */
  getProvidentFundByEmployee(req, res);
});

/* ── By ID (integer — must come last among GETs) ── */
router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Get provident fund by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Provident fund record ID'
    }
  */
  getProvidentFundById(req, res);
});
router.put("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Update provident fund by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string'
    }
  */
  updateProvidentFund(req, res);
});

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Provident Fund']
    #swagger.summary = 'Delete provident fund record (soft delete)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string'
    }
  */
  deleteProvidentFund(req, res);
});

module.exports = router;