const express = require("express");
const router = express.Router();
const authenticate = require("../../middlewares/authenticate");
const stockController = require("../../controllers/production/stockConsumptionController");
const { routes } = require("../../app");

/**
 * @swagger
 * tags:
 *   name: Stock Management
 *   description: Stock consumption and inventory management
 */

/* ============================================================
   STOCK CONSUMPTION ROUTES
============================================================ */

// Update stock after consumption
router.put("/stock/consume/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Update stock after consumption'
    #swagger.description = 'Reduces stock when items are consumed'
    #swagger.parameters['itemId'] = { in: 'path', required: true, type: 'integer' }
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        quantity: 10,
        consumption_id: 1,
        shift_id: 1,
        remarks: "Morning shift consumption"
      }
    }
  */
  stockController.updateStockAfterConsumption(req, res);
});

// Add stock (purchase/restock)
router.put("/stock/add/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Add stock to inventory'
    #swagger.description = 'Increases stock when new items are purchased'
  */
  stockController.addStock(req, res);
});

// Get stock transactions for an item
router.get("/stock/transactions/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Get stock transaction history'
    #swagger.description = 'Returns all stock movements for a specific item'
  */
  stockController.getStockTransactions(req, res);
});

// Get stock summary for all items
router.get("/stock/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Get stock summary'
    #swagger.description = 'Returns current stock levels for all items'
  */
  stockController.getStockSummary(req, res);
});

// Update minimum stock level
router.put("/stock/minimum/:itemId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Update minimum stock level'
    #swagger.description = 'Sets the minimum stock threshold for alerts'
  */
  stockController.updateMinimumStock(req, res);
});

// Get low stock items
router.get("/stock/low-stock", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Get low stock items'
    #swagger.description = 'Returns items that are below minimum stock level'
  */
  stockController.getLowStockItems(req, res);
});

// Get stock alerts
router.get("/stock/alerts", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Get stock alerts'
    #swagger.description = 'Returns unresolved stock alerts'
  */
  stockController.getStockAlerts(req, res);
});

// Resolve stock alert
router.put("/stock/alerts/:alertId/resolve", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Resolve stock alert'
    #swagger.description = 'Marks a stock alert as resolved'
  */
  stockController.resolveStockAlert(req, res);
});

// Bulk update stock
router.post("/stock/bulk-update", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Bulk update stock'
    #swagger.description = 'Updates stock for multiple items at once'
  */
  stockController.bulkUpdateStock(req, res);
});

// Get stock movement report
router.get("/stock/movement-report", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Stock Management']
    #swagger.summary = 'Get stock movement report'
    #swagger.description = 'Returns stock movement report for date range'
  */
  stockController.getStockMovementReport(req, res);
});

module.exports = router; // Export as object for backward compatibility