const express = require("express");
const router = express.Router();
const itemMasterController = require("../../controllers/procurement/itemMasterController");
const authenticate = require("../../middlewares/authenticate");
const upload = require("../../middlewares/upload");

// CREATE
router.post(
  "/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Create a new item'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          itemName: "Ethanol",
          uom: "Ltr",
          hsnCode: "2207",
          description: "High quality ethanol",
          qty: 50,
          tax: 18,
          groupId: 1,
          groupName: "Raw Materials"
        }
      }
    */
    itemMasterController.createItem(req, res);
  }
);

// BULK UPLOAD
router.post(
  "/bulk-upload",
  authenticate,
  upload.single('file'),
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Bulk upload items via Excel/CSV'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['file'] = {
        in: 'formData',
        type: 'file',
        required: true,
        description: 'Excel/CSV file with headers: Item Name, UOM, HSN Code, Quantity, Description, Tax (%), Group Name'
      }
    */
    itemMasterController.bulkUploadItems(req, res);
  }
);

// DOWNLOAD TEMPLATE
router.get(
  "/download-template",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Download Excel template for bulk upload'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.produces = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
    */
    itemMasterController.downloadTemplate(req, res);
  }
);

// GET ALL ITEM GROUPS
router.get(
  "/groups",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Get all item groups from group master'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.getItemGroups(req, res);
  }
);

// GET ALL (with pagination)
router.get(
  "/",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Get all items with pagination'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['search'] = { in: 'query', type: 'string' }
      #swagger.parameters['page'] = { in: 'query', type: 'integer' }
      #swagger.parameters['limit'] = { in: 'query', type: 'integer' }
    */
    itemMasterController.getAllItems(req, res);
  }
);

// GET ALL (without pagination)
router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Get all items without pagination'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.getAllItemsWithoutPagination(req, res);
  }
);

// GET BY ID
router.get(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Get item by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.getItemById(req, res);
  }
);

// UPDATE
router.put(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Update item'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.updateItem(req, res);
  }
);

// SOFT DELETE
router.delete(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Soft delete item'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.deleteItem(req, res);
  }
);

// HARD DELETE
router.delete(
  "/hard/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Item Master Postgre']
      #swagger.summary = 'Hard delete item (permanent)'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    itemMasterController.hardDeleteItem(req, res);
  }
);

module.exports = router;