<?php
require_once __DIR__ . '/auth.php';

function h($s): string {
    return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');
}

/**
 * Always return the ROOT URL of logviewer, even when called from /bid-control/
 * Example output:
 * /servicedirect/pingpostcalls/logviewer
 */
function lv_root_url(): string {
    static $root = null;
    if ($root !== null) return $root;

    $scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
    $scriptName = str_replace('\\', '/', $scriptName);

    $pos = strpos($scriptName, '/logviewer/');
    if ($pos !== false) {
        $root = substr($scriptName, 0, $pos + strlen('/logviewer'));
        return $root;
    }

    $dir = rtrim(str_replace('\\', '/', dirname($scriptName)), '/');
    $root = ($dir === '.' || $dir === '/') ? '' : $dir;
    return $root;
}

function lv_url(string $path = ''): string {
    $root = lv_root_url();
    $path = ltrim($path, '/');
    return $root . ($path !== '' ? '/' . $path : '');
}

function page_header(string $title): void {
    $cfg = app_config();
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= h($cfg['app_name'] ?? 'LogViewer') ?> - <?= h($title) ?></title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
        <link href="<?= h(lv_url('assets/app.css')) ?>" rel="stylesheet">
    </head>
    <body class="bg-light">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark border-bottom border-secondary-subtle">
        <div class="container-fluid">
            <a class="navbar-brand fw-semibold" href="<?= h(lv_url('dashboard.php')) ?>">
                <?= h($cfg['app_name'] ?? 'LogViewer') ?>
            </a>

            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#lvNavbar" aria-controls="lvNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="lvNavbar">
                <?php if (is_logged_in()): ?>
                    <div class="ms-auto d-flex flex-column flex-lg-row gap-2 mt-3 mt-lg-0">
                        <a class="btn btn-outline-light btn-sm" href="<?= h(lv_url('dashboard.php')) ?>">Dashboard</a>
                        <a class="btn btn-outline-info btn-sm" href="<?= h(lv_url('view.php?log=sd&range=24h')) ?>">SD Logs</a>
                        <a class="btn btn-outline-info btn-sm" href="<?= h(lv_url('view.php?log=twilio&range=24h')) ?>">Twilio Logs</a>
                        <a class="btn btn-outline-info btn-sm" href="<?= h(lv_url('view.php?log=merged&range=24h')) ?>">Merged</a>
                        <a class="btn btn-outline-success btn-sm" href="<?= h(lv_url('bid-control/index.php')) ?>">Bid Control</a>
                        <a class="btn btn-outline-warning btn-sm" href="<?= h(lv_url('logout.php')) ?>">Logout</a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </nav>

    <main class="container-fluid container-xl py-3 py-md-4">
    <?php
}

function page_footer(): void {
    ?>
    </main>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    </body>
    </html>
    <?php
}