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

const productController = require("../../controllers/sales/productController");
const upload = require("../../middlewares/upload");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Product Master
 *   description: Product creation, listing, update and deletion APIs
 */

/* ===========================
   ✅ CREATE PRODUCT
=========================== */
router.post(
  "/create",
  authenticate,
  upload.single("product_image"),
  (req, res) => {

    /*
      #swagger.tags = ['Product Master']
      #swagger.summary = 'Create Product'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']

      #swagger.parameters['product_name'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: 'ENA'
      }

      #swagger.parameters['product_code'] = {
        in: 'formData',
        type: 'string',
        required: true,
        example: 'P-1001'
      }

      #swagger.parameters['purity'] = {
        in: 'formData',
        type: 'number',
        example: 99.9
      }

      #swagger.parameters['unit'] = {
        in: 'formData',
        type: 'string',
        example: 'KL'
      }

      #swagger.parameters['denaturant_type'] = {
        in: 'formData',
        type: 'string',
        example: 'Applicable'
      }

      #swagger.parameters['hsn_code'] = {
        in: 'formData',
        type: 'string',
        example: '2207'
      }

      #swagger.parameters['gst_rate'] = {
        in: 'formData',
        type: 'number',
        example: 18
      }

      #swagger.parameters['storage_type'] = {
        in: 'formData',
        type: 'string',
        example: 'Tank'
      }

      #swagger.parameters['product_image'] = {
        in: 'formData',
        type: 'file'
      }
    */

    productController.createProduct(req, res);
  }
);

/* ===========================
   ✅ UPDATE PRODUCT
=========================== */
// router.put(
//   "/:id",
//   authenticate,
//   upload.single("product_image"),
//   (req, res) => {

//     /*
//       #swagger.tags = ['Product Master']
//       #swagger.summary = 'Update Product'
//       #swagger.security = [{ "bearerAuth": [] }]
//     */

//     productController.updateProduct(req, res);
//   }
// );
router.put(
  "/:id",
  authenticate,
  upload.single("product_image"),
  (req, res) => {
    /*
      #swagger.tags = ['Product Master']
      #swagger.summary = 'Patch update product by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    productController.updateProduct(req, res);
  }
);

/* ===========================
   ✅ GET ALL PRODUCTS
=========================== */
router.get(
  "/all",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Product Master']
      #swagger.summary = 'Get all products'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    productController.getProducts(req, res);
  }
);

/* ===========================
   ✅ GET PRODUCT BY ID
=========================== */
router.get(
  "/:id",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Product Master']
      #swagger.summary = 'Get product by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    productController.getProductById(req, res);
  }
);

/* ===========================
   ✅ DELETE PRODUCT
=========================== */
router.delete(
  "/:id",
  authenticate,
  (req, res) => {

    /*
      #swagger.tags = ['Product Master']
      #swagger.summary = 'Delete product (soft delete)'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    productController.deleteProduct(req, res);
  }
);

module.exports = router;