import { Router } from 'express';
import { getAll, getById, getByBarcode, create, update, remove, deleteImage } from '../controllers/productController.js';
import { protect } from '../middlewares/auth.js';
import { authorize } from '../middlewares/authorize.js';
import { ACTIONS } from '../utils/permissions.js';
import { createProductValidator, updateProductValidator } from '../validators/productValidator.js';
import { handleValidation } from '../utils/handleValidation.js';
import upload from '../middlewares/upload.js';
import { parseFormDataFields } from '../middlewares/parseFormData.js';

const router = Router();

router.get('/', protect, getAll);
router.get('/barcode/:barcode', protect, getByBarcode);
router.get('/:id', protect, getById);
router.post('/', protect, authorize(ACTIONS.PRODUCT_CREATE), upload.single('image'), parseFormDataFields, createProductValidator, handleValidation, create);
router.put('/:id', protect, authorize(ACTIONS.PRODUCT_UPDATE), upload.single('image'), parseFormDataFields, updateProductValidator, handleValidation, update);
router.delete('/:id/image', protect, authorize(ACTIONS.PRODUCT_UPDATE), deleteImage);
router.delete('/:id', protect, authorize(ACTIONS.PRODUCT_DELETE), remove);

export default router;
