const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const projectsController = require('../controllers/projects.controller');
const { authenticate, authorize, readOnlyForAuditor } = require('../middleware/auth.middleware');
const { ROLES } = require('../utils/roles');

const projectValidation = [
  body('name').trim().notEmpty().isLength({ max: 200 }),
  body('type').trim().notEmpty(),
  body('category').trim().notEmpty(),
  body('startDate').isISO8601(),
  body('plannedEndDate').isISO8601(),
  body('budget').isDecimal({ decimal_digits: '0,2' }),
];

router.use(authenticate);

router.get('/', projectsController.getAll);
router.get('/:id', projectsController.getById);
router.post(
  '/',
  authorize(ROLES.SUPER_ADMIN, ROLES.DIRECTOR, ROLES.GERENTE),
  readOnlyForAuditor,
  projectValidation,
  projectsController.create
);
router.patch(
  '/:id',
  authorize(ROLES.SUPER_ADMIN, ROLES.DIRECTOR, ROLES.GERENTE),
  readOnlyForAuditor,
  projectsController.update
);

module.exports = router;
