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


/**
 * @swagger
 * tags:
 *   name: Brands
 *   description: brands management APIs
 */

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

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

// GET brands by ID (all logged in users)
router.get("/get/:id", authenticate, (req, res) => {
  /* 
    #swagger.tags = ['Brands']
    #swagger.summary = 'Get a brand by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Brand ID', required: true }
  */
  brandController.getBrandById(req, res);
});

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

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

module.exports = router;
