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

const router = Router();

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

// ========== PRICELISTS ==========
router.get('/pricelists', (req, res, next) => salesController.getPricelists(req, res, next));

router.get('/pricelists/:id', (req, res, next) => salesController.getPricelist(req, res, next));

router.post('/pricelists', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.createPricelist(req, res, next)
);

router.put('/pricelists/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.updatePricelist(req, res, next)
);

router.post('/pricelists/:id/items', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.addPricelistItem(req, res, next)
);

router.delete('/pricelists/:id/items/:itemId', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.removePricelistItem(req, res, next)
);

// ========== SALES TEAMS ==========
router.get('/teams', (req, res, next) => salesController.getSalesTeams(req, res, next));

router.get('/teams/:id', (req, res, next) => salesController.getSalesTeam(req, res, next));

router.post('/teams', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.createSalesTeam(req, res, next)
);

router.put('/teams/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.updateSalesTeam(req, res, next)
);

router.post('/teams/:id/members', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.addSalesTeamMember(req, res, next)
);

router.delete('/teams/:id/members/:memberId', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.removeSalesTeamMember(req, res, next)
);

// ========== CUSTOMER GROUPS ==========
router.get('/customer-groups', (req, res, next) => salesController.getCustomerGroups(req, res, next));

router.get('/customer-groups/:id', (req, res, next) => salesController.getCustomerGroup(req, res, next));

router.post('/customer-groups', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.createCustomerGroup(req, res, next)
);

router.put('/customer-groups/:id', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.updateCustomerGroup(req, res, next)
);

router.delete('/customer-groups/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  salesController.deleteCustomerGroup(req, res, next)
);

router.post('/customer-groups/:id/members', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.addCustomerGroupMember(req, res, next)
);

router.delete('/customer-groups/:id/members/:memberId', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.removeCustomerGroupMember(req, res, next)
);

// ========== QUOTATIONS ==========
router.get('/quotations', (req, res, next) => salesController.getQuotations(req, res, next));

router.get('/quotations/:id', (req, res, next) => salesController.getQuotation(req, res, next));

router.post('/quotations', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.createQuotation(req, res, next)
);

router.put('/quotations/:id', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.updateQuotation(req, res, next)
);

router.delete('/quotations/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.deleteQuotation(req, res, next)
);

router.post('/quotations/:id/lines', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.addQuotationLine(req, res, next)
);

router.put('/quotations/:id/lines/:lineId', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.updateQuotationLine(req, res, next)
);

router.delete('/quotations/:id/lines/:lineId', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.removeQuotationLine(req, res, next)
);

router.post('/quotations/:id/send', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.sendQuotation(req, res, next)
);

router.post('/quotations/:id/confirm', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.confirmQuotation(req, res, next)
);

router.post('/quotations/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.cancelQuotation(req, res, next)
);

// ========== SALES ORDERS ==========
router.get('/orders', (req, res, next) => salesController.getOrders(req, res, next));

router.get('/orders/:id', (req, res, next) => salesController.getOrder(req, res, next));

router.post('/orders', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.createOrder(req, res, next)
);

router.put('/orders/:id', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.updateOrder(req, res, next)
);

router.delete('/orders/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.deleteOrder(req, res, next)
);

router.post('/orders/:id/lines', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.addOrderLine(req, res, next)
);

router.put('/orders/:id/lines/:lineId', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.updateOrderLine(req, res, next)
);

router.delete('/orders/:id/lines/:lineId', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  salesController.removeOrderLine(req, res, next)
);

router.post('/orders/:id/confirm', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.confirmOrder(req, res, next)
);

router.post('/orders/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  salesController.cancelOrder(req, res, next)
);

router.post('/orders/:id/invoice', requireRoles('admin', 'manager', 'accountant', 'super_admin'), (req, res, next) =>
  salesController.createOrderInvoice(req, res, next)
);

export default router;
