// dispatchRoutes.js
const express = require("express");
const router = express.Router();
const dispatchController = require("../../controllers/dispatch/dispatchController");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Dispatch Management
 *   description: Dispatch management APIs
 */

router.get("/dashboard-products", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get products for dispatch dashboard'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.responses[200] = { description: "Dashboard products retrieved" }
  */
  dispatchController.getDispatchDashboardProducts(req, res);
});


router.get("/allocations", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get product dispatch allocations'
    #swagger.parameters['productId'] = { in: 'query', type: 'integer' }
    #swagger.parameters['status'] = { in: 'query', type: 'string' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getProductDispatchAllocations(req, res);
});

router.get("/allocations/:productId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get allocations for specific product'
    #swagger.parameters['productId'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getProductDispatchAllocations(req, res);
});

router.get("/ready-dispatch", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get items ready for dispatch'
    #swagger.parameters['warehouse_id'] = { in: 'query', type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getReadyForDispatch(req, res);
});

router.post("/run-auto-allocation", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Run automatic stock allocation'
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["order_ids"], properties: { order_ids: { type: "array", items: { type: "integer" } }, allocation_strategy: { type: "string", enum: ["FIFO", "priority"] } } } } } }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.runAutoAllocation(req, res);
});

router.post("/create-shipment", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Create a new shipment'
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["allocation_ids", "shipping_address"], properties: { allocation_ids: { type: "array", items: { type: "integer" } }, shipping_address: { type: "string" }, carrier: { type: "string" } } } } } }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.createShipment(req, res);
});

router.post("/manual-allocation", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Manually allocate stock'
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["order_id", "product_id", "quantity"], properties: { order_id: { type: "integer" }, product_id: { type: "integer" }, quantity: { type: "integer" } } } } } }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.manualAllocateStock(req, res);
});

router.post("/cancel-allocation", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Cancel an allocation'
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["allocation_id"], properties: { allocation_id: { type: "integer" }, reason: { type: "string" } } } } } }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.cancelAllocation(req, res);
});

router.get("/allocation-details/:allocationId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get allocation details'
    #swagger.parameters['allocationId'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getAllocationDetails(req, res);
});

router.get("/dispatched", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get dispatched shipments'
    #swagger.parameters['status'] = { in: 'query', type: 'string', enum: ['pending', 'in_transit', 'delivered'] }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getDispatchedShipments(req, res);
});

router.get("/shipment-details/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get shipment by ID'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getShipmentById(req, res);
});

router.put("/:id/approve", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Approve shipment'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.approveShipment(req, res);
});

router.get("/shipment-by/:salesOrderId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Dispatch Management']
    #swagger.summary = 'Get shipment by sales order ID'
    #swagger.parameters['salesOrderId'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  dispatchController.getShipmentBySalesOrderId(req, res);
});

module.exports = router;