

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



router.use((req, res, next) => {
  console.log(`📌 Deduction route hit: ${req.method} ${req.originalUrl}`);
  next();
});


router.get("/ping", authenticate, (req, res) => {
  console.log("✅ PING endpoint hit!");
  
  res.json({ success: true, message: "Deduction routes are working!" });
});
/**
 * @swagger
 * tags:
 *   name: Deduction Management
 *   description: Deduction rules, employee salary, and deduction calculation APIs
 */

/* ===========================
   ✅ DEDUCTION RULES CRUD (Matches Product pattern)
=========================== */

/**
 * @swagger
 * /api/deductions/rules/create:
 *   post:
 *     summary: Create a new deduction rule
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [rule_name, rule_type]
 *             properties:
 *               rule_name: { type: string }
 *               rule_type: { type: string, enum: [percentage, fixed] }
 *               percentage: { type: number }
 *               threshold_minutes: { type: integer }
 *               max_per_day: { type: number }
 *               calculation_type: { type: string, enum: [percentage, fixed] }
 *               max_amount: { type: number }
 *               min_amount: { type: number }
 *               applies_to: { type: string, enum: [all, department] }
 *               department_id: { type: integer }
 *               description: { type: string }
 *               is_active: { type: boolean }
 */
router.post(
  "/rules/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Create Deduction Rule'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.createDeductionRule(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/rules/all:
 *   get:
 *     summary: Get all deduction rules with filters
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: query
 *         name: rule_type
 *         schema: { type: string }
 *       - in: query
 *         name: is_active
 *         schema: { type: boolean }
 *       - in: query
 *         name: department_id
 *         schema: { type: integer }
 */
router.get(
  "/rules/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get all deduction rules'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getDeductionRules(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/rules/{id}:
 *   get:
 *     summary: Get deduction rule by ID
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.get(
  "/rules/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get deduction rule by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getDeductionRuleById(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/rules/{id}:
 *   put:
 *     summary: Update deduction rule
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.put(
  "/rules/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Update deduction rule'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.updateDeductionRule(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/rules/{id}:
 *   delete:
 *     summary: Delete deduction rule (soft delete)
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.delete(
  "/rules/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Delete deduction rule'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.deleteDeductionRule(req, res);
  }
);

/* ===========================
   ✅ EMPLOYEE SALARY CRUD (Matches Product pattern)
=========================== */

/**
 * @swagger
 * /api/deductions/employee-salary/create:
 *   post:
 *     summary: Create employee salary structure
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [employee_id]
 *             properties:
 *               employee_id: { type: string }
 *               basic: { type: number }
 *               hra: { type: number }
 *               conveyance: { type: number }
 *               special_allowance: { type: number }
 *               shift_allowance: { type: number }
 *               holiday_wages: { type: number }
 *               advance_bonus: { type: number }
 *               monthly_gross: { type: number }
 *               effective_from: { type: string, format: date }
 */
router.post(
  "/employee-salary/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Create employee salary structure'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.createEmployeeSalary(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/employee-salary/all:
 *   get:
 *     summary: Get all employees salary with pagination
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema: { type: integer, default: 1 }
 *       - in: query
 *         name: limit
 *         schema: { type: integer, default: 10 }
 *       - in: query
 *         name: search
 *         schema: { type: string }
 *       - in: query
 *         name: department_id
 *         schema: { type: integer }
 */
router.get(
  "/employee-salary/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get all employees salary'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getAllEmployeesSalary(req, res);
  }
);


/**
 * @swagger
 * /api/deductions/history/all:
 *   get:
 *     summary: Get deduction history with filters
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: query
 *         name: employee_id
 *         schema: { type: string }
 *       - in: query
 *         name: year
 *         schema: { type: integer }
 *       - in: query
 *         name: department_id
 *         schema: { type: integer }
 */
router.get(
  "/history/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get deduction history'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getDeductionHistory(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/employee-salary/{employee_id}:
 *   get:
 *     summary: Get employee salary structure by ID
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: employee_id
 *         required: true
 *         schema: { type: string }
 */
router.get(
  "/employee-salary/:employee_id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get employee salary by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getEmployeeSalary(req, res);
  }
);


/* ===========================
   ✅ DEDUCTION SUMMARY & HISTORY
=========================== */

/**
 * @swagger
 * /api/deductions/summary:
 *   get:
 *     summary: Get complete deduction summary for an employee
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: query
 *         name: employee_id
 *         required: true
 *         schema: { type: string }
 *       - in: query
 *         name: month
 *         required: true
 *         schema: { type: integer, minimum: 1, maximum: 12 }
 *       - in: query
 *         name: year
 *         required: true
 *         schema: { type: integer, minimum: 2000 }
 */
router.get(
  "/summary",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get deduction summary'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getDeductionSummary(req, res);
  }
);
/**
 * @swagger
 * /api/deductions/employee-salary/{employee_id}:
 *   put:
 *     summary: Update employee salary structure
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: employee_id
 *         required: true
 *         schema: { type: string }
 */
router.put(
  "/employee-salary/:employee_id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Update employee salary'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.updateEmployeeSalary(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/employee-salary/{employee_id}:
 *   delete:
 *     summary: Delete employee salary structure
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: employee_id
 *         required: true
 *         schema: { type: string }
 */
router.delete(
  "/employee-salary/:employee_id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Delete employee salary'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.deleteEmployeeSalary(req, res);
  }
);

/* ===========================
   ✅ DEPARTMENTS & PAYROLL GROUPS
=========================== */

/**
 * @swagger
 * /api/deductions/departments/all:
 *   get:
 *     summary: Get all departments for dropdown
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 */
router.get(
  "/departments/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get all departments'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getDepartments(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/payroll-groups/all:
 *   get:
 *     summary: Get payroll groups (departments with employee counts)
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 */
router.get(
  "/payroll-groups/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get payroll groups'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getPayrollGroups(req, res);
  }
);





/* ===========================
   ✅ MANUAL DEDUCTIONS
=========================== */

/**
 * @swagger
 * /api/deductions/manual/create:
 *   post:
 *     summary: Add manual deduction for an employee
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required: [employee_id, month, year, deduction_type, amount]
 *             properties:
 *               employee_id: { type: string }
 *               month: { type: integer, minimum: 1, maximum: 12 }
 *               year: { type: integer }
 *               deduction_type: { type: string }
 *               amount: { type: number }
 *               reason: { type: string }
 */
router.post(
  "/manual/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Create manual deduction'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.addManualDeduction(req, res);
  }
);

// Add this route after your existing routes
router.get("/rules", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Deduction Management']
    #swagger.summary = 'Get all deduction rules (for dropdowns)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  deductionController.getDeductionRules(req, res);
});
/**
 * @swagger
 * /api/deductions/manual/all:
 *   get:
 *     summary: Get all manual deductions
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 */
router.get(
  "/manual/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get all manual deductions'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getManualDeductions ? deductionController.getManualDeductions(req, res) : 
    deductionController.getDeductionHistory(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/manual/{id}:
 *   get:
 *     summary: Get manual deduction by ID
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.get(
  "/manual/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Get manual deduction by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.getManualDeductionById ? deductionController.getManualDeductionById(req, res) :
    res.status(501).json({ success: false, message: "Not implemented" });
  }
);

/**
 * @swagger
 * /api/deductions/manual/{id}:
 *   put:
 *     summary: Update manual deduction
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.put(
  "/manual/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Update manual deduction'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.updateManualDeduction(req, res);
  }
);

/**
 * @swagger
 * /api/deductions/manual/{id}:
 *   delete:
 *     summary: Delete manual deduction
 *     tags: [Deduction Management]
 *     security: [{ bearerAuth: [] }]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: integer }
 */
router.delete(
  "/manual/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Deduction Management']
      #swagger.summary = 'Delete manual deduction'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    deductionController.deleteManualDeduction(req, res);
  }
);



router.post('/generate-pdf', authenticate,(req, res) => { /**
 * @swagger
 * /api/deductions/generate-pdf:
 *   post:
 *     summary: Generate employee deduction report as PDF
 *     description: Generates a PDF report of employee deductions for a specific month and year using Puppeteer
 *     tags: [Deductions]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - employee_id
 *               - month
 *               - year
 *             properties:
 *               employee_id:
 *                 type: string
 *                 description: Employee code or ID
 *                 example: "EMP001"
 *               month:
 *                 type: integer
 *                 description: Month number (1-12)
 *                 example: 11
 *                 minimum: 1
 *                 maximum: 12
 *               year:
 *                 type: integer
 *                 description: Year (YYYY)
 *                 example: 2024
 *                 minimum: 2000
 *                 maximum: 2100
 *     responses:
 *       200:
 *         description: PDF generated successfully
 *         content:
 *           application/pdf:
 *             schema:
 *               type: string
 *               format: binary
 *             description: PDF file download
 *       400:
 *         description: Missing required parameters
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "employee_id, month, and year are required"
 *       404:
 *         description: Employee not found
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Employee not found"
 *       500:
 *         description: Server error
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: false
 *                 message:
 *                   type: string
 *                   example: "Internal server error"
 */
deductionController.generateDeductionPDF(req, res) });

module.exports = router;