

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

// ==================== CREATE ====================

router.post("/create-from-pr/:pr_id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Create purchase order from approved PR'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.createPOFromApprovedPR(req, res);
});

router.post("/create-from-rfq/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Create purchase order from RFQ'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.createPOFromRFQ(req, res);
});

router.post("/create-from-comparison/:rfqId/:vendorId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Create purchase order from quotation comparison'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.createPOFromComparison(req, res);
});

// Add this route after the create-from-comparison route
router.post("/create-from-comparison-external/:rfqId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Create purchase order from quotation comparison for external vendor'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.createPOFromComparisonExternal(req, res);
});

router.post("/create-direct", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Create PO directly'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.createDirectPO(req, res);
});



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

router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Get all purchase orders 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' }
    #swagger.parameters['status'] = { in: 'query', type: 'string' }
  */
  purchaseOrderController.getAllPurchaseOrders(req, res);
});

router.get("/waiting", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Get approved PRs waiting for PO'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.getWaitingForPO(req, res);
});

router.get("/status/:status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Get purchase orders by status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.getPOsByStatus(req, res);
});

router.get("/by-number/:po_number", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Get purchase order by PO number'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.getPOByNumber(req, res);
});


  

router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Get purchase order by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.getPOById(req, res);
});

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

router.patch("/:id/status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Update purchase order status'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.updatePOStatus(req, res);
});

// Send PO email after full approval
router.post("/:poId/send-email", authenticate, purchaseOrderController.sendPOEmailOnFullApproval);

// POST /procurement/purchase-orders/create-from-comparison/:rfqId/:vendorId
router.post(
  "/purchase-orders/create-from-comparison/:rfqId/:vendorId",
  authenticate,
  purchaseOrderController.createPOFromComparison
);

router.put("/:id/items", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Update purchase order items'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.updatePOItems(req, res);
});

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

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Soft delete purchase order'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.deletePO(req, res);
});

router.delete("/hard/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Hard delete purchase order (permanent)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  purchaseOrderController.hardDeletePO(req, res);
});

router.get("/:id/download", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Purchase Order Postgre']
    #swagger.summary = 'Download purchase order as PDF'
    #swagger.description = 'Generates and downloads a PDF version of the purchase order'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Purchase Order ID',
      required: true,
      type: 'integer'
    }
    #swagger.responses[200] = {
      description: 'PDF file downloaded successfully',
      content: {
        'application/pdf': {
          schema: {
            type: 'string',
            format: 'binary'
          }
        }
      }
    }
    #swagger.responses[401] = {
      description: 'Unauthorized - Invalid or missing token'
    }
    #swagger.responses[404] = {
      description: 'Purchase Order not found'
    }
    #swagger.responses[500] = {
      description: 'Server error - Failed to generate PDF'
    }
  */
  purchaseOrderController.downloadPOPDF(req, res);
});





module.exports = router;