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

const discountController = require("../../controllers/admin/discountController");
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Discounts
 *   description: Discount management APIs
 */

/* =====================================================
   CREATE DISCOUNT (ADMIN ONLY)
   ===================================================== */
router.post(
  "/create",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Create a new discount'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Discount details',
        required: true,
        schema: {
          name: "New Year Offer",
          code: "NY2026",
          type: "PERCENTAGE",
          value: 10,
          maxDiscountAmount: 1000,
          minOrderAmount: 2000,
          appliesTo: "INVOICE",
          isStackable: false,
          isDefault: false
        }
      }
    */
    discountController.createDiscount(req, res);
  }
);

/* =====================================================
   GET ALL DISCOUNTS (ADMIN / MANAGER)
   ===================================================== */
router.get(
  "/get/all",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Get all discounts'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    discountController.getAllDiscounts(req, res);
  }
);

/* =====================================================
   GET ACTIVE DEFAULT DISCOUNT
   ===================================================== */
router.get(
  "/get/active",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Get active default discount'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    discountController.getActiveDefaultDiscount(req, res);
  }
);

/* =====================================================
   UPDATE DISCOUNT (ADMIN ONLY)
   ===================================================== */
router.put(
  "/update/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Update a discount by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = { description: 'Discount ID', required: true }
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Discount update fields',
        required: true,
        schema: {
          name: "Festival Offer",
          value: 15,
          maxDiscountAmount: 1500,
          isDefault: false
        }
      }
    */
    discountController.updateDiscount(req, res);
  }
);

/* =====================================================
   ACTIVATE / DEACTIVATE DISCOUNT
   ===================================================== */
router.put(
  "/toggle/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Activate or deactivate a discount'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = { description: 'Discount ID', required: true }
    */
    discountController.toggleDiscountStatus(req, res);
  }
);

/* =====================================================
   DELETE DISCOUNT (SOFT DELETE – ADMIN ONLY)
   ===================================================== */
router.delete(
  "/delete/:id",
  authenticate,
  authorize("admin"),
  (req, res) => {
    /*
      #swagger.tags = ['Discounts']
      #swagger.summary = 'Delete a discount'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = { description: 'Discount ID', required: true }
    */
    discountController.deleteDiscount(req, res);
  }
);

module.exports = router;
