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

// IMPORTANT: Specific routes must come BEFORE parameterized routes /:id

// CREATE
router.post(
  "/create",
  authenticate,
  upload.fields([
    { name: "panDocument", maxCount: 1 },
    { name: "licenseDocument", maxCount: 1 },
    { name: "bankDocument", maxCount: 1 },
  ]),
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Create a new vendor'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.createVendor(req, res);
  }
);

// GET ALL (without pagination) — before /:id
router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Get all vendors without pagination'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.getAllVendorsWithoutPagination(req, res);
  }
);

// GET VENDORS BY ITEM CODE — must be before /:id so Express doesn't treat "by-item" as an id
router.get(
  "/by-item",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Get vendors that supply a specific item'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['item_code'] = {
        in: 'query',
        required: true,
        type: 'string',
        description: 'Item code to filter vendors by'
      }
    */
    vendorController.getVendorsByItemCode(req, res);
  }
);

// GST LOOKUP — specific path, before /:id
router.get(
  "/gst-lookup/:gstin",
  (req, res) => {
    vendorController.lookupGstin(req, res);
  }
);

// GET ALL (with pagination)
router.get(
  "/",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Get all vendors 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' }
    */
    vendorController.getAllVendors(req, res);
  }
);

// GET BY ID — parameterized, must be after all specific GET routes
router.get(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Get vendor by ID'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.getVendorById(req, res);
  }
);

// UPDATE
router.put(
  "/:id",
  authenticate,
  upload.fields([
    { name: "panDocument", maxCount: 1 },
    { name: "licenseDocument", maxCount: 1 },
    { name: "bankDocument", maxCount: 1 },
  ]),
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Update vendor'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.updateVendor(req, res);
  }
);

// UPDATE STATUS
router.patch(
  "/:id/status",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Update vendor status'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.updateVendorStatus(req, res);
  }
);

// HARD DELETE — specific path before /:id soft delete
router.delete(
  "/hard/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Hard delete vendor'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.hardDeleteVendor(req, res);
  }
);

// SOFT DELETE
router.delete(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Vendor Postgre']
      #swagger.summary = 'Soft delete vendor'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    vendorController.deleteVendor(req, res);
  }
);

module.exports = router;