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

const router = Router();

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

// ========== CATEGORIES (must be before :id routes) ==========
// List categories
router.get('/categories', (req, res, next) => productsController.findAllCategories(req, res, next));

// Get category by ID
router.get('/categories/:id', (req, res, next) => productsController.findCategoryById(req, res, next));

// Create category
router.post('/categories', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  productsController.createCategory(req, res, next)
);

// Update category
router.patch('/categories/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  productsController.updateCategory(req, res, next)
);

// Delete category
router.delete('/categories/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  productsController.deleteCategory(req, res, next)
);

// ========== CONVENIENCE ROUTES ==========
// Get sellable products
router.get('/sellable', (req, res, next) => productsController.getSellable(req, res, next));

// Get purchasable products
router.get('/purchasable', (req, res, next) => productsController.getPurchasable(req, res, next));

// Get product by SKU
router.get('/sku/:sku', (req, res, next) => productsController.findBySku(req, res, next));

// Get product by Barcode
router.get('/barcode/:barcode', (req, res, next) => productsController.findByBarcode(req, res, next));

// ========== PRODUCTS ==========
// List products
router.get('/', (req, res, next) => productsController.findAll(req, res, next));

// Get product by ID
router.get('/:id', (req, res, next) => productsController.findById(req, res, next));

// Create product
router.post('/', requireRoles('admin', 'manager', 'inventory', 'super_admin'), (req, res, next) =>
  productsController.create(req, res, next)
);

// Update product
router.patch('/:id', requireRoles('admin', 'manager', 'inventory', 'super_admin'), (req, res, next) =>
  productsController.update(req, res, next)
);

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

export default router;
