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

const router = Router();

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

// Get current user's permissions (any authenticated user)
router.get('/me', (req, res, next) =>
  permissionsController.getMyPermissions(req, res, next)
);

// Check permissions for current user (any authenticated user)
router.post('/check', (req, res, next) =>
  permissionsController.checkPermissions(req, res, next)
);

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

// Get available modules (admin, manager)
router.get('/modules', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  permissionsController.getModules(req, res, next)
);

// Get available resources (admin, manager)
router.get('/resources', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  permissionsController.getResources(req, res, next)
);

// Get permissions grouped by module (admin, manager)
router.get('/grouped', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  permissionsController.getGrouped(req, res, next)
);

// Get permissions by module (admin, manager)
router.get('/by-module/:module', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  permissionsController.getByModule(req, res, next)
);

// Get permission matrix for admin UI (admin only)
router.get('/matrix', requireRoles('admin', 'super_admin'), (req, res, next) =>
  permissionsController.getMatrix(req, res, next)
);

// Get effective permissions for a specific user (admin only)
router.get('/user/:userId', requireRoles('admin', 'super_admin'), (req, res, next) =>
  permissionsController.getUserPermissions(req, res, next)
);

export default router;
