import { Router } from 'express';
import { getAll, getById, create, getInvoice } from '../controllers/saleController.js';
import { getReturns, createReturn } from '../controllers/returnController.js';
import { protect } from '../middlewares/auth.js';
import { authorize } from '../middlewares/authorize.js';
import { ACTIONS } from '../utils/permissions.js';
import { createSaleValidator } from '../validators/saleValidator.js';
import { validateSaleId, validateReturn } from '../validators/returnValidator.js';
import { handleValidation } from '../utils/handleValidation.js';

const router = Router();

router.get('/', protect, getAll);
router.get('/:id', protect, getById);
router.get('/:id/invoice', protect, getInvoice);
router.get('/:id/returns', protect, validateSaleId, handleValidation, getReturns);
router.post('/:id/returns', protect, authorize(ACTIONS.RETURN_CREATE), validateReturn, handleValidation, createReturn);
router.post('/', protect, authorize(ACTIONS.SALE_CREATE), createSaleValidator, handleValidation, create);

export default router;
