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

const employeeRelievingController = require("../../controllers/postgre/employeeRelievingController");
const authenticate = require("../../middlewares/authenticate");
const upload = require("../../middlewares/upload");

// Configure file upload for relieving documents
const relievingUpload = upload.fields([
  { name: "resignation_letter_file", maxCount: 1 },
  { name: "acceptance_letter_file", maxCount: 1 },
  { name: "relieving_letter_file", maxCount: 1 },
  { name: "exit_interview_file", maxCount: 1 },
  { name: "settlement_document_file", maxCount: 1 },
]);

/**
 * @swagger
 * tags:
 *   name: Employee Relieving
 *   description: Employee relieving/resignation management APIs
 */

/* ===========================
   ✅ CREATE EMPLOYEE RELIEVING
=========================== */

router.post(
  "/create",
  authenticate,
  relievingUpload,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Relieving']
      #swagger.summary = 'Create employee relieving 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['resignation_date'] = {
        in: 'formData',
        type: 'string',
        format: 'date',
        description: 'Date of resignation',
        required: false
      }
      #swagger.parameters['last_working_day'] = {
        in: 'formData',
        type: 'string',
        format: 'date',
        description: 'Last working day',
        required: false
      }
      #swagger.parameters['reason_for_resignation'] = {
        in: 'formData',
        type: 'string',
        description: 'Reason for resignation',
        required: false
      }
      #swagger.parameters['resignation_letter_file'] = {
        in: 'formData',
        type: 'file',
        description: 'Resignation letter file',
        required: false
      }
    */
    employeeRelievingController.createEmployeeRelieving(req, res);
  }
);

/* ===========================
   ✅ UPDATE RELIEVING RECORD
=========================== */

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

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

router.patch(
  "/:id/approval",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Relieving']
      #swagger.summary = 'Update approval status'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        type: 'integer',
        description: 'Relieving 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
      }
    */
    employeeRelievingController.updateApprovalStatus(req, res);
  }
);

/* ===========================
   ✅ GET ALL RELIEVING RECORDS
=========================== */

router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Employee Relieving']
      #swagger.summary = 'Get all relieving 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
      }
    */
    employeeRelievingController.getAllRelievingRecords(req, res);
  }
);

/* ===========================
   ✅ GET RELIEVING SUMMARY
=========================== */

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

/* ===========================
   ✅ GET RELIEVING BY EMPLOYEE
=========================== */

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

/* ===========================
   ✅ GET RELIEVING BY ID
=========================== */

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

/* ===========================
   ✅ DELETE RELIEVING RECORD
=========================== */

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

module.exports = router;