<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';

if (!dbnToolsIsAuthenticated()) {
    header('Location: /?return=' . urlencode($_SERVER['REQUEST_URI'] ?? '/dashboard.php'));
    exit;
}

$uiLang = dbnToolsCurrentLanguage();
$tools  = dbnToolsLaunchedTools($uiLang);
$workbench = dbnToolsWorkbenchMeta($uiLang);
$langPath = '/dashboard.php';

require_once __DIR__ . '/includes/tool-svgs.php';
?>
<!doctype html>
<html lang="<?= htmlspecialchars($uiLang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars(dbnToolsT('dashboard_title', $uiLang)) ?> — Do Better Norge</title>
    <meta name="robots" content="noindex, nofollow">
    <meta name="theme-color" content="#00205B">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;600;700&family=IBM+Plex+Sans:wght@400;500;600;700&display=swap">
    <link rel="stylesheet" href="assets/css/tools.css">
</head>
<body data-authenticated="true" class="lt-app">
<script>
window.DBN_TOOLS_AUTHENTICATED = true;
window.DBN_TOOLS_LANG = <?= json_encode($uiLang, JSON_UNESCAPED_UNICODE) ?>;
</script>

<main id="appShell" class="app-shell dashboard-shell">
    <header class="topbar">
        <div>
            <p class="eyebrow"><?= htmlspecialchars(dbnToolsT('brand_line', $uiLang)) ?></p>
            <h1><?= htmlspecialchars(dbnToolsT('dashboard_title', $uiLang)) ?></h1>
            <div class="case-no">
                <span class="pulse"></span>
                <span>family-legal</span>
                <span class="case-sep">.</span>
                <span><?= htmlspecialchars(dbnToolsT('retention', $uiLang)) ?></span>
            </div>
        </div>
        <div class="topbar-actions">
            <nav class="shell-lang-switcher" aria-label="Language">
<?php foreach (dbnToolsSupportedLanguages() as $langCode): ?>
                <a href="<?= htmlspecialchars($langPath . '?lang=' . $langCode) ?>" class="<?= $langCode === $uiLang ? 'is-active' : '' ?>"><?= htmlspecialchars(dbnToolsLanguageLabel($langCode)) ?></a>
<?php endforeach; ?>
            </nav>
            <span id="healthPill" class="status-pill"><?= htmlspecialchars(dbnToolsT('session_active', $uiLang)) ?></span>
            <button id="healthButton" class="secondary-button" type="button"><?= htmlspecialchars(dbnToolsT('health', $uiLang)) ?></button>
        </div>
    </header>

    <section class="manifesto dashboard-manifesto">
        <div class="manifesto-copy">
            <p class="manifesto-eyebrow"><?= htmlspecialchars(dbnToolsT('dashboard_eyebrow', $uiLang)) ?></p>
            <h2 class="manifesto-title"><?= htmlspecialchars(dbnToolsT('manifesto_title', $uiLang)) ?></h2>
            <p class="manifesto-sub"><?= htmlspecialchars(dbnToolsT('dashboard_sub', $uiLang)) ?></p>
        </div>
        <div class="manifesto-stats" aria-label="Headline statistics">
            <div class="manifesto-stat"><strong>23</strong><span><?= htmlspecialchars(dbnToolsT('stat_echr', $uiLang)) ?></span></div>
            <div class="manifesto-stat"><strong>64%</strong><span><?= htmlspecialchars(dbnToolsT('stat_loss', $uiLang)) ?></span></div>
            <div class="manifesto-stat"><strong>1,731</strong><span><?= htmlspecialchars(dbnToolsT('stat_tribunal', $uiLang)) ?></span></div>
            <div class="manifesto-stat"><strong>20+</strong><span><?= htmlspecialchars(dbnToolsT('stat_pending', $uiLang)) ?></span></div>
        </div>
    </section>

    <div class="disclaimer" role="note"><?= htmlspecialchars(dbnToolsT('disclaimer', $uiLang)) ?></div>

    <section class="tool-dashboard-grid" aria-label="Available tools">
        <a class="dashboard-tool-card dashboard-tool-card--workbench" href="<?= htmlspecialchars($workbench['url']) ?>">
            <span class="dashboard-tool-card__icon"><?= htmlspecialchars($workbench['icon']) ?></span>
            <span class="dashboard-tool-card__badge"><?= htmlspecialchars($workbench['badge']) ?></span>
            <h2><?= htmlspecialchars($workbench['label']) ?></h2>
            <p><?= htmlspecialchars($workbench['description']) ?></p>
            <strong><?= htmlspecialchars(dbnToolsT('enter_workbench', $uiLang)) ?></strong>
        </a>
<?php foreach ($tools as $slug => $item): ?>
        <a class="dashboard-tool-card" href="<?= htmlspecialchars($item['url']) ?>">
            <span class="dashboard-tool-card__icon"><?= htmlspecialchars($item['icon']) ?></span>
            <span class="dashboard-tool-card__badge"><?= htmlspecialchars($item['badge']) ?></span>
            <h2><?= htmlspecialchars($item['label']) ?></h2>
            <p><?= htmlspecialchars($item['description']) ?></p>
            <strong><?= htmlspecialchars(dbnToolsT('open_tool', $uiLang)) ?></strong>
        </a>
<?php endforeach; ?>
    </section>
</main>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
<script src="assets/js/tools.js" defer></script>
</body>
</html>
