import { Router } from 'express';
import { rolesController } from './roles.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

// All routes require authentication
router.use(authenticate);

// List roles (admin, manager)
router.get('/', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  rolesController.findAll(req, res, next)
);

// Get system roles (admin)
router.get('/system', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.getSystemRoles(req, res, next)
);

// Get role by ID (admin, manager)
router.get('/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  rolesController.findById(req, res, next)
);

// Create role (admin only)
router.post('/', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.create(req, res, next)
);

// Update role (admin only)
router.put('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.update(req, res, next)
);

// Delete role (admin only)
router.delete('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.delete(req, res, next)
);

// Role permissions management
router.get('/:id/permissions', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  rolesController.getPermissions(req, res, next)
);

router.put('/:id/permissions', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.assignPermissions(req, res, next)
);

router.post('/:id/permissions', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.addPermission(req, res, next)
);

router.delete('/:id/permissions/:permissionId', requireRoles('admin', 'super_admin'), (req, res, next) =>
  rolesController.removePermission(req, res, next)
);

export default router;
