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

const employeeTerminationController = require("../../controllers/hrandpayrollsController/employeeTerminationController");
const authenticate = require("../../middlewares/authenticate");
const upload = require("../../middlewares/upload");

// Configure file upload for termination documents
const terminationUpload = upload.fields([
  { name: "termination_letter_file", maxCount: 1 },
  { name: "investigation_report_file", maxCount: 1 },
  { name: "exit_interview_file", maxCount: 1 },
  { name: "settlement_document_file", maxCount: 1 },
]);

/**
 * @swagger
 * tags:
 *   name: Employee Termination
 *   description: Employee termination management APIs
 */

/* ===========================
   ✅ CREATE EMPLOYEE TERMINATION
=========================== */

router.post(
  "/create",
  authenticate,
  terminationUpload,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Create employee termination record'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['employee_code'] = {
        in: 'formData',
        type: 'string',
        description: 'Employee code',
        required: true
      }
      #swagger.parameters['org_id'] = {
        in: 'formData',
        type: 'string',
        description: 'Organization ID',
        required: true
      }
      #swagger.parameters['termination_date'] = {
        in: 'formData',
        type: 'string',
        format: 'date',
        description: 'Date of termination',
        required: false
      }
      #swagger.parameters['last_working_day'] = {
        in: 'formData',
        type: 'string',
        format: 'date',
        description: 'Last working day',
        required: false
      }
      #swagger.parameters['termination_type'] = {
        in: 'formData',
        type: 'string',
        description: 'Termination type (Voluntary, Involuntary, Mutual)',
        required: false
      }
      #swagger.parameters['primary_reason'] = {
        in: 'formData',
        type: 'string',
        description: 'Primary reason for termination',
        required: false
      }
      #swagger.parameters['termination_letter_file'] = {
        in: 'formData',
        type: 'file',
        description: 'Termination letter file',
        required: false
      }
    */
    employeeTerminationController.createEmployeeTermination(req, res);
  }
);

/* ===========================
   ✅ UPDATE TERMINATION RECORD
=========================== */

router.put(
  "/:id",
  authenticate,
  terminationUpload,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Update termination record by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Termination record ID',
        required: true
      }
    */
    employeeTerminationController.updateTerminationRecord(req, res);
  }
);

/* ===========================
   ✅ UPDATE TERMINATION APPROVAL STATUS
=========================== */

router.patch(
  "/:id/approval",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Update termination approval status'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Termination record ID',
        required: true
      }
      #swagger.parameters['approval_type'] = {
        in: 'body',
        type: 'string',
        description: 'Approval type (reporting_manager, hr, legal, final)',
        required: true
      }
      #swagger.parameters['status'] = {
        in: 'body',
        type: 'string',
        description: 'Status (Pending, Approved, Rejected)',
        required: true
      }
    */
    employeeTerminationController.updateTerminationApproval(req, res);
  }
);

/* ===========================
   ✅ GET ALL TERMINATION RECORDS
=========================== */

router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Get all termination records with pagination and filters'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = {
        in: 'query',
        type: 'integer',
        description: 'Page number',
        default: 1,
        required: false
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        type: 'integer',
        description: 'Records per page',
        default: 10,
        required: false
      }
      #swagger.parameters['status'] = {
        in: 'query',
        type: 'string',
        description: 'Filter by approval status',
        required: false
      }
      #swagger.parameters['org_id'] = {
        in: 'query',
        type: 'string',
        description: 'Filter by organization ID',
        required: false
      }
      #swagger.parameters['search'] = {
        in: 'query',
        type: 'string',
        description: 'Search by employee name or code',
        required: false
      }
    */
    employeeTerminationController.getAllTerminationRecords(req, res);
  }
);

/* ===========================
   ✅ GET TERMINATION SUMMARY
=========================== */

router.get(
  "/summary",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Get termination summary statistics'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['org_id'] = {
        in: 'query',
        type: 'string',
        description: 'Organization ID',
        required: false
      }
    */
    employeeTerminationController.getTerminationSummary(req, res);
  }
);

/* ===========================
   ✅ GET TERMINATION BY REASON
=========================== */

router.get(
  "/by-reason",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Termination']
      #swagger.summary = 'Get termination records grouped by reason'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['org_id'] = {
        in: 'query',
        type: 'string',
        description: 'Organization ID',
        required: false
      }
    */
    employeeTerminationController.getTerminationByReason(req, res);
  }
);

/* ===========================
   ✅ GET TERMINATION BY EMPLOYEE
=========================== */

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

/* ===========================
   ✅ GET TERMINATION BY ID
=========================== */

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

/* ===========================
   ✅ DELETE TERMINATION RECORD
=========================== */

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

module.exports = router;