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

/**
 * @swagger
 * tags:
 *   name: SubCategories
 *   description: SubCategory management APIs
 */

// CREATE subcategory (admin only)
router.post("/create", authenticate, authorize("admin"), (req, res) => {
  /* 
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Create a new subcategory'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'SubCategory details including categoryId and orgId',
      required: true,
      schema: { 
        subCategoryName: "Frontend Development", 
        categoryId: "64f5a2d8c9e4b3a1f0a12345",
        orgId: "64f5a2d8c9e4b3a1f0a67890"
      }
    }
  */
  subCategoryController.createSubCategory(req, res);
});

// GET all subcategories (admin + manager allowed)
router.get("/get/all", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /* 
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Get all subcategories'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  subCategoryController.getSubCategories(req, res);
});

// GET subcategory by ID (all logged in users)
router.get("/get/:id", authenticate, (req, res) => {
  /* 
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Get a subcategory by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'SubCategory ID', required: true }
  */
  subCategoryController.getSubCategoryById(req, res);
});

// GET subcategories by Category ID (admin + manager allowed)
router.get("/category/:categoryId", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Get all subcategories under a specific category'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['categoryId'] = { description: 'Category ID', required: true }
  */
  subCategoryController.getSubCategoriesByCategoryId(req, res);
});


// UPDATE subcategory (admin only)
router.put("/update/:id", authenticate, authorize("admin"), (req, res) => {
  /* 
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Update a subcategory by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'SubCategory ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Update data (subCategoryName, categoryId, orgId, status)',
      required: true,
      schema: { 
        subCategoryName: "Advanced Frontend", 
        categoryId: "64f5a2d8c9e4b3a1f0a12345",
        orgId: "64f5a2d8c9e4b3a1f0a67890",
        status: true 
      }
    }
  */
  subCategoryController.updateSubCategory(req, res);
});

// DELETE subcategory (admin only)
router.delete("/delete/:id", authenticate, authorize("admin"), (req, res) => {
  /* 
    #swagger.tags = ['SubCategories']
    #swagger.summary = 'Delete a subcategory by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'SubCategory ID', required: true }
  */
  subCategoryController.deleteSubCategory(req, res);
});

module.exports = router;
