<?php

declare(strict_types=1);

$ddbRequestStartedAt = microtime(true);

require_once dirname(__DIR__) . '/app/bootstrap.php';

use App\Controllers\AuthController;
use App\Controllers\BedsController;
use App\Controllers\BranchComparisonController;
use App\Controllers\ActionCenterController;
use App\Controllers\AccessControlController;
use App\Controllers\DashboardController;
use App\Controllers\DoctorsController;
use App\Controllers\DiarRuleSettingsController;
use App\Controllers\DiarInvestigationController;
use App\Controllers\DiarCommandCenterController; // DIAR_UI1_CONTROLLER_USE_V1 // DIAR4B_CONTROLLER_USE_V1
use App\Controllers\DiarExecutiveOverviewController; // DIAR_6B_1_COMPAT_CONTROLLER_USE_R1_1
use App\Controllers\DiarWorkspaceController; // DIAR_6B_2_R2_CONTROLLER_USE
use App\Controllers\DiarCasesController; // DIAR_6B_2_R2_CONTROLLER_USE
use App\Controllers\LabRadiologyController;
use App\Controllers\InpatientDailyRevenueController;
use App\Controllers\InpatientDailyRevenueSettingsController;
use App\Controllers\OperationsController;
use App\Controllers\RevenueController;
use App\Controllers\SpecialtyFinanceController;
use App\Core\AccessControl;
use App\Core\Auth;
use App\Core\Request;

$page = (string) Request::get('page', 'dashboard');
\App\Core\RequestTrace::start($ddbRequestStartedAt, $page);
\App\Core\RequestTrace::add('bootstrap', (microtime(true) - $ddbRequestStartedAt) * 1000);
$method = Request::method();

$publicPages = ['login', 'logout'];
if (!in_array($page, $publicPages, true)) {
    Auth::requireLogin();
    $requiredPermission = AccessControl::permissionForRoute($page, $method);
    if ($requiredPermission !== null && !AccessControl::allows($requiredPermission)) {
        AccessControl::forbidden();
    }
}

switch ($page) {
    case 'login':
        if ($method === 'POST') {
            (new AuthController())->login();
        } else {
            (new AuthController())->loginForm();
        }
        break;

    case 'switch-hospital':
        if ($method === 'POST') {
            (new AuthController())->switchHospital();
        } else {
            \redirect('index.php?page=dashboard&lang=' . \current_lang());
        }
        break;

    case 'logout':
        (new AuthController())->logout();
        break;

    case 'dashboard':
        (new DashboardController())->index();
        break;

    case 'branch-comparison':
        (new BranchComparisonController())->index();
        break;

    case 'action-center':
        (new ActionCenterController())->index();
        break;

    case 'action-review':
        if ($method === 'POST') {
            (new ActionCenterController())->updateReview();
        } else {
            (new ActionCenterController())->review();
        }
        break;

    case 'action-review-dashboard':
        (new ActionCenterController())->reviewDashboard();
        break;

    case 'action-governance-dashboard':
        (new ActionCenterController())->governanceDashboard();
        break;

    case 'action-governance-update':
        if ($method === 'POST') {
            (new ActionCenterController())->updateGovernance();
        } else {
            \redirect('index.php?page=action-center&lang=' . \current_lang());
        }
        break;

    case 'leadership-huddle':
        (new ActionCenterController())->leadershipHuddle();
        break;

    case 'revenue':
        (new RevenueController())->index();
        break;

    case 'revenue-performance-beacon':
        (new RevenueController())->performanceBeacon();
        break;

    case 'inpatient-daily-revenue':
        (new InpatientDailyRevenueController())->index();
        break;

    case 'inpatient-daily-revenue-export':
        (new InpatientDailyRevenueController())->export();
        break;

    case 'inpatient-daily-revenue-powerbi-export':
        (new InpatientDailyRevenueController())->exportPowerBi();
        break;

    case 'inpatient-daily-revenue-powerpoint-export':
        (new InpatientDailyRevenueController())->exportPowerPoint();
        break;

    case 'inpatient-daily-revenue-settings':
        if ($method === 'POST') {
            (new InpatientDailyRevenueSettingsController())->save();
        } else {
            (new InpatientDailyRevenueSettingsController())->index();
        }
        break;

    case 'specialty-finance':
        (new SpecialtyFinanceController())->index();
        break;

    case 'access-control':
        if ($method === 'POST') {
            (new AccessControlController())->save();
        } else {
            (new AccessControlController())->index();
        }
        break;

    case 'diar-rules':
        if ($method === 'POST') {
            (new DiarRuleSettingsController())->save();
        } else {
            (new DiarRuleSettingsController())->index();
        }
        break;


    // DIAR4B_INVESTIGATION_ROUTER_V1
    case 'diar-investigations':
        (new DiarInvestigationController())->index();
        break;

    case 'diar-case-evidence':
        (new DiarInvestigationController())->evidence();
        break;

    case 'diar-investigation':
        if ($method === 'POST') {
            (new DiarInvestigationController())->update();
        } else {
            (new DiarInvestigationController())->detail();
        }
        break;


    // DIAR_UI1_READ_ONLY_ROUTER_V1
    // DIAR_6B_1_COMPAT_ROUTER_R1_1
    // DIAR_6B_2_R2_ROUTES
    case 'diar-portal':
        (new DiarWorkspaceController())->portal();
        break;

    case 'diar-statistics':
        (new DiarWorkspaceController())->statistics();
        break;

    case 'diar-cases':
        (new DiarCasesController())->index();
        break;

    case 'diar-investigation-workspace':
        (new DiarWorkspaceController())->investigation();
        break;

    case 'diar-reports':
        (new DiarWorkspaceController())->reports();
        break;

    case 'diar-governance':
        (new DiarWorkspaceController())->governance();
        break;

    case 'diar-executive-overview':
        (new DiarExecutiveOverviewController())->index();
        break;

    case 'diar-command-center':
        (new DiarCommandCenterController())->index();
        break;

    case 'diar-engine-runs':
        (new DiarCommandCenterController())->runs();
        break;

    case 'diar-movement-dictionary':
        (new DiarCommandCenterController())->movements();
        break;

    case 'diar-daily-facts':
        (new DiarCommandCenterController())->facts();
        break;

    // DIAR_UI3_PROFILE_AND_DETECTION_WORKSPACE_V1
    case 'diar-statistical-profiles':
        (new DiarCommandCenterController())->profiles();
        break;

    case 'diar-anomaly-candidates':
        (new DiarCommandCenterController())->candidates();
        break;

    case 'visits':
    case 'operations':
        (new OperationsController())->module($page);
        break;

    case 'beds':
        (new BedsController())->index();
        break;

    case 'doctors':
        (new DoctorsController())->index();
        break;

    case 'lab':
        (new LabRadiologyController())->index();
        break;


    // DIAR_6B_5_R2_1_REPORT_EXPORT_ROUTES

    case 'diar-report-excel':
        (new DiarWorkspaceController())->exportReportExcel();
        break;

    case 'diar-report-powerpoint':
        (new DiarWorkspaceController())->exportReportPowerPoint();
        break;

    case 'diar-report-download':
        (new DiarWorkspaceController())->downloadReportExport();
        break;

    default:
        (new DashboardController())->index();
        break;
}
