import { Router } from 'express';
import { partnersController } from './partners.controller.js';
import { rankingController } from './ranking.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

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

// ============================================================================
// RANKING ROUTES (must be before /:id routes to avoid conflicts)
// ============================================================================

// Calculate rankings (admin, manager)
router.post('/rankings/calculate', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  rankingController.calculateRankings(req, res, next)
);

// Get all rankings
router.get('/rankings', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.findRankings(req, res, next)
);

// Top partners
router.get('/rankings/top/customers', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getTopCustomers(req, res, next)
);
router.get('/rankings/top/suppliers', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getTopSuppliers(req, res, next)
);

// ABC distribution
router.get('/rankings/abc/customers', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getCustomerABCDistribution(req, res, next)
);
router.get('/rankings/abc/suppliers', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getSupplierABCDistribution(req, res, next)
);

// Partners by ABC
router.get('/rankings/abc/customers/:abc', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getCustomersByABC(req, res, next)
);
router.get('/rankings/abc/suppliers/:abc', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getSuppliersByABC(req, res, next)
);

// Partner-specific ranking
router.get('/rankings/partner/:partnerId', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.findPartnerRanking(req, res, next)
);
router.get('/rankings/partner/:partnerId/history', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  rankingController.getPartnerHistory(req, res, next)
);

// ============================================================================
// PARTNER ROUTES
// ============================================================================

// Convenience endpoints for customers and suppliers
router.get('/customers', (req, res, next) => partnersController.findCustomers(req, res, next));
router.get('/suppliers', (req, res, next) => partnersController.findSuppliers(req, res, next));

// List all partners (admin, manager, sales, accountant)
router.get('/', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  partnersController.findAll(req, res, next)
);

// Get partner by ID
router.get('/:id', requireRoles('admin', 'manager', 'sales', 'accountant', 'super_admin'), (req, res, next) =>
  partnersController.findById(req, res, next)
);

// Create partner (admin, manager, sales)
router.post('/', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  partnersController.create(req, res, next)
);

// Update partner (admin, manager, sales)
router.put('/:id', requireRoles('admin', 'manager', 'sales', 'super_admin'), (req, res, next) =>
  partnersController.update(req, res, next)
);

// Delete partner (admin only)
router.delete('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  partnersController.delete(req, res, next)
);

export default router;
