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

const router = Router();

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

// ========== CURRENCIES ==========
router.get('/currencies', (req, res, next) => coreController.getCurrencies(req, res, next));
router.get('/currencies/:id', (req, res, next) => coreController.getCurrency(req, res, next));
router.post('/currencies', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createCurrency(req, res, next)
);
router.put('/currencies/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateCurrency(req, res, next)
);

// ========== COUNTRIES ==========
router.get('/countries', (req, res, next) => coreController.getCountries(req, res, next));
router.get('/countries/:id', (req, res, next) => coreController.getCountry(req, res, next));

// ========== STATES ==========
router.get('/states', (req, res, next) => coreController.getStates(req, res, next));
router.get('/states/:id', (req, res, next) => coreController.getState(req, res, next));
router.get('/countries/:countryId/states', (req, res, next) => coreController.getStatesByCountry(req, res, next));
router.get('/countries/code/:countryCode/states', (req, res, next) => coreController.getStatesByCountryCode(req, res, next));
router.post('/states', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createState(req, res, next)
);
router.put('/states/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateState(req, res, next)
);
router.delete('/states/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deleteState(req, res, next)
);

// ========== CURRENCY RATES ==========
router.get('/currency-rates', (req, res, next) => coreController.getCurrencyRates(req, res, next));
router.get('/currency-rates/latest', (req, res, next) => coreController.getLatestRates(req, res, next));
router.get('/currency-rates/rate/:from/:to', (req, res, next) => coreController.getLatestRate(req, res, next));
router.get('/currency-rates/history/:from/:to', (req, res, next) => coreController.getCurrencyRateHistory(req, res, next));
router.get('/currency-rates/:id', (req, res, next) => coreController.getCurrencyRate(req, res, next));
router.post('/currency-rates', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.createCurrencyRate(req, res, next)
);
router.post('/currency-rates/convert', (req, res, next) => coreController.convertCurrency(req, res, next));
router.delete('/currency-rates/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deleteCurrencyRate(req, res, next)
);

// ========== UOM CATEGORIES ==========
router.get('/uom-categories', (req, res, next) => coreController.getUomCategories(req, res, next));
router.get('/uom-categories/:id', (req, res, next) => coreController.getUomCategory(req, res, next));

// ========== UOM ==========
router.get('/uom', (req, res, next) => coreController.getUoms(req, res, next));
router.get('/uom/by-code/:code', (req, res, next) => coreController.getUomByCode(req, res, next));
router.get('/uom/:id', (req, res, next) => coreController.getUom(req, res, next));
router.post('/uom', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createUom(req, res, next)
);
router.post('/uom/convert', (req, res, next) => coreController.convertUom(req, res, next));
router.get('/uom-categories/:categoryId/conversions', (req, res, next) =>
  coreController.getUomConversions(req, res, next)
);
router.put('/uom/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateUom(req, res, next)
);

// ========== PRODUCT CATEGORIES ==========
router.get('/product-categories', (req, res, next) => coreController.getProductCategories(req, res, next));
router.get('/product-categories/:id', (req, res, next) => coreController.getProductCategory(req, res, next));
router.post('/product-categories', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.createProductCategory(req, res, next)
);
router.put('/product-categories/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.updateProductCategory(req, res, next)
);
router.delete('/product-categories/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deleteProductCategory(req, res, next)
);

// ========== PAYMENT TERMS ==========
router.get('/payment-terms', (req, res, next) => coreController.getPaymentTerms(req, res, next));
router.get('/payment-terms/standard', (req, res, next) => coreController.getStandardPaymentTerms(req, res, next));
router.get('/payment-terms/:id', (req, res, next) => coreController.getPaymentTerm(req, res, next));
router.post('/payment-terms', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.createPaymentTerm(req, res, next)
);
router.post('/payment-terms/initialize', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.initializePaymentTerms(req, res, next)
);
router.post('/payment-terms/:id/calculate-due-date', (req, res, next) =>
  coreController.calculateDueDate(req, res, next)
);
router.put('/payment-terms/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.updatePaymentTerm(req, res, next)
);
router.delete('/payment-terms/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deletePaymentTerm(req, res, next)
);

// ========== DISCOUNT RULES ==========
router.get('/discount-rules', (req, res, next) => coreController.getDiscountRules(req, res, next));
router.get('/discount-rules/by-product/:productId', (req, res, next) =>
  coreController.getDiscountRulesByProduct(req, res, next)
);
router.get('/discount-rules/by-customer/:customerId', (req, res, next) =>
  coreController.getDiscountRulesByCustomer(req, res, next)
);
router.get('/discount-rules/:id', (req, res, next) => coreController.getDiscountRule(req, res, next));
router.post('/discount-rules', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.createDiscountRule(req, res, next)
);
router.post('/discount-rules/apply', (req, res, next) => coreController.applyDiscounts(req, res, next));
router.put('/discount-rules/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  coreController.updateDiscountRule(req, res, next)
);
router.post('/discount-rules/:id/reset-usage', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.resetDiscountRuleUsage(req, res, next)
);
router.delete('/discount-rules/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deleteDiscountRule(req, res, next)
);

export default router;
