const express = require('express');
const router = express.Router();
const { authenticate } = require('../middlewares/auth');
const inventoryGoodsReceiptController = require('../controllers/inventoryGoodsReceiptController');
const inventoryGoodsIssueController = require('../controllers/inventoryGoodsIssueController');
const inventoryStockTransferController = require('../controllers/inventoryStockTransferController');
const inventoryStockAdjustmentController = require('../controllers/inventoryStockAdjustmentController');
const atpController = require('../controllers/atpController');
const stockLedgerController = require('../controllers/stockLedgerController');
const stockRegisterController = require('../controllers/stockRegisterController');
const stockRegisterPDFController = require('../controllers/stockRegisterPDFController');
const materialReorderLevelController = require('../controllers/materialReorderLevelController');
const batchSerialTrackingController = require('../controllers/batchSerialTrackingController');
const subcontractController = require('../controllers/subcontractController');
const subcontractDCPDFController = require('../controllers/subcontractDCPDFController');
const subcontractReceiptPDFController = require('../controllers/subcontractReceiptPDFController');
const generalDCController = require('../controllers/generalDCController');
const generalDCPDFController = require('../controllers/generalDCPDFController');
const generalReceiptPDFController = require('../controllers/generalReceiptPDFController');

// Goods Receipt Routes
router.get('/goodsreceipt', authenticate, inventoryGoodsReceiptController.getAll);
router.get('/goodsreceipt/:id', authenticate, inventoryGoodsReceiptController.getById);
router.post('/goodsreceipt', authenticate, inventoryGoodsReceiptController.create);
router.put('/goodsreceipt/:id', authenticate, inventoryGoodsReceiptController.update);
router.delete('/goodsreceipt/:id', authenticate, inventoryGoodsReceiptController.delete);
router.post('/goodsreceipt/:id/post', authenticate, inventoryGoodsReceiptController.post);

// Goods Issue Routes
router.get('/goodsissue', authenticate, inventoryGoodsIssueController.getAll);
router.get('/goodsissue/:id', authenticate, inventoryGoodsIssueController.getById);
router.post('/goodsissue', authenticate, inventoryGoodsIssueController.create);
router.put('/goodsissue/:id', authenticate, inventoryGoodsIssueController.update);
router.delete('/goodsissue/:id', authenticate, inventoryGoodsIssueController.delete);
router.post('/goodsissue/:id/post', authenticate, inventoryGoodsIssueController.post);

// Subcontract DC (Send Material)
router.get('/subcontract-dc', authenticate, subcontractController.getAllDC);
router.get('/subcontract-dc/vendor/:vendorId/address', authenticate, subcontractController.getVendorAddress);
router.get('/subcontract-dc/:id/pdf', authenticate, subcontractDCPDFController.generatePDF);
router.get('/subcontract-dc/:id', authenticate, subcontractController.getDCById);
router.get('/subcontract-dc/:id/pending-items', authenticate, subcontractController.getDCPendingItems);
router.post('/subcontract-dc', authenticate, subcontractController.createDC);
router.put('/subcontract-dc/:id', authenticate, subcontractController.updateDC);
router.post('/subcontract-dc/:id/approve', authenticate, subcontractController.approveDC);
router.post('/subcontract-dc/:id/send', authenticate, subcontractController.sendDC);

// Subcontract Receipt (Receive Processed Material)
router.get('/subcontract-receipt', authenticate, subcontractController.getAllReceipts);
router.get('/subcontract-receipt/:id/pdf', authenticate, subcontractReceiptPDFController.generatePDF);
router.get('/subcontract-receipt/:id', authenticate, subcontractController.getReceiptById);
router.post('/subcontract-receipt', authenticate, subcontractController.createReceipt);

// General DC (Machine)
router.get('/general-dc', authenticate, generalDCController.getAllDC);
router.get('/general-dc/machine/:machineId/details', authenticate, generalDCController.getMachineDetails);
router.get('/general-dc/vendor/:vendorId/address', authenticate, generalDCController.getVendorAddress);
router.get('/general-dc/:id/pdf', authenticate, generalDCPDFController.generatePDF);
router.get('/general-dc/:id', authenticate, generalDCController.getDCById);
router.get('/general-dc/:id/pending-items', authenticate, generalDCController.getDCPendingItems);
router.post('/general-dc', authenticate, generalDCController.createDC);
router.put('/general-dc/:id', authenticate, generalDCController.updateDC);
router.post('/general-dc/:id/approve', authenticate, generalDCController.approveDC);
router.post('/general-dc/:id/send', authenticate, generalDCController.sendDC);

// General Receipt (Machine)
router.get('/general-receipt', authenticate, generalDCController.getAllReceipts);
router.get('/general-receipt/:id/pdf', authenticate, generalReceiptPDFController.generatePDF);
router.get('/general-receipt/:id', authenticate, generalDCController.getReceiptById);
router.post('/general-receipt', authenticate, generalDCController.createReceipt);

// Stock Transfer Routes
router.get('/stocktransfer', authenticate, inventoryStockTransferController.getAll);
router.get('/stocktransfer/:id', authenticate, inventoryStockTransferController.getById);
router.post('/stocktransfer', authenticate, inventoryStockTransferController.create);
router.put('/stocktransfer/:id', authenticate, inventoryStockTransferController.update);
router.delete('/stocktransfer/:id', authenticate, inventoryStockTransferController.delete);
router.post('/stocktransfer/:id/post', authenticate, inventoryStockTransferController.post);

// Stock Adjustment Routes
router.get('/stockadjustment', authenticate, inventoryStockAdjustmentController.getAll);
router.get('/stockadjustment/:id', authenticate, inventoryStockAdjustmentController.getById);
router.post('/stockadjustment', authenticate, inventoryStockAdjustmentController.create);
router.put('/stockadjustment/:id', authenticate, inventoryStockAdjustmentController.update);
router.delete('/stockadjustment/:id', authenticate, inventoryStockAdjustmentController.delete);
router.post('/stockadjustment/:id/post', authenticate, inventoryStockAdjustmentController.post);

// ATP (Available to Promise) Routes
router.get('/atp/stock', authenticate, atpController.getStock);
router.post('/atp/check', authenticate, atpController.checkATP);

// Stock Ledger Routes
router.get('/stock-ledger', authenticate, stockLedgerController.getStockLedger);
router.get('/stock-ledger/balance', authenticate, stockLedgerController.getCurrentBalance);
router.post('/stock-ledger/balances', authenticate, stockLedgerController.getCurrentBalances);
router.get('/stock-ledger/summary', authenticate, stockLedgerController.getStockMovementSummary);

// Material Reorder Level Routes
router.get('/reorder-level', authenticate, materialReorderLevelController.getAll);
router.get('/reorder-level/:id', authenticate, materialReorderLevelController.getById);
router.post('/reorder-level', authenticate, materialReorderLevelController.create);
router.put('/reorder-level/:id', authenticate, materialReorderLevelController.update);
router.delete('/reorder-level/:id', authenticate, materialReorderLevelController.delete);
router.get('/reorder-level/alerts/low-stock', authenticate, materialReorderLevelController.getLowStockAlerts);

// Stock Register Routes
router.get('/stock-register', authenticate, stockRegisterController.getStockRegister);
router.get('/stock-register/material/:materialId', authenticate, stockRegisterController.getMaterialStockRegister);
router.get('/stock-register/material/:materialId/pdf', authenticate, stockRegisterPDFController.generatePDF);

// Batch & Serial Number Tracking Routes
router.get('/batch', authenticate, batchSerialTrackingController.getBatch);
router.post('/batch', authenticate, batchSerialTrackingController.createOrUpdateBatch);
router.get('/batch/material/:materialId', authenticate, batchSerialTrackingController.getBatchesForMaterial);
router.get('/batch/material/:materialId/available', authenticate, batchSerialTrackingController.getAvailableBatches);
router.get('/batch/expired', authenticate, batchSerialTrackingController.getExpiredBatches);

router.get('/serial/:serialNumber', authenticate, batchSerialTrackingController.getSerial);
router.post('/serial', authenticate, batchSerialTrackingController.createOrUpdateSerial);
router.put('/serial/:serialNumber/status', authenticate, batchSerialTrackingController.updateSerialStatus);
router.get('/serial/material/:materialId', authenticate, batchSerialTrackingController.getSerialsForMaterial);
router.get('/serial/material/:materialId/available', authenticate, batchSerialTrackingController.getAvailableSerials);
router.get('/serial/:serialNumber/traceability', authenticate, batchSerialTrackingController.getSerialTraceability);

module.exports = router;

