import { Router } from 'express';
import { partnersController } from './partners.controller.js';
import { rankingController } from './ranking.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);

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

// Calculate rankings (admin, manager)
router.post('/rankings/calculate', requireAccess({ roles: ['admin', 'manager'], permission: 'partners:update' }), (req, res, next) =>
  rankingController.calculateRankings(req, res, next)
);

// Get all rankings
router.get('/rankings', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'partners:read' }), (req, res, next) =>
  rankingController.findRankings(req, res, next)
);

// Top partners
router.get('/rankings/top/customers', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'customers:read' }), (req, res, next) =>
  rankingController.getTopCustomers(req, res, next)
);
router.get('/rankings/top/suppliers', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'suppliers:read' }), (req, res, next) =>
  rankingController.getTopSuppliers(req, res, next)
);

// ABC distribution
router.get('/rankings/abc/customers', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'customers:read' }), (req, res, next) =>
  rankingController.getCustomerABCDistribution(req, res, next)
);
router.get('/rankings/abc/suppliers', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'suppliers:read' }), (req, res, next) =>
  rankingController.getSupplierABCDistribution(req, res, next)
);

// Partners by ABC
router.get('/rankings/abc/customers/:abc', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'customers:read' }), (req, res, next) =>
  rankingController.getCustomersByABC(req, res, next)
);
router.get('/rankings/abc/suppliers/:abc', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'suppliers:read' }), (req, res, next) =>
  rankingController.getSuppliersByABC(req, res, next)
);

// Partner-specific ranking
router.get('/rankings/partner/:partnerId', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'partners:read' }), (req, res, next) =>
  rankingController.findPartnerRanking(req, res, next)
);
router.get('/rankings/partner/:partnerId/history', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'partners:read' }), (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('/', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'partners:read' }), (req, res, next) =>
  partnersController.findAll(req, res, next)
);

// Get partner by ID
router.get('/:id', requireAccess({ roles: ['admin', 'manager', 'sales', 'accountant'], permission: 'partners:read' }), (req, res, next) =>
  partnersController.findById(req, res, next)
);

// Create partner (admin, manager, sales)
router.post('/', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'partners:create' }), (req, res, next) =>
  partnersController.create(req, res, next)
);

// Update partner (admin, manager, sales)
router.put('/:id', requireAccess({ roles: ['admin', 'manager', 'sales'], permission: 'partners:update' }), (req, res, next) =>
  partnersController.update(req, res, next)
);

// Delete partner (admin only)
router.delete('/:id', requireAccess({ roles: ['admin'], permission: 'partners:delete' }), (req, res, next) =>
  partnersController.delete(req, res, next)
);

export default router;
