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

// ==================== CREATE ====================
// NEW ROUTE: Create RFQ from PR Inventory (for New Requests tab)
router.post("/create-from-pr-inventory", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Create RFQ from PR Inventory (New Requests tab)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.createRFQFromPRInventory(req, res);
});

router.post("/create-from-pr/:pr_id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Create RFQ from Purchase Requisition'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.createRFQFromPR(req, res);
});

router.post("/manual", authenticate,(req, res) =>{
  /*
    #swagger.tags = ['RFQ Postgre']     
    #swagger.summary = 'Create RFQ manually'
    #swagger.security = [{ "bearerAuth": [] }]
  */        
  rfqController.createManualRFQ(req, res);
});

router.post("/create-from-requisition", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Create RFQ from requisition'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.createRFQFromRequisition(req, res);
});

router.post("/bulk-create", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Bulk create RFQs'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.bulkCreateRFQs(req, res);
});

router.post("/:id/send-to-vendors", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Send RFQ to vendors'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.sendRFQToVendors(req, res);
});

router.post("/:id/quotation-prices", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Save quotation prices'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.saveQuotationPrices(req, res);
});

router.post("/:id/upload-quotation", authenticate, rfqController.uploadQuotationFileMiddleware, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Upload quotation document'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.uploadQuotation(req, res);
});

// ==================== GET ====================

router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get all RFQs 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' }
  */
  rfqController.getAllRFQs(req, res);
});

router.get("/statistics/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get RFQ statistics summary'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getRFQStatistics(req, res);
});

router.get("/comparison-list", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get comparison RFQ list'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getComparisonRFQList(req, res);
});

router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get RFQ by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getRFQById(req, res);
});

router.get("/:id/items", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get RFQ items'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getRFQItems(req, res);
});

// Add this route - specific route BEFORE generic :id routes
router.get("/:rfqId/vendor-items/:vendorId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get vendor-specific RFQ items'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getVendorRFQItems(req, res);
});
router.get("/:id/comparison", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get quotation comparison'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getQuotationComparison(req, res);
});

router.get("/:id/quotation-prices", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get quotation prices'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getQuotationPrices(req, res);
});

router.get("/:id/uploaded-vendors", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get uploaded vendors'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getUploadedVendors(req, res);
});

router.get("/:id/upload-quotation", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get quotation upload'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getQuotationUpload(req, res);
});

// ==================== EXTERNAL VENDOR ROUTES ====================

router.post("/:id/external-vendors", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Add external vendors to RFQ by email'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.addExternalVendorsToRFQ(req, res);
});

router.get("/:id/external-vendors", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get external vendors for RFQ'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getExternalVendorsForRFQ(req, res);
});

router.post("/:id/upload-external-quotation", authenticate, rfqController.uploadQuotationFileMiddleware, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Upload quotation for external vendor'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.uploadExternalQuotation(req, res);
});

router.get("/:id/external-uploaded-quotations", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Get uploaded quotations from external vendors'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.getExternalUploadedQuotations(req, res);
});

// ==================== UPDATE ====================

router.put("/:id/status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Update RFQ status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.updateRFQStatus(req, res);
});

router.put("/:id/deadline", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Update RFQ deadline'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.updateRFQDeadline(req, res);
});

// ==================== DELETE ====================

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Soft delete RFQ'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.deleteRFQ(req, res);
});

router.delete("/hard/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Hard delete RFQ (permanent)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.hardDeleteRFQ(req, res);
});

router.delete("/:id/quotation-prices", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Delete quotation prices'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.deleteQuotationPrices(req, res);
});

router.delete("/:id/upload-quotation", authenticate, (req, res) => {
  /*
    #swagger.tags = ['RFQ Postgre']
    #swagger.summary = 'Delete quotation upload'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  rfqController.deleteQuotationUpload(req, res);
});

module.exports = router;