const express = require("express");
const router = express.Router();
const controller = require("../../controllers/hrandpayrollsController/allowancePackagesController");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Allowance Packages
 *   description: Allowance packages management APIs for Step 4 dropdown and CRUD operations
 */

// List all packages (for Step 4 dropdown)
router.get("/list", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Allowance Packages']
    #swagger.summary = 'Get all allowance packages for dropdown/auto-fetch (lightweight, no pagination)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = {
      description: 'List of allowance packages retrieved successfully'
    }
    #swagger.responses[401] = {
      description: 'Unauthorized - Invalid or missing token'
    }
  */
  controller.getAllPackages(req, res);
});

// Get single package by ID (auto-fill on selection)
router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Allowance Packages']
    #swagger.summary = 'Get allowance package by ID (auto-fill on selection)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Allowance package ID'
    }
    #swagger.responses[200] = {
      description: 'Allowance package retrieved successfully'
    }
    #swagger.responses[404] = {
      description: 'Allowance package not found'
    }
  */
  controller.getPackageById(req, res);
});

// Create / Update / Delete (for your packages management page later)
router.post("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Allowance Packages']
    #swagger.summary = 'Create a new allowance package'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              package_name: { type: "string" },
              package_code: { type: "string" },
              description: { type: "string" },
              allowances: { 
                type: "array", 
                items: { 
                  type: "object",
                  properties: {
                    allowance_type: { type: "string" },
                    amount: { type: "number" },
                    is_taxable: { type: "boolean" }
                  }
                }
              },
              is_active: { type: "boolean" },
              effective_from: { type: "string", format: "date" },
              effective_to: { type: "string", format: "date" },
              org_id: { type: "string" },
              created_by: { type: "string" }
            },
            required: ["package_name", "package_code", "org_id"]
          }
        }
      }
    }
    #swagger.responses[201] = {
      description: 'Allowance package created successfully'
    }
    #swagger.responses[400] = {
      description: 'Bad request - Invalid data'
    }
  */
  controller.createPackage(req, res);
});

router.put("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Allowance Packages']
    #swagger.summary = 'Update allowance package by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Allowance package ID'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              package_name: { type: "string" },
              package_code: { type: "string" },
              description: { type: "string" },
              allowances: { 
                type: "array", 
                items: { 
                  type: "object",
                  properties: {
                    allowance_type: { type: "string" },
                    amount: { type: "number" },
                    is_taxable: { type: "boolean" }
                  }
                }
              },
              is_active: { type: "boolean" },
              effective_from: { type: "string", format: "date" },
              effective_to: { type: "string", format: "date" },
              updated_by: { type: "string" }
            }
          }
        }
      }
    }
    #swagger.responses[200] = {
      description: 'Allowance package updated successfully'
    }
    #swagger.responses[404] = {
      description: 'Allowance package not found'
    }
  */
  controller.updatePackage(req, res);
});

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Allowance Packages']
    #swagger.summary = 'Delete allowance package (soft delete)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'Allowance package ID'
    }
    #swagger.responses[200] = {
      description: 'Allowance package deleted successfully'
    }
    #swagger.responses[404] = {
      description: 'Allowance package not found'
    }
  */
  controller.deletePackage(req, res);
});

module.exports = router;