<?php
require_once __DIR__ . '/../controllers/AuthController.php';
require_once __DIR__ . '/../middleware/AuthMiddleware.php';
require_once __DIR__ . '/../middleware/RoleMiddleware.php';

function handleAuthRoutes($path, $method, $database) {
    $authController = new AuthController($database);
    
    switch ($path) {
        case 'auth/login':
            if ($method === 'POST') {
                return $authController->login();
            }
            break;
            
        case 'auth/logout':
            if ($method === 'POST') {
                // Verificar autenticación para logout
                AuthMiddleware::requireAuth();
                return $authController->logout();
            }
            break;
            
        case 'auth/refresh':
            if ($method === 'POST') {
                return $authController->refresh();
            }
            break;
            
        case 'auth/me':
            if ($method === 'GET') {
                // Requiere autenticación
                AuthMiddleware::requireAuth();
                return $authController->me();
            }
            break;
            
        case 'auth/change-password':
            if ($method === 'POST') {
                // Requiere autenticación
                AuthMiddleware::requireAuth();
                return $authController->changePassword();
            }
            break;
            
        case 'auth/permissions':
            if ($method === 'GET') {
                // Requiere autenticación
                AuthMiddleware::requireAuth();
                
                $permissions = RoleMiddleware::getPermissions();
                $userRole = AuthMiddleware::getUserRole();
                
                http_response_code(200);
                return json_encode([
                    'success' => true,
                    'data' => [
                        'role' => $userRole,
                        'permissions' => $permissions
                    ]
                ]);
            }
            break;
    }
    
    return null;
}
?>