// routes/productRoutes.js
const express = require('express');
const router = express.Router();
const productController = require('../controllers/productController');
const { authenticate, authorize } = require('../middleware/auth');



// Template download endpoint
router.get('/import-template', productController.downloadImportTemplate);

// Specific routes FIRST
router.get('/detailed', productController.getAllProductsDetailed);
router.get('/top', productController.getTopProducts); // ✅ Move this up
router.get('/search/barcode/:barcode', productController.searchByBarcode);

// General routes
router.get('/', productController.getAllProducts);

// Parameterized routes LAST
router.get('/:id', productController.getProductById); // ✅ This should be last



// Protected routes
router.use(authenticate);


router.post('/', authorize(['admin', 'manager', 'inventory_manager']), productController.createProduct);
router.put('/:id', authorize(['admin', 'manager', 'inventory_manager']), productController.updateProduct);
router.delete('/:id', authorize(['admin']), productController.deleteProduct);

router.post('/import', productController.importProductsFromExcel);


// Image upload routes
router.post('/upload-images', productController.uploadProductImages);
router.get('/:productId/images', productController.getProductImages);
router.delete('/images/:imageId', productController.deleteProductImage);

// Product CRUD with images
router.post('/newproduct', productController.createProductWithImages);
router.put('/updateproduct/:id', productController.updateProductWithImages);




module.exports = router;




