<?php
/*
 * package examples/crash_reporter/backend/public/index.php
 * index.php
 * Created at: Wed 20 May 2026 14:31:55 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 63 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	90d9b287d10572f80696355281ba308437d49936dba1d421037afbabc1dc135e
 */
declare(strict_types=1);
require_once __DIR__ . '/../src/bootstrap.php';

$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
$base = Auth::basePath();
if ($base !== '' && str_starts_with($uri, $base)) {
    $uri = substr($uri, strlen($base)) ?: '/';
}
$route = rtrim($uri, '/') ?: '/';

if ($route === '/api/upload.php' || str_ends_with($route, '/api/upload.php')) {
    require __DIR__ . '/api/upload.php';
    exit;
}

if ($route === '/api/diag.php' || str_ends_with($route, '/api/diag.php')) {
    require __DIR__ . '/api/diag.php';
    exit;
}

if ($route === '/api/heartbeat.php' || str_ends_with($route, '/api/heartbeat.php')) {
    require __DIR__ . '/api/heartbeat.php';
    exit;
}

if ($route === '/api/reports.php' || str_ends_with($route, '/api/reports.php')) {
    require __DIR__ . '/api/reports.php';
    exit;
}

if ($route === '/api/report_viewed.php' || str_ends_with($route, '/api/report_viewed.php')) {
    require __DIR__ . '/api/report_viewed.php';
    exit;
}

if ($route === '/api/report_tags.php' || str_ends_with($route, '/api/report_tags.php')) {
    require __DIR__ . '/api/report_tags.php';
    exit;
}

if ($route === '/api/ticket_upload.php' || str_ends_with($route, '/api/ticket_upload.php')) {
    require __DIR__ . '/api/ticket_upload.php';
    exit;
}

if ($route === '/api/tickets.php' || str_ends_with($route, '/api/tickets.php')) {
    require __DIR__ . '/api/tickets.php';
    exit;
}

if ($route === '/api/ticket_tags.php' || str_ends_with($route, '/api/ticket_tags.php')) {
    require __DIR__ . '/api/ticket_tags.php';
    exit;
}

if ($route === '/api/ticket_update.php' || str_ends_with($route, '/api/ticket_update.php')) {
    require __DIR__ . '/api/ticket_update.php';
    exit;
}

if ($route === '/api/ticket_comments.php' || str_ends_with($route, '/api/ticket_comments.php')) {
    require __DIR__ . '/api/ticket_comments.php';
    exit;
}

if ($route === '/api/users.php' || str_ends_with($route, '/api/users.php')) {
    require __DIR__ . '/api/users.php';
    exit;
}

if ($route === '/api/ticket_attachments.php' || str_ends_with($route, '/api/ticket_attachments.php')) {
    require __DIR__ . '/api/ticket_attachments.php';
    exit;
}

if ($route === '/api/graphs.php' || str_ends_with($route, '/api/graphs.php') || str_ends_with($route, '/graphs/api/graphs.php')) {
    require __DIR__ . '/api/graphs.php';
    exit;
}

if ($route === '/api/graph_upload.php' || str_ends_with($route, '/api/graph_upload.php') || str_ends_with($route, '/graphs/api/graph_upload.php')) {
    require __DIR__ . '/api/graph_upload.php';
    exit;
}

if ($route === '/api/remote_access.php' || str_ends_with($route, '/api/remote_access.php')) {
    require __DIR__ . '/api/remote_access.php';
    exit;
}

if ($route === '/api/rbac.php' || str_ends_with($route, '/api/rbac.php')) {
    require __DIR__ . '/api/rbac.php';
    exit;
}

if ($route === '/api/ticket_attachment.php' || str_ends_with($route, '/api/ticket_attachment.php')) {
    require __DIR__ . '/api/ticket_attachment.php';
    exit;
}

if ($route === '/api/hub_deploy_docs.php' || str_ends_with($route, '/api/hub_deploy_docs.php')) {
    require __DIR__ . '/api/hub_deploy_docs.php';
    exit;
}

if ($route === '/api/tag_catalog.php' || str_ends_with($route, '/api/tag_catalog.php')) {
    require __DIR__ . '/api/tag_catalog.php';
    exit;
}

if ($route === '/logout') {
    Auth::logout();
    header('Location: ' . $base . '/login');
    exit;
}

if ($route === '/login' && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = trim($_POST['username'] ?? '');
    $pass = $_POST['password'] ?? '';
    if (Auth::login($user, $pass)) {
        header('Location: ' . $base . '/');
        exit;
    }
    $loginError = 'Invalid credentials';
    require __DIR__ . '/../views/login.php';
    exit;
}

if ($route === '/login') {
    require __DIR__ . '/../views/login.php';
    exit;
}

if ($route === '/graphs' || $route === '/graphs/' || str_ends_with($route, '/app/androidcast_project/graphs') || str_ends_with($route, '/app/androidcast_project/graphs/')) {
    $_GET['view'] = 'graphs';
    $route = '/';
}

Auth::check();

$grouped = isset($_GET['group']) && $_GET['group'] === '1';
$view = $_GET['view'] ?? 'home';

if ($view === 'report' && isset($_GET['id'])) {
    try {
        $report = ReportRepository::getById((int) $_GET['id']);
        if (!$report) {
            http_response_code(404);
            echo 'Not found';
            exit;
        }
        $uid = (int) (Auth::user()['id'] ?? 0);
        if ($uid > 0) {
            try {
                ReportRepository::markViewed((int) $report['id'], $uid);
            } catch (Throwable $e) {
                error_log('markViewed: ' . $e->getMessage());
            }
        }
        $pageTitle = 'Report';
        require __DIR__ . '/../views/layout.php';
    } catch (Throwable $e) {
        error_log('report view: ' . $e->getMessage());
        http_response_code(500);
        if (cfg('debug')) {
            header('Content-Type: text/plain; charset=utf-8');
            echo 'Report view failed: ' . $e->getMessage();
        } else {
            echo 'Report view failed';
        }
    }
    exit;
}

if ($view === 'ticket' && isset($_GET['id'])) {
    try {
        Database::requireTicketsTable();
        $ticket = TicketRepository::getById((int) $_GET['id']);
        if (!$ticket) {
            http_response_code(404);
            echo 'Not found';
            exit;
        }
        $pageTitle = 'Ticket';
        require __DIR__ . '/../views/layout.php';
    } catch (Throwable $e) {
        error_log('ticket view: ' . $e->getMessage());
        http_response_code(500);
        echo cfg('debug') ? 'Ticket view failed: ' . $e->getMessage() : 'Ticket view failed';
    }
    exit;
}

$pageTitle = match ($view) {
    'home' => 'Home',
    'tickets' => 'Tickets',
    'graphs' => 'Graphs',
    'remote_access' => 'Remote access',
    'rbac' => 'Access control',
    'reports', 'report' => 'Issues',
    default => 'Console',
};
require __DIR__ . '/../views/layout.php';
