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

const {
  createHealthInsurance,
  getAllHealthInsurance,
  getAllPolicyNumbers,
  getHealthInsuranceById,
  getHealthInsuranceByPolicyNumber,
  getHealthInsuranceByEmployee,
  updateHealthInsurance,
  deleteHealthInsurance,
  getHealthInsuranceSummary,
  getExpiringPolicies,
} = require("../../controllers/hrandpayrollsController/healthInsuranceController");

/**
 * @swagger
 * tags:
 *   name: Health Insurance
 *   description: Health insurance management APIs
 */

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

/* ── Special routes (must come before /:id to avoid param conflicts) ── */

router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Get health insurance summary statistics'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = {
      description: 'Summary retrieved successfully'
    }
  */
  getHealthInsuranceSummary(req, res);
});

router.get("/expiring", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Get expiring policies (next 30 days)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['days'] = {
      in: 'query',
      type: 'integer',
      description: 'Number of days to check for expiry (default 30)'
    }
  */
  getExpiringPolicies(req, res);
});

router.get("/policy/:policy_number", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Get health insurance by policy number'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['policy_number'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Policy number to search'
    }
  */
  getHealthInsuranceByPolicyNumber(req, res);
});

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

/* ── Standard CRUD ── */

router.post("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Create new health insurance record'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              employee_id: { type: "string" },
              policy_number: { type: "string" },
              provider: { type: "string" },
              coverage_amount: { type: "number" },
              premium_amount: { type: "number" },
              start_date: { type: "string", format: "date" },
              end_date: { type: "string", format: "date" },
              family_coverage: { type: "boolean" }
            },
            required: ["employee_id", "policy_number", "provider", "start_date"]
          }
        }
      }
    }
  */
  createHealthInsurance(req, res);
});
// Add this route along with your other health insurance routes
router.get('/health-insurance/policies/list', authenticate, (req, res) => {

  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Get list of all health insurance policy numbers'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  getAllPolicyNumbers(req, res);
});
router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Get all health insurance 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['provider'] = { in: 'query', type: 'string' }
    #swagger.parameters['is_active'] = { in: 'query', type: 'boolean' }
  */
  getAllHealthInsurance(req, res);
});

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

router.put("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Update health insurance by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string'
    }
  */
  updateHealthInsurance(req, res);
});

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Health Insurance']
    #swagger.summary = 'Delete health insurance record (soft delete)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string'
    }
  */
  deleteHealthInsurance(req, res);
});

module.exports = router;