<?php

require_once __DIR__ . '/../../config/helpers.php';
handleCors();

$route = getApiRouteSegments();
$method = getMethod();
$resource = strtolower($route[0] ?? '');
$action = strtolower($route[1] ?? '');
$subAction = strtolower($route[2] ?? '');

if ($resource !== 'auth') {
    jsonResponse(false, 'Auth route not found', [
        'method' => $method,
        'resource' => $resource,
        'action' => $action,
        'subAction' => $subAction,
        'segments' => $route,
    ], 404);
}

switch ($action) {
    case 'login':
        require __DIR__ . '/login.php';
        break;
    case 'register':
    case 'signup':
        require __DIR__ . '/register.php';
        break;
    case 'check-username':
    case 'check_username':
        require __DIR__ . '/check-username.php';
        break;
    case 'logout':
        require __DIR__ . '/logout.php';
        break;
    case 'me':
        require __DIR__ . '/me.php';
        break;
    case 'update-profile':
        require __DIR__ . '/update-profile.php';
        break;
    case 'change-password':
        require __DIR__ . '/change-password.php';
        break;
    case 'forgot-password':
        require __DIR__ . '/forgot-password.php';
        break;
    case 'reset-password':
        require __DIR__ . '/reset-password.php';
        break;
    case 'sessions':
        if ($subAction === 'revoke') {
            require __DIR__ . '/sessions-revoke.php';
            break;
        }
        if ($subAction === 'revoke-others') {
            require __DIR__ . '/sessions-revoke-others.php';
            break;
        }
        require __DIR__ . '/sessions.php';
        break;
    case 'login-history':
        require __DIR__ . '/login-history.php';
        break;
    case 'security-overview':
        require __DIR__ . '/security-overview.php';
        break;
    default:
        jsonResponse(false, 'Auth route not found', [
            'method' => $method,
            'resource' => $resource,
            'action' => $action,
            'subAction' => $subAction,
            'segments' => $route,
        ], 404);
}
