const express = require("express");
const router = express.Router();
const productController = require("../../controllers/admin/productController");
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");
const upload = require("../../middlewares/upload");
const authorizePermission = require("../../middlewares/authorizePermission");
/**
 * @swagger
 * tags:
 *   name: Products
 *   description: Product management APIs
 */

// CREATE product (admin only)
router.post("/create", authenticate, authorize(["admin"]),upload.array("images", 5), (req, res) => {
  /*
    #swagger.tags = ['Products']
    #swagger.summary = 'Create a new product'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Product details including orgId, variants, category, subcategory',
      required: true,
      schema: {
        productName: "iPhone 15",
        category: "64f5a2d8c9e4b3a1f0a12345",
        subcategory: "64f5a2d8c9e4b3a1f0a12346",
        sku: "IP15-BLK-256",
        variant: ["64f5a2d8c9e4b3a1f0a12347"],
        stock: 50,
        price: 999,
        status: true,
        description: "Latest iPhone model",
        image: ["path/to/image1.jpg", "path/to/image2.jpg"],
        brand: "Apple",
        orgId: "64f5a2d8c9e4b3a1f0a12348"
      }
    }
  */
  // req.body.orgId = req.body.orgId || req.user?.orgId;
  productController.createProduct(req, res);
});

// GET all products (admin + manager allowed)
router.get("/get/all", authenticate,  (req, res) => {
  /*
    #swagger.tags = ['Products']
    #swagger.summary = 'Get all products with pagination'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  productController.getProducts(req, res);
});

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

// UPDATE product (admin only)
router.put("/update/:id", authenticate, upload.array("images", 5),  (req, res) => {
  /*
    #swagger.tags = ['Products']
    #swagger.summary = 'Update a product by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Product ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Update product data (any fields including status)',
      required: true,
      schema: {
        productName: "iPhone 15 Pro",
        stock: 45,
        price: 1099,
        status: true,
        description: "Updated description",
        image: ["path/to/image1.jpg"],
        brand: "Apple"
      }
    }
  */
  req.body.orgId = req.body.orgId || req.user?.orgId;
  productController.updateProduct(req, res);
});

// UPDATE product status (admin only)
router.put("/update/status/:id", authenticate, authorize("admin"), (req, res) => {
  /*
    #swagger.tags = ['Products']
    #swagger.summary = 'Update product status by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Product ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Status field only',
      required: true,
      schema: { status: true }
    }
  */
  productController.updateProductStatus(req, res);
});

// DELETE product (admin only)
router.delete("/delete/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Products']
    #swagger.summary = 'Soft delete a product by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Product ID', required: true }
  */
  productController.deleteProduct(req, res);
});

module.exports = router;
