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


/**
 * @swagger
 * tags:
 *   name: Categories
 *   description: Category management APIs
 */

// CREATE category with single image (admin only)
router.post( "/create", authenticate, authorize("admin"), upload.single("image"), // Multer middleware for single file
  (req, res) => {
    /* 
      #swagger.tags = ['Categories']
      #swagger.summary = 'Create a new category with image'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        description: 'Category details including orgId',
        required: true,
        schema: { categoryName: "Electronics", orgId: "64f5a2d8c9e4b3a1f0a12345" }
      }
    */
    
    categoryController.createCategory(req, res);
  }
);

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

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

// UPDATE category (admin only)
router.put("/update/:id", authenticate, authorize("admin"),upload.single("image"), (req, res) => {
  /* 
    #swagger.tags = ['Categories']
    #swagger.summary = 'Update a category by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Category ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Update data (categoryName, orgId, status)',
      required: true,
      schema: { categoryName: "Home Appliances", orgId: "64f5a2d8c9e4b3a1f0a12345", status: true }
    }
  */
  // Attach orgId from req.user if missing (for logging)
  req.body.orgId = req.body.orgId || req.user?.orgId;
  categoryController.updateCategory(req, res);
});

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

module.exports = router;
