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

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

router.post("/create", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Create GRN from vehicle exit'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.createGrn(req, res);
});

// ADD THIS NEW ROUTE FOR MANUAL GRN CREATION
router.post("/manual/create", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Create manual GRN without vehicle/PO'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        grn_number: "GRN-2024-MANUAL-001",
        grn_date: "2024-01-15",
        vendor_name: "ABC Suppliers",
        po_number: "PO-2024-001",
        invoice_no: "INV-2024-001",
        delivery_challan_no: "DC-2024-001",
        gate_entry_no: "GE-2024-001",
        remarks: "Manual GRN entry",
        items: [
          {
            item_name: "Product Name",
            ordered_qty: 100,
            received_qty: 100,
            accepted_qty: 95,
            rejected_qty: 5,
            unit_price: 10.50,
            rejected_unit_price: 5.00,
            store_location: "Main Store"
          }
        ]
      }
    }
  */
  grnController.createManualGrn(req, res);
});

// Add this route
router.put("/:grnId/details", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Update GRN details (PO, Invoice, DC, Gate Entry, Vehicle)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.updateGrnDetails(req, res);
});

router.post("/:grnId/items", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Add new item to GRN'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.addGrnItem(req, res);
});


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

router.get("/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Get all GRNs without pagination'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.getAllGrnsWithoutPagination(req, res);
});

router.get("/vehicle/:vehicleId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Get GRN by vehicle ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.getGrnByVehicleId(req, res);
});

router.get("/number/:grnNumber", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Get GRN by GRN number'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.getGrnByGrnNumber(req, res);
});

// In your procurement routes file
router.get("/by-po/:poId", authenticate, grnController.getGrnsByPoId);

// PDF DOWNLOAD ROUTE - MUST COME BEFORE THE GENERIC /:id ROUTE
router.get("/:id/download-pdf", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Download GRN as PDF'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  console.log("✅ PDF download route hit for ID:", req.params.id);
  grnController.downloadGrnPdf(req, res);
});

// MAIN GET ALL ROUTE
router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Get all GRNs 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' }
  */
  grnController.getAllGrns(req, res);
});

// GENERIC GET BY ID ROUTE - MUST BE LAST
router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Get GRN by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.getGrnById(req, res);
});

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

router.put("/:grnId/header", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Update GRN header'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.updateGrnHeader(req, res);
});

router.put("/:grnId/items/batch", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Update multiple GRN items (batch)'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.updateGrnItems(req, res);
});

router.put("/:grnId/items/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Update single GRN item'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.updateGrnItem(req, res);
});

router.put("/:grnId/complete", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Complete GRN'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.completeGrn(req, res);
});

// Approve GRN
router.put("/:id/approve", grnController.approveGrn);
// ==================== DELETE ====================

router.delete("/:grnId/items/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Delete GRN item'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.deleteGrnItem(req, res);
});

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



router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['GRN Postgre']
    #swagger.summary = 'Soft delete GRN'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  grnController.deleteGrn(req, res);
});
module.exports = router;