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

/**
 * @swagger
 * tags:
 *   name: Organization PF
 *   description: Organization Provident Fund Management - Create, Read, Update, Delete operations
 */

/* CREATE - Create new organization PF record */
router.post("/create", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Create a new organization PF record'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            required: ["pf_number"],
            properties: {
              pf_number: { type: "string", example: "MH/BAN/123456/0001" },
              uan_number: { type: "string", example: "123456789012345" },
              employee_contribution_percent: { type: "number", example: 12 },
              employer_contribution_percent: { type: "number", example: 12 },
              pension_scheme_applicable: { type: "string", enum: ["Yes", "No"], example: "Yes" },
              gratuity_eligible: { type: "string", enum: ["Yes", "No", "Conditional"], example: "Yes" },
              gratuity_start_date: { type: "string", format: "date", example: "2024-01-01" },
              gratuity_years_required: { type: "number", example: 5 },
              pf_join_date: { type: "string", format: "date", example: "2024-01-01" },
              status: { type: "string", enum: ["Active", "Inactive"], example: "Active" },
              description: { type: "string", example: "Standard PF for IT department" }
            }
          }
        }
      }
    }
    #swagger.responses[201] = {
      description: 'PF record created successfully',
      schema: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" },
          data: {
            type: "object",
            properties: {
              id: { type: "integer" },
              pf_number: { type: "string" },
              uan_number: { type: "string" },
              status: { type: "string" }
            }
          }
        }
      }
    }
    #swagger.responses[400] = {
      description: 'Bad request - Invalid data or duplicate PF number'
    }
    #swagger.responses[401] = {
      description: 'Unauthorized - Invalid or missing token'
    }
  */
  controller.createOrganizationPF(req, res);
});

/* READ - Get all organization PF records with pagination */
router.get("/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Get all organization PF records with pagination and filters'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['page'] = { in: 'query', type: 'integer', description: 'Page number', default: 1 }
    #swagger.parameters['limit'] = { in: 'query', type: 'integer', description: 'Items per page', default: 10 }
    #swagger.parameters['search'] = { in: 'query', type: 'string', description: 'Search by PF number or UAN' }
    #swagger.parameters['status'] = { in: 'query', type: 'string', enum: ['Active', 'Inactive'], description: 'Filter by status' }
    #swagger.responses[200] = {
      description: 'PF records retrieved successfully',
      schema: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          data: {
            type: "array",
            items: {
              type: "object",
              properties: {
                id: { type: "integer" },
                pf_number: { type: "string" },
                uan_number: { type: "string" },
                employee_contribution_percent: { type: "number" },
                employer_contribution_percent: { type: "number" },
                pension_scheme_applicable: { type: "string" },
                gratuity_eligible: { type: "string" },
                gratuity_start_date: { type: "string", format: "date" },
                gratuity_years_required: { type: "number" },
                pf_join_date: { type: "string", format: "date" },
                status: { type: "string" },
                description: { type: "string" }
              }
            }
          },
          pagination: {
            type: "object",
            properties: {
              page: { type: "integer" },
              limit: { type: "integer" },
              total: { type: "integer" },
              totalPages: { type: "integer" }
            }
          }
        }
      }
    }
    #swagger.responses[401] = {
      description: 'Unauthorized'
    }
  */
  controller.getAllOrganizationPF(req, res);
});

/* READ - Get PF numbers list for dropdown */
router.get("/list", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Get PF numbers list for dropdown selection'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = {
      description: 'PF numbers retrieved successfully',
      schema: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          data: {
            type: "array",
            items: {
              type: "object",
              properties: {
                id: { type: "integer" },
                pf_number: { type: "string" },
                uan_number: { type: "string" },
                employee_contribution_percent: { type: "number" },
                employer_contribution_percent: { type: "number" },
                status: { type: "string" }
              }
            }
          }
        }
      }
    }
  */
  controller.getOrganizationPFList(req, res);
});

/* READ - Get PF record by PF number */
router.get("/pf/:pf_number", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Get PF record by PF number'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['pf_number'] = {
      in: 'path',
      required: true,
      type: 'string',
      description: 'PF Number'
    }
    #swagger.responses[200] = {
      description: 'PF record retrieved successfully',
      schema: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          data: {
            type: "object",
            properties: {
              id: { type: "integer" },
              pf_number: { type: "string" },
              uan_number: { type: "string" },
              employee_contribution_percent: { type: "number" },
              employer_contribution_percent: { type: "number" },
              pension_scheme_applicable: { type: "string" },
              gratuity_eligible: { type: "string" },
              gratuity_start_date: { type: "string", format: "date" },
              gratuity_years_required: { type: "number" }
            }
          }
        }
      }
    }
    #swagger.responses[404] = {
      description: 'PF number not found'
    }
  */
  controller.getOrganizationPFByNumber(req, res);
});

/* READ - Get PF record by ID */
router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Get PF record by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'integer',
      description: 'PF Record ID'
    }
    #swagger.responses[200] = {
      description: 'PF record retrieved successfully'
    }
    #swagger.responses[404] = {
      description: 'PF record not found'
    }
  */
  controller.getOrganizationPFById(req, res);
});

/* UPDATE - Update organization PF record */
router.put("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Update organization PF record by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'integer',
      description: 'PF Record ID'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              pf_number: { type: "string" },
              uan_number: { type: "string" },
              employee_contribution_percent: { type: "number" },
              employer_contribution_percent: { type: "number" },
              pension_scheme_applicable: { type: "string", enum: ["Yes", "No"] },
              gratuity_eligible: { type: "string", enum: ["Yes", "No", "Conditional"] },
              gratuity_start_date: { type: "string", format: "date" },
              gratuity_years_required: { type: "number" },
              pf_join_date: { type: "string", format: "date" },
              status: { type: "string", enum: ["Active", "Inactive"] },
              description: { type: "string" }
            }
          }
        }
      }
    }
    #swagger.responses[200] = {
      description: 'PF record updated successfully'
    }
    #swagger.responses[404] = {
      description: 'PF record not found'
    }
  */
  controller.updateOrganizationPF(req, res);
});

/* DELETE - Delete organization PF record (soft delete) */
router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organization PF']
    #swagger.summary = 'Delete organization PF record (soft delete)'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      required: true,
      type: 'integer',
      description: 'PF Record ID'
    }
    #swagger.responses[200] = {
      description: 'PF record deleted successfully',
      schema: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" }
        }
      }
    }
    #swagger.responses[404] = {
      description: 'PF record not found'
    }
  */
  controller.deleteOrganizationPF(req, res);
});

module.exports = router;