import { Router } from 'express';
import { purchasesController } from './purchases.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

// All routes require authentication
router.use(authenticate);

// List purchase orders
router.get('/', requireRoles('admin', 'manager', 'warehouse', 'accountant', 'super_admin'), (req, res, next) =>
  purchasesController.findAll(req, res, next)
);

// Get purchase order by ID
router.get('/:id', requireRoles('admin', 'manager', 'warehouse', 'accountant', 'super_admin'), (req, res, next) =>
  purchasesController.findById(req, res, next)
);

// Create purchase order
router.post('/', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.create(req, res, next)
);

// Update purchase order
router.put('/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.update(req, res, next)
);

// Confirm purchase order
router.post('/:id/confirm', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.confirm(req, res, next)
);

// Cancel purchase order
router.post('/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.cancel(req, res, next)
);

// Delete purchase order
router.delete('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  purchasesController.delete(req, res, next)
);

// ========== RFQs (Request for Quotation) ==========
router.get('/rfqs', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.getRfqs(req, res, next)
);
router.get('/rfqs/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.getRfq(req, res, next)
);
router.post('/rfqs', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.createRfq(req, res, next)
);
router.put('/rfqs/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.updateRfq(req, res, next)
);
router.delete('/rfqs/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  purchasesController.deleteRfq(req, res, next)
);

// RFQ Lines
router.post('/rfqs/:id/lines', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.addRfqLine(req, res, next)
);
router.put('/rfqs/:id/lines/:lineId', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.updateRfqLine(req, res, next)
);
router.delete('/rfqs/:id/lines/:lineId', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  purchasesController.removeRfqLine(req, res, next)
);

// RFQ Workflow
router.post('/rfqs/:id/send', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.sendRfq(req, res, next)
);
router.post('/rfqs/:id/responded', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.markRfqResponded(req, res, next)
);
router.post('/rfqs/:id/accept', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.acceptRfq(req, res, next)
);
router.post('/rfqs/:id/reject', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.rejectRfq(req, res, next)
);
router.post('/rfqs/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  purchasesController.cancelRfq(req, res, next)
);

export default router;
