import { Router } from 'express';
import { crmController } from './crm.controller.js';
import { authenticate } from '../../shared/middleware/auth.middleware.js';
import { requireAccess } from '../../shared/middleware/rbac.middleware.js';

const router = Router();

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

// ========== LEADS ==========

router.get('/leads', (req, res, next) => crmController.getLeads(req, res, next));

router.get('/leads/:id', (req, res, next) => crmController.getLead(req, res, next));

router.post('/leads', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'leads:create' }), (req, res, next) =>
  crmController.createLead(req, res, next)
);

router.put('/leads/:id', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'leads:update' }), (req, res, next) =>
  crmController.updateLead(req, res, next)
);

router.post('/leads/:id/move', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'leads:update' }), (req, res, next) =>
  crmController.moveLeadStage(req, res, next)
);

router.post('/leads/:id/convert', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'leads:convert' }), (req, res, next) =>
  crmController.convertLead(req, res, next)
);

router.post('/leads/:id/lost', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'leads:update' }), (req, res, next) =>
  crmController.markLeadLost(req, res, next)
);

router.delete('/leads/:id', requireAccess({ roles: ['admin'], permission: 'leads:delete' }), (req, res, next) =>
  crmController.deleteLead(req, res, next)
);

// ========== OPPORTUNITIES ==========

router.get('/opportunities', (req, res, next) => crmController.getOpportunities(req, res, next));

router.get('/opportunities/:id', (req, res, next) => crmController.getOpportunity(req, res, next));

router.post('/opportunities', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'opportunities:create' }), (req, res, next) =>
  crmController.createOpportunity(req, res, next)
);

router.put('/opportunities/:id', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'opportunities:update' }), (req, res, next) =>
  crmController.updateOpportunity(req, res, next)
);

router.post('/opportunities/:id/move', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'opportunities:update' }), (req, res, next) =>
  crmController.moveOpportunityStage(req, res, next)
);

router.post('/opportunities/:id/won', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'opportunities:close' }), (req, res, next) =>
  crmController.markOpportunityWon(req, res, next)
);

router.post('/opportunities/:id/lost', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'opportunities:close' }), (req, res, next) =>
  crmController.markOpportunityLost(req, res, next)
);

router.post('/opportunities/:id/quote', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'quotations:create' }), (req, res, next) =>
  crmController.createOpportunityQuotation(req, res, next)
);

router.delete('/opportunities/:id', requireAccess({ roles: ['admin'], permission: 'opportunities:delete' }), (req, res, next) =>
  crmController.deleteOpportunity(req, res, next)
);

// ========== PIPELINE ==========

router.get('/pipeline', (req, res, next) => crmController.getPipeline(req, res, next));

// ========== LEAD STAGES ==========

router.get('/lead-stages', (req, res, next) => crmController.getLeadStages(req, res, next));

router.post('/lead-stages', requireAccess({ roles: ['admin'], permission: 'leads:create' }), (req, res, next) =>
  crmController.createLeadStage(req, res, next)
);

router.put('/lead-stages/:id', requireAccess({ roles: ['admin'], permission: 'leads:update' }), (req, res, next) =>
  crmController.updateLeadStage(req, res, next)
);

router.delete('/lead-stages/:id', requireAccess({ roles: ['admin'], permission: 'leads:delete' }), (req, res, next) =>
  crmController.deleteLeadStage(req, res, next)
);

// ========== OPPORTUNITY STAGES ==========

router.get('/opportunity-stages', (req, res, next) => crmController.getOpportunityStages(req, res, next));

router.post('/opportunity-stages', requireAccess({ roles: ['admin'], permission: 'opportunities:create' }), (req, res, next) =>
  crmController.createOpportunityStage(req, res, next)
);

router.put('/opportunity-stages/:id', requireAccess({ roles: ['admin'], permission: 'opportunities:update' }), (req, res, next) =>
  crmController.updateOpportunityStage(req, res, next)
);

router.delete('/opportunity-stages/:id', requireAccess({ roles: ['admin'], permission: 'opportunities:delete' }), (req, res, next) =>
  crmController.deleteOpportunityStage(req, res, next)
);

// ========== LOST REASONS ==========

router.get('/lost-reasons', (req, res, next) => crmController.getLostReasons(req, res, next));

router.post('/lost-reasons', requireAccess({ roles: ['admin'], permission: 'leads:create' }), (req, res, next) =>
  crmController.createLostReason(req, res, next)
);

router.put('/lost-reasons/:id', requireAccess({ roles: ['admin'], permission: 'leads:update' }), (req, res, next) =>
  crmController.updateLostReason(req, res, next)
);

router.delete('/lost-reasons/:id', requireAccess({ roles: ['admin'], permission: 'leads:delete' }), (req, res, next) =>
  crmController.deleteLostReason(req, res, next)
);

export default router;
