const express = require('express');
const { body } = require('express-validator');
const validate = require('../middleware/validate');
const { authenticate, authorize } = require('../middleware/auth');
const PromotionController = require('../controllers/PromotionController');

const router = express.Router();

router.get('/', PromotionController.getAll);
router.get('/active', PromotionController.getActive);
router.get('/code/:code', PromotionController.getByCode);
router.get('/:id', PromotionController.getById);
router.post(
  '/',
  authenticate,
  authorize('admin', 'manager'),
  [
    body('title').notEmpty().withMessage('Title required'),
    body('discount_value').isDecimal().withMessage('Discount value required'),
    body('start_date').isISO8601().withMessage('Valid start date required'),
    body('end_date').isISO8601().withMessage('Valid end date required'),
    validate,
  ],
  PromotionController.create
);
router.put('/:id', authenticate, authorize('admin', 'manager'), PromotionController.update);
router.delete('/:id', authenticate, authorize('admin'), PromotionController.remove);

module.exports = router;
