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)
);

// ========== CURRENCY RATES ==========
// Note: These routes are under /currencies/rates to maintain RESTful hierarchy
router.get('/currencies/rates', (req, res, next) => coreController.getCurrencyRates(req, res, next));
router.get('/currencies/rates/latest', (req, res, next) => coreController.getLatestCurrencyRate(req, res, next));
router.get('/currencies/rates/history', (req, res, next) => coreController.getHistoricalCurrencyRates(req, res, next));
router.post('/currencies/rates', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createCurrencyRate(req, res, next)
);
router.get('/currencies/convert', (req, res, next) => coreController.convertCurrency(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));
router.get('/countries/:id/states', (req, res, next) => coreController.getStatesByCountry(req, res, next));
router.post('/countries', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createCountry(req, res, next)
);
router.put('/countries/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateCountry(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.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)
);

// ========== 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));
router.get('/uom-categories/:id/reference', (req, res, next) => coreController.getUomCategoryReference(req, res, next));
router.post('/uom-categories', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.createUomCategory(req, res, next)
);
router.put('/uom-categories/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateUomCategory(req, res, next)
);
router.delete('/uom-categories/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.deleteUomCategory(req, res, next)
);

// ========== UOM ==========
router.get('/uom', (req, res, next) => coreController.getUoms(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.put('/uom/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  coreController.updateUom(req, res, next)
);
router.post('/uom/convert', (req, res, next) => coreController.convertUom(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)
);

export default router;
