const express = require('express');
const router = express.Router();
const path = require('path');
const fs = require('fs');
const multer = require('multer');
const { authenticate } = require('../middlewares/auth');

const salesQuotationController = require('../controllers/salesQuotationController');
const salesOrderController = require('../controllers/salesOrderController');
const deliveryController = require('../controllers/deliveryController');
const salesInvoiceController = require('../controllers/salesInvoiceController');
const salesInvoicePDFController = require('../controllers/salesInvoicePDFController');
const salesReturnController = require('../controllers/salesReturnController');
const deliveryChallanPDFController = require('../controllers/deliveryChallanPDFController');
const receiptEntryPDFController = require('../controllers/receiptEntryPDFController');
const deliveryChallanController = require('../controllers/deliveryChallanController');
const receiptEntryController = require('../controllers/receiptEntryController');

const deliveryProofDir = path.join(__dirname, '../uploads/delivery-proof');
if (!fs.existsSync(deliveryProofDir)) {
  fs.mkdirSync(deliveryProofDir, { recursive: true });
}
const deliveryProofUpload = multer({
  storage: multer.diskStorage({
    destination: (req, file, cb) => cb(null, deliveryProofDir),
    filename: (req, file, cb) => {
      const ext = path.extname(file.originalname || '');
      const base = path.basename(file.originalname || 'proof', ext).replace(/[^a-zA-Z0-9-_]/g, '_');
      cb(null, `${base}_${Date.now()}${ext}`);
    }
  }),
  limits: { fileSize: 10 * 1024 * 1024 }
});

router.get('/quotation', authenticate, salesQuotationController.getAll);
router.get('/quotation/:id', authenticate, salesQuotationController.getById);
router.post('/quotation', authenticate, salesQuotationController.create);
router.put('/quotation/:id', authenticate, salesQuotationController.update);
router.delete('/quotation/:id', authenticate, salesQuotationController.delete);
router.patch('/quotation/:id/status', authenticate, salesQuotationController.updateStatus);

router.get('/order', authenticate, salesOrderController.getAll);
router.get('/order/:id', authenticate, salesOrderController.getById);
router.post('/order', authenticate, salesOrderController.create);
router.put('/order/:id', authenticate, salesOrderController.update);
router.delete('/order/:id', authenticate, salesOrderController.delete);
router.patch('/order/:id/status', authenticate, salesOrderController.updateStatus);
router.post('/order/:id/submit', authenticate, salesOrderController.submit);
router.post('/order/:id/approve', authenticate, salesOrderController.approve);
router.post('/order/:id/release', authenticate, salesOrderController.release);
router.post('/order/:id/create-customer-po', authenticate, salesOrderController.createCustomerPO);
router.post('/order/:id/link-customer-po', authenticate, salesOrderController.linkCustomerPO);
router.get('/order/:id/customer-po', authenticate, salesOrderController.getCustomerPO);

router.get('/delivery', authenticate, deliveryController.getAll);
router.get('/delivery/:id', authenticate, deliveryController.getById);
router.post('/delivery', authenticate, deliveryController.create);
router.post('/delivery/create-from-customer-po/:customerPOId', authenticate, deliveryController.createFromCustomerPO);
router.put('/delivery/:id', authenticate, deliveryController.update);
router.delete('/delivery/:id', authenticate, deliveryController.delete);
router.patch('/delivery/:id/status', authenticate, deliveryController.updateStatus);

router.get('/delivery-challan', authenticate, deliveryChallanController.getAll);
router.get('/delivery-challan/dispatch-options', authenticate, deliveryChallanController.getDispatchOptions);
router.get('/delivery-challan/prefill/:dispatchId', authenticate, deliveryChallanController.getPrefill);
router.get('/delivery-challan/:id/pdf', authenticate, deliveryChallanPDFController.generatePDF);
router.get('/delivery-challan/:id', authenticate, deliveryChallanController.getById);
router.post('/delivery-challan', authenticate, deliveryChallanController.create);
router.put('/delivery-challan/:id', authenticate, deliveryChallanController.update);
router.patch('/delivery-challan/:id/mark-delivered', authenticate, deliveryProofUpload.single('delivery_proof'), deliveryChallanController.markDelivered);
router.patch('/delivery-challan/:id/status', authenticate, deliveryChallanController.updateStatus);

router.get('/receipt-entry', authenticate, receiptEntryController.getAll);
router.get('/receipt-entry/customer-options', authenticate, receiptEntryController.getCustomerOptions);
router.get('/receipt-entry/challan-options', authenticate, receiptEntryController.getDeliveryChallanOptions);
router.get('/receipt-entry/invoice-options', authenticate, receiptEntryController.getInvoiceOptions);
router.get('/receipt-entry/:id/pdf', authenticate, receiptEntryPDFController.generatePDF);
router.get('/receipt-entry/:id', authenticate, receiptEntryController.getById);
router.post('/receipt-entry', authenticate, receiptEntryController.create);
router.put('/receipt-entry/:id', authenticate, receiptEntryController.update);
router.patch('/receipt-entry/:id/status', authenticate, receiptEntryController.updateStatus);
router.delete('/receipt-entry/:id', authenticate, receiptEntryController.delete);

router.get('/invoice', authenticate, salesInvoiceController.getAll);
router.get('/invoice/available-stock', authenticate, salesInvoiceController.getAvailableStockForInvoiceItem);
router.get('/invoice/customer-po-options', authenticate, salesInvoiceController.getEligibleCustomerPOs);
router.get('/invoice/customer-po-prefill/:customerPOId', authenticate, salesInvoiceController.getCustomerPOPrefill);
router.get('/invoice/:id/pdf', authenticate, salesInvoicePDFController.generatePDF);
router.get('/invoice/:id', authenticate, salesInvoiceController.getById);
router.post('/invoice', authenticate, salesInvoiceController.create);
router.put('/invoice/:id', authenticate, salesInvoiceController.update);
router.delete('/invoice/:id', authenticate, salesInvoiceController.delete);
router.patch('/invoice/:id/status', authenticate, salesInvoiceController.updateStatus);

router.get('/sales-return', authenticate, salesReturnController.getAll);
router.get('/sales-return/invoice-options', authenticate, salesReturnController.getInvoiceOptions);
router.get('/sales-return/invoice/:invoiceId/prefill', authenticate, salesReturnController.getInvoicePrefill);
router.get('/sales-return/:id', authenticate, salesReturnController.getById);
router.post('/sales-return', authenticate, salesReturnController.create);
router.put('/sales-return/:id', authenticate, salesReturnController.update);
router.delete('/sales-return/:id', authenticate, salesReturnController.delete);
router.post('/sales-return/:id/approve', authenticate, salesReturnController.approve);

module.exports = router;

