const express = require('express');
const router = express.Router();
const gateEntryRoutes = require('./gateEntryRoutes');
const vendorQuotationRequestRoutes = require('./vendorQuotationRequestRoutes');
const { authenticate } = require('../middlewares/auth');
const purchaseRequisitionController = require('../controllers/purchaseRequisitionController');
const purchaseRequisitionPDFController = require('../controllers/purchaseRequisitionPDFController');
const requestForQuotationController = require('../controllers/requestForQuotationController');
const purchaseOrderController = require('../controllers/purchaseOrderController');
const goodsReceiptNoteController = require('../controllers/goodsReceiptNoteController');
const vendorInvoiceController = require('../controllers/vendorInvoiceController');
const vendorPaymentController = require('../controllers/vendorPaymentController');
const vendorPaymentPDFController = require('../controllers/vendorPaymentPDFController');
const purchaseReturnController = require('../controllers/purchaseReturnController');

// Purchase Requisition Routes
router.get('/purchaserequisition', authenticate, purchaseRequisitionController.getAll);
router.get('/purchaserequisition/:id/pdf', authenticate, purchaseRequisitionPDFController.generatePDF);
router.get('/purchaserequisition/:id', authenticate, purchaseRequisitionController.getById);
router.post('/purchaserequisition', authenticate, purchaseRequisitionController.create);
router.put('/purchaserequisition/:id', authenticate, purchaseRequisitionController.update);
router.delete('/purchaserequisition/:id', authenticate, purchaseRequisitionController.delete);
router.post('/purchaserequisition/:id/submit', authenticate, purchaseRequisitionController.submit);
router.post('/purchaserequisition/:id/approve', authenticate, purchaseRequisitionController.approve);
router.post('/purchaserequisition/:id/reject', authenticate, purchaseRequisitionController.reject);

// Request for Quotation Routes
router.get('/requestforquotation', authenticate, requestForQuotationController.getAll);
router.get('/requestforquotation/:id', authenticate, requestForQuotationController.getById);
router.post('/requestforquotation', authenticate, requestForQuotationController.create);
router.put('/requestforquotation/:id', authenticate, requestForQuotationController.update);
router.delete('/requestforquotation/:id', authenticate, requestForQuotationController.delete);
router.post('/requestforquotation/:id/submit', authenticate, requestForQuotationController.submit);
router.post('/requestforquotation/:id/close', authenticate, requestForQuotationController.close);

// Purchase Order Routes
router.get('/purchaseorder', authenticate, purchaseOrderController.getAll);
router.get('/purchaseorder/costing', authenticate, purchaseOrderController.getPurchaseCosting);
router.get('/purchaseorder/costing/:id', authenticate, purchaseOrderController.getPurchaseCostingById);
router.patch('/purchaseorder/costing/:id/approval', authenticate, purchaseOrderController.updatePurchaseCostingApproval);
router.get('/purchaseorder/material/:materialId/last-rate', authenticate, purchaseOrderController.getLastPurchaseRate);
router.get('/purchaseorder/material/:materialId/vendor-comparison', authenticate, purchaseOrderController.getMaterialVendorComparison);
router.get('/purchaseorder/material/:materialId/trend', authenticate, purchaseOrderController.getMaterialPriceTrend);
router.get('/purchaseorder/:id/pdf', authenticate, purchaseOrderController.generatePDF);
router.get('/purchaseorder/:id', authenticate, purchaseOrderController.getById);
router.post('/purchaseorder', authenticate, purchaseOrderController.create);
router.put('/purchaseorder/:id', authenticate, purchaseOrderController.update);
router.delete('/purchaseorder/:id', authenticate, purchaseOrderController.delete);
router.post('/purchaseorder/:id/submit', authenticate, purchaseOrderController.submit);
router.post('/purchaseorder/:id/approve', authenticate, purchaseOrderController.approve);
router.post('/purchaseorder/:id/post', authenticate, purchaseOrderController.post);
router.patch('/purchaseorder/:id/edit-rate', authenticate, purchaseOrderController.editRate);
router.post('/purchaseorder/:id/send-to-vendor', authenticate, purchaseOrderController.sendToVendor);
router.post('/purchaseorder/:id/send-email', authenticate, purchaseOrderController.sendEmailToVendor);
router.post('/purchaseorder/:id/send-whatsapp', authenticate, purchaseOrderController.sendWhatsAppToVendor);
router.post('/purchaseorder/:id/create-gate-entry', authenticate, purchaseOrderController.createGateEntry);
router.get('/notification-settings', authenticate, purchaseOrderController.getNotificationSettings);
router.put('/notification-settings', authenticate, purchaseOrderController.updateNotificationSettings);

// Goods Receipt Note Routes
router.get('/goodsreceiptnote', authenticate, goodsReceiptNoteController.getAll);
router.get('/goodsreceiptnote/:id/pdf', authenticate, goodsReceiptNoteController.generatePDF);
router.get('/goodsreceiptnote/:id', authenticate, goodsReceiptNoteController.getById);
router.post('/goodsreceiptnote', authenticate, goodsReceiptNoteController.create);
router.put('/goodsreceiptnote/:id', authenticate, goodsReceiptNoteController.update);
router.delete('/goodsreceiptnote/:id', authenticate, goodsReceiptNoteController.delete);
router.post('/goodsreceiptnote/:id/post', authenticate, goodsReceiptNoteController.post);

// Vendor Invoice Routes
router.get('/vendorinvoice', authenticate, vendorInvoiceController.getAll);
router.get('/vendorinvoice/:id', authenticate, vendorInvoiceController.getById);
router.post('/vendorinvoice', authenticate, vendorInvoiceController.create);
router.put('/vendorinvoice/:id', authenticate, vendorInvoiceController.update);
router.delete('/vendorinvoice/:id', authenticate, vendorInvoiceController.delete);
router.post('/vendorinvoice/:id/submit', authenticate, vendorInvoiceController.submit);
router.post('/vendorinvoice/:id/approve', authenticate, vendorInvoiceController.approve);
router.post('/vendorinvoice/:id/post', authenticate, vendorInvoiceController.post);

// Vendor Payment Routes
router.get('/vendor-payment', authenticate, vendorPaymentController.getAll);
router.get('/vendor-payment/invoice-options', authenticate, vendorPaymentController.getInvoiceOptions);
router.get('/vendor-payment/:id/pdf', authenticate, vendorPaymentPDFController.generatePDF);
router.get('/vendor-payment/:id', authenticate, vendorPaymentController.getById);
router.post('/vendor-payment', authenticate, vendorPaymentController.create);
router.put('/vendor-payment/:id', authenticate, vendorPaymentController.update);
router.patch('/vendor-payment/:id/status', authenticate, vendorPaymentController.updateStatus);
router.delete('/vendor-payment/:id', authenticate, vendorPaymentController.delete);

// Purchase Return Routes
router.get('/purchase-return', authenticate, purchaseReturnController.getAll);
router.get('/purchase-return/grn-options', authenticate, purchaseReturnController.getGRNOptions);
router.get('/purchase-return/grn/:grnId/prefill', authenticate, purchaseReturnController.getGRNPrefill);
router.get('/purchase-return/:id/pdf', authenticate, purchaseReturnController.generatePDF);
router.get('/purchase-return/:id', authenticate, purchaseReturnController.getById);
router.post('/purchase-return', authenticate, purchaseReturnController.create);
router.put('/purchase-return/:id', authenticate, purchaseReturnController.update);
router.delete('/purchase-return/:id', authenticate, purchaseReturnController.delete);
router.post('/purchase-return/:id/approve', authenticate, purchaseReturnController.approve);

// Gate Entry Routes
router.use('/gateentry', gateEntryRoutes);

// Vendor Quotation Request Routes
router.use('/vendor-quotation-request', vendorQuotationRequestRoutes);

module.exports = router;

