const express = require("express");
const router = express.Router();
const taxController = require("../../controllers/admin/taxController");
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Taxes
 *   description: Tax management APIs
 */

/* =====================================================
   CREATE TAX (ADMIN ONLY)
   ===================================================== */
router.post(
  "/create",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Create a new tax'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Tax details',
        required: true,
        schema: {
          name: "GST 18%",
          type: "GST",
          rates: { cgst: 9, sgst: 9, igst: 18 },
          applicableFor: "BOTH",
          isDefault: true
        }
      }
    */
    taxController.createTax(req, res);
  }
);

/* =====================================================
   GET ALL TAXES (ADMIN / MANAGER)
   ===================================================== */
router.get(
  "/get/all",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Get all taxes'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    taxController.getAllTaxes(req, res);
  }
);

/* =====================================================
   GET ACTIVE DEFAULT TAX
   ===================================================== */
router.get(
  "/get/active",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Get active default tax'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    taxController.getActiveDefaultTax(req, res);
  }
);

/* =====================================================
   UPDATE TAX (ADMIN ONLY)
   ===================================================== */
router.put(
  "/update/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Update a tax by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        description: 'Tax ID',
        required: true
      }
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Tax update fields',
        required: true,
        schema: {
          name: "GST 12%",
          rates: { cgst: 6, sgst: 6 },
          applicableFor: "INTRA_STATE",
          isDefault: false
        }
      }
    */
    taxController.updateTax(req, res);
  }
);

/* =====================================================
   ACTIVATE / DEACTIVATE TAX
   ===================================================== */
router.patch(
  "/toggle/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Activate or deactivate a tax'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        description: 'Tax ID',
        required: true
      }
    */
    taxController.toggleTaxStatus(req, res);
  }
);

/* =====================================================
   DELETE TAX (SOFT DELETE – ADMIN ONLY)
   ===================================================== */
router.delete(
  "/delete/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Taxes']
      #swagger.summary = 'Delete a tax'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        description: 'Tax ID',
        required: true
      }
    */
    taxController.deleteTax(req, res);
  }
);

module.exports = router;
