<?php

declare(strict_types=1);

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$bootstrapPath = __DIR__ . '/install-support.php';

if (!is_file($bootstrapPath)) {
    http_response_code(500);
    header('Content-Type: text/plain; charset=utf-8');
    echo "Installer-Basis nicht gefunden. Bitte `saas-app/public/install-support.php` mit hochladen.\n";
    echo 'Erwarteter Pfad: ' . $bootstrapPath;
    exit;
}

require_once $bootstrapPath;

clearstatcache();

$requestMethod = $_SERVER['REQUEST_METHOD'] ?? 'GET';

if (isset($_GET['download']) && $_GET['download'] === 'bundle' && is_file(scripts_bundle_output_path())) {
    header('Content-Type: application/sql; charset=utf-8');
    header('Content-Disposition: attachment; filename="all-migrations.sql"');
    readfile(scripts_bundle_output_path());
    exit;
}

if (!isset($_SESSION['installer_csrf'])) {
    $_SESSION['installer_csrf'] = bin2hex(random_bytes(24));
}

$defaults = scripts_read_env_file(scripts_env_example_path());
$currentEnv = scripts_read_env_file(scripts_env_path());
$defaultConnection = scripts_normalize_db_connection($currentEnv['DB_CONNECTION'] ?? ($defaults['DB_CONNECTION'] ?? 'mysql'));
$values = [
    'APP_URL' => $currentEnv['APP_URL'] ?? ($defaults['APP_URL'] ?? ''),
    'DB_CONNECTION' => $defaultConnection,
    'DB_HOST' => $currentEnv['DB_HOST'] ?? ($defaults['DB_HOST'] ?? '127.0.0.1'),
    'DB_PORT' => $currentEnv['DB_PORT'] ?? ($defaults['DB_PORT'] ?? scripts_default_db_port($defaultConnection)),
    'DB_DATABASE' => $currentEnv['DB_DATABASE'] ?? ($defaults['DB_DATABASE'] ?? ''),
    'DB_USERNAME' => $currentEnv['DB_USERNAME'] ?? ($defaults['DB_USERNAME'] ?? ''),
    'DB_PASSWORD' => '',
    'TENANCY_MODE' => $currentEnv['TENANCY_MODE'] ?? ($defaults['TENANCY_MODE'] ?? 'subdomain'),
    'TENANCY_CENTRAL_DOMAINS' => $currentEnv['TENANCY_CENTRAL_DOMAINS'] ?? ($defaults['TENANCY_CENTRAL_DOMAINS'] ?? 'localhost'),
    'TENANCY_FALLBACK_TENANT' => $currentEnv['TENANCY_FALLBACK_TENANT'] ?? ($defaults['TENANCY_FALLBACK_TENANT'] ?? ''),
    'MAIL_HOST' => $currentEnv['MAIL_HOST'] ?? ($defaults['MAIL_HOST'] ?? '127.0.0.1'),
    'MAIL_PORT' => $currentEnv['MAIL_PORT'] ?? ($defaults['MAIL_PORT'] ?? '1025'),
    'MAIL_USERNAME' => $currentEnv['MAIL_USERNAME'] ?? ($defaults['MAIL_USERNAME'] ?? ''),
    'MAIL_PASSWORD' => '',
    'MAIL_FROM_ADDRESS' => $currentEnv['MAIL_FROM_ADDRESS'] ?? ($defaults['MAIL_FROM_ADDRESS'] ?? 'noreply@example.test'),
    'MAIL_FROM_NAME' => $currentEnv['MAIL_FROM_NAME'] ?? ($defaults['MAIL_FROM_NAME'] ?? 'KaffeelisteSaaS'),
    'OIDC_ENABLED' => $currentEnv['OIDC_ENABLED'] ?? ($defaults['OIDC_ENABLED'] ?? 'false'),
    'OIDC_DEFAULT_PROVIDER' => $currentEnv['OIDC_DEFAULT_PROVIDER'] ?? ($defaults['OIDC_DEFAULT_PROVIDER'] ?? ''),
];
$adminValues = [
    'ADMIN_DISPLAY_NAME' => '',
    'ADMIN_EMAIL' => '',
    'ADMIN_PASSWORD' => '',
];

$messages = [];
$errors = [];
$executedMigrations = [];
$bundlePath = is_file(scripts_bundle_output_path()) ? scripts_bundle_output_path() : null;
$locked = scripts_installer_is_locked();
$saasAppPath = scripts_saas_app_path();
$saasAppVisible = is_dir($saasAppPath);
$openBaseDir = (string) ini_get('open_basedir');
$envPathState = scripts_path_writable_state(scripts_env_path());
$lockPathState = scripts_path_writable_state(scripts_installer_lock_path());
$bundlePathState = scripts_path_writable_state(scripts_bundle_output_path());
$activeConnection = scripts_normalize_db_connection($values['DB_CONNECTION']);
$activeConnectionLabel = scripts_connection_label($activeConnection);
$requiredExtension = scripts_required_pdo_extension($activeConnection);
$requiredExtensionLoaded = extension_loaded($requiredExtension);

if (!$saasAppVisible) {
    $errors[] = 'PHP darf den SaaS-App-Ordner aktuell nicht lesen. Die `open_basedir`-Konfiguration erlaubt nur `public/`.';
    $errors[] = 'Stelle den Document-Root weiter auf `saas-app/public`, erweitere aber `open_basedir` mindestens auf `saas-app/`.';
}

if ($requestMethod === 'POST' && !$locked) {
    $csrf = (string) ($_POST['csrf'] ?? '');

    if (!hash_equals($_SESSION['installer_csrf'], $csrf)) {
        $errors[] = 'Der Sicherheitsstatus ist ungültig. Bitte lade die Seite neu.';
    } else {
        foreach (array_keys($values) as $key) {
            if (array_key_exists($key, $_POST)) {
                $values[$key] = trim((string) $_POST[$key]);
            }
        }

        foreach (array_keys($adminValues) as $key) {
            if (array_key_exists($key, $_POST)) {
                $adminValues[$key] = trim((string) $_POST[$key]);
            }
        }

        $values['DB_CONNECTION'] = scripts_normalize_db_connection($values['DB_CONNECTION']);

        if ($values['DB_PORT'] === '') {
            $values['DB_PORT'] = scripts_default_db_port($values['DB_CONNECTION']);
        }

        $activeConnection = scripts_normalize_db_connection($values['DB_CONNECTION']);
        $activeConnectionLabel = scripts_connection_label($activeConnection);
        $requiredExtension = scripts_required_pdo_extension($activeConnection);
        $requiredExtensionLoaded = extension_loaded($requiredExtension);

        $values['OIDC_ENABLED'] = isset($_POST['OIDC_ENABLED']) ? 'true' : 'false';

        if ($values['DB_PASSWORD'] === '' && isset($currentEnv['DB_PASSWORD'])) {
            $values['DB_PASSWORD'] = $currentEnv['DB_PASSWORD'];
        }

        if ($values['MAIL_PASSWORD'] === '' && isset($currentEnv['MAIL_PASSWORD'])) {
            $values['MAIL_PASSWORD'] = $currentEnv['MAIL_PASSWORD'];
        }

        foreach (['APP_URL', 'DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USERNAME'] as $required) {
            if ($values[$required] === '') {
                $errors[] = $required . ' darf nicht leer sein.';
            }
        }

        foreach (['ADMIN_DISPLAY_NAME', 'ADMIN_EMAIL', 'ADMIN_PASSWORD'] as $required) {
            if ($adminValues[$required] === '') {
                $errors[] = $required . ' darf nicht leer sein.';
            }
        }

        if ($errors === []) {
            try {
                scripts_write_env_file($values);
                $messages[] = '.env wurde geschrieben.';

                $bundlePath = scripts_build_migration_bundle(null, $values['DB_CONNECTION']);
                $messages[] = 'Das SQL-Bundle für ' . scripts_connection_label($values['DB_CONNECTION']) . ' wurde erzeugt.';

                if (isset($_POST['run_migrations'])) {
                    $executedMigrations = scripts_run_sql_migrations([
                        'connection' => $values['DB_CONNECTION'],
                        'server' => $values['DB_HOST'],
                        'database' => $values['DB_DATABASE'],
                        'port' => $values['DB_PORT'],
                        'username' => $values['DB_USERNAME'],
                        'password' => $values['DB_PASSWORD'],
                    ]);
                    $messages[] = 'Die Schema-Migrationen wurden direkt über PHP für ' . $activeConnectionLabel . ' ausgeführt.';
                }

                if (!$requiredExtensionLoaded) {
                    throw new RuntimeException('Global-Admin konnte nicht angelegt werden, weil ' . $requiredExtension . ' nicht geladen ist.');
                }

                scripts_ensure_schema([
                    'connection' => $values['DB_CONNECTION'],
                    'server' => $values['DB_HOST'],
                    'database' => $values['DB_DATABASE'],
                    'port' => $values['DB_PORT'],
                    'username' => $values['DB_USERNAME'],
                    'password' => $values['DB_PASSWORD'],
                ]);
                $updateResults = scripts_run_pending_updates([
                    'connection' => $values['DB_CONNECTION'],
                    'server' => $values['DB_HOST'],
                    'database' => $values['DB_DATABASE'],
                    'port' => $values['DB_PORT'],
                    'username' => $values['DB_USERNAME'],
                    'password' => $values['DB_PASSWORD'],
                ], 'installer');
                $messages[] = count(array_filter($updateResults, static fn(array $result): bool => ($result['status'] ?? '') === 'success'))
                    . ' System-Update(s) wurden eingespielt.';

                $adminResult = scripts_create_platform_admin(
                    [
                        'connection' => $values['DB_CONNECTION'],
                        'server' => $values['DB_HOST'],
                        'database' => $values['DB_DATABASE'],
                        'port' => $values['DB_PORT'],
                        'username' => $values['DB_USERNAME'],
                        'password' => $values['DB_PASSWORD'],
                    ],
                    $adminValues['ADMIN_EMAIL'],
                    $adminValues['ADMIN_DISPLAY_NAME'],
                    $adminValues['ADMIN_PASSWORD']
                );
                $messages[] = $adminResult['created']
                    ? 'Der erste Global-Admin wurde angelegt.'
                    : 'Der bestehende Global-Admin wurde aktualisiert.';

                if (isset($_POST['lock_installer'])) {
                    scripts_installer_lock([
                        'app_url' => $values['APP_URL'],
                        'db_database' => $values['DB_DATABASE'],
                    ]);
                    $messages[] = 'Der Installer wurde gesperrt. Für eine erneute Ausführung muss `saas-app/.installer.lock` entfernt werden.';
                    $locked = true;
                }
            } catch (Throwable $exception) {
                $message = $exception->getMessage();

                if (scripts_string_contains($message, 'pdo_')) {
                    $errors[] = 'Die Schema-Migrationen konnten nicht direkt über PHP ausgeführt werden. Bitte prüfe `' . $requiredExtension . '` oder importiere das SQL-Bundle manuell.';
                } elseif (scripts_string_contains($message, '.env')) {
                    $errors[] = 'Die Konfiguration konnte nicht gespeichert werden. Zielpfad: ' . scripts_env_path();
                    $errors[] = 'Status für .env: ' . scripts_path_state_label($envPathState) . '. Elternordner: ' . $envPathState['parent'];
                    if (!$saasAppVisible) {
                        $errors[] = 'PHP kann den SaaS-App-Ordner nicht lesen. Bitte prüfe Rechte, Document-Root und `open_basedir`.';
                    }
                } else {
                    $errors[] = 'Die Installation konnte nicht abgeschlossen werden: ' . $message;
                }
            }
        }
    }
}

function h(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES);
}
?><!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Kaffeeliste SaaS Installer</title>
    <style>
        :root {
            --bg: #f7f1e8;
            --ink: #24170f;
            --muted: #6a5649;
            --brand: #0f766e;
            --accent: #b45309;
            --danger: #b42318;
            --ok: #027a48;
            --card: rgba(255, 252, 247, 0.92);
            --line: rgba(36, 23, 15, 0.12);
            --shadow: 0 24px 60px rgba(61, 38, 24, 0.12);
            --radius: 24px;
        }

        * { box-sizing: border-box; }
        body {
            margin: 0;
            font-family: "Aptos", "Segoe UI", sans-serif;
            color: var(--ink);
            background:
                radial-gradient(circle at top left, rgba(180, 83, 9, 0.16), transparent 28%),
                radial-gradient(circle at top right, rgba(15, 118, 110, 0.12), transparent 22%),
                linear-gradient(180deg, #fbf7f0 0%, var(--bg) 100%);
        }
        .shell {
            width: min(1100px, calc(100vw - 32px));
            margin: 24px auto 40px;
        }
        .hero, .panel {
            background: var(--card);
            border: 1px solid var(--line);
            border-radius: var(--radius);
            box-shadow: var(--shadow);
        }
        .hero {
            padding: 28px;
            margin-bottom: 18px;
        }
        .grid {
            display: grid;
            gap: 18px;
            grid-template-columns: 1.35fr 0.85fr;
        }
        .panel {
            padding: 24px;
        }
        h1, h2 {
            margin: 0 0 12px;
            font-family: Georgia, serif;
            line-height: 1.05;
        }
        h1 { font-size: clamp(2rem, 4vw, 3.4rem); }
        h2 { font-size: 1.35rem; }
        p, li {
            color: var(--muted);
            line-height: 1.65;
        }
        .eyebrow {
            display: inline-block;
            margin-bottom: 12px;
            color: var(--accent);
            font-size: 0.82rem;
            font-weight: 700;
            letter-spacing: 0.14em;
            text-transform: uppercase;
        }
        .message-list, .status-list {
            margin: 0;
            padding-left: 18px;
        }
        .message-list li + li,
        .status-list li + li {
            margin-top: 8px;
        }
        .ok { color: var(--ok); }
        .error { color: var(--danger); }
        form {
            display: grid;
            gap: 18px;
        }
        .field-grid {
            display: grid;
            gap: 14px;
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }
        .field {
            display: grid;
            gap: 8px;
        }
        .field.full {
            grid-column: 1 / -1;
        }
        label {
            font-size: 0.92rem;
            font-weight: 700;
        }
        input, select {
            width: 100%;
            border: 1px solid rgba(36, 23, 15, 0.16);
            border-radius: 14px;
            padding: 12px 14px;
            font: inherit;
            background: rgba(255, 255, 255, 0.96);
        }
        .checkbox {
            display: flex;
            gap: 10px;
            align-items: flex-start;
        }
        .checkbox input {
            width: auto;
            margin-top: 4px;
        }
        .stack {
            display: grid;
            gap: 10px;
        }
        .actions {
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
        }
        button, .link-button {
            border: 0;
            border-radius: 999px;
            padding: 12px 18px;
            font: inherit;
            font-weight: 700;
            text-decoration: none;
            cursor: pointer;
        }
        button {
            background: linear-gradient(135deg, var(--brand), #115e59);
            color: #fff;
        }
        .link-button {
            color: var(--brand);
            background: rgba(15, 118, 110, 0.08);
        }
        code {
            padding: 2px 6px;
            border-radius: 8px;
            background: rgba(15, 118, 110, 0.08);
            color: var(--brand);
        }
        @media (max-width: 900px) {
            .grid,
            .field-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<main class="shell">
    <section class="hero">
        <div class="eyebrow">Browser-Installer</div>
        <h1>Kaffeeliste SaaS direkt im Browser einrichten.</h1>
        <p>
            Diese Seite führt die Ersteinrichtung über den Webspace aus: `.env` schreiben,
            das Schema vorbereiten, ein SQL-Bundle erzeugen und den ersten Global-Admin anlegen.
            Nach erfolgreicher Einrichtung sollte der Installer gesperrt bleiben.
        </p>
        <div class="actions" style="margin-top: 18px;">
            <a class="link-button" href="./../index.php">Zur Preview</a>
            <a class="link-button" href="/admin/login/">Zum Global-Admin</a>
        </div>
    </section>

    <section class="grid">
        <section class="panel">
            <h2>Installation</h2>

            <?php if ($messages !== []): ?>
                <ul class="message-list">
                    <?php foreach ($messages as $message): ?>
                        <li class="ok"><?= h($message) ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if ($errors !== []): ?>
                <ul class="message-list">
                    <?php foreach ($errors as $error): ?>
                        <li class="error"><?= h($error) ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if ($locked): ?>
                <ul class="status-list">
                    <li>Der Installer ist gesperrt.</li>
                    <li>Für eine erneute Ausführung muss <code>saas-app/.installer.lock</code> entfernt werden.</li>
                </ul>
            <?php else: ?>
                <form method="post">
                    <input type="hidden" name="csrf" value="<?= h($_SESSION['installer_csrf']) ?>">

                    <div class="field-grid">
                        <div class="field full">
                            <label for="APP_URL">APP_URL</label>
                            <input id="APP_URL" name="APP_URL" value="<?= h($values['APP_URL']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="DB_CONNECTION">DB_CONNECTION</label>
                            <select id="DB_CONNECTION" name="DB_CONNECTION">
                                <?php foreach (scripts_supported_connections() as $connectionKey => $connectionLabel): ?>
                                    <option value="<?= h($connectionKey) ?>"<?= $values['DB_CONNECTION'] === $connectionKey ? ' selected' : '' ?>><?= h($connectionLabel) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="field">
                            <label for="DB_HOST">DB_HOST</label>
                            <input id="DB_HOST" name="DB_HOST" value="<?= h($values['DB_HOST']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="DB_PORT">DB_PORT</label>
                            <input id="DB_PORT" name="DB_PORT" value="<?= h($values['DB_PORT']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="DB_DATABASE">DB_DATABASE</label>
                            <input id="DB_DATABASE" name="DB_DATABASE" value="<?= h($values['DB_DATABASE']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="DB_USERNAME">DB_USERNAME</label>
                            <input id="DB_USERNAME" name="DB_USERNAME" value="<?= h($values['DB_USERNAME']) ?>" required>
                        </div>
                        <div class="field full">
                            <label for="DB_PASSWORD">DB_PASSWORD</label>
                            <input id="DB_PASSWORD" name="DB_PASSWORD" type="password" value="<?= h($values['DB_PASSWORD']) ?>">
                        </div>
                        <div class="field">
                            <label for="TENANCY_MODE">TENANCY_MODE</label>
                            <select id="TENANCY_MODE" name="TENANCY_MODE">
                                <option value="subdomain"<?= $values['TENANCY_MODE'] === 'subdomain' ? ' selected' : '' ?>>subdomain</option>
                                <option value="path"<?= $values['TENANCY_MODE'] === 'path' ? ' selected' : '' ?>>path</option>
                            </select>
                        </div>
                        <div class="field">
                            <label for="TENANCY_CENTRAL_DOMAINS">TENANCY_CENTRAL_DOMAINS</label>
                            <input id="TENANCY_CENTRAL_DOMAINS" name="TENANCY_CENTRAL_DOMAINS" value="<?= h($values['TENANCY_CENTRAL_DOMAINS']) ?>">
                        </div>
                        <div class="field full">
                            <label for="TENANCY_FALLBACK_TENANT">TENANCY_FALLBACK_TENANT</label>
                            <input id="TENANCY_FALLBACK_TENANT" name="TENANCY_FALLBACK_TENANT" value="<?= h($values['TENANCY_FALLBACK_TENANT']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_HOST">MAIL_HOST</label>
                            <input id="MAIL_HOST" name="MAIL_HOST" value="<?= h($values['MAIL_HOST']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_PORT">MAIL_PORT</label>
                            <input id="MAIL_PORT" name="MAIL_PORT" value="<?= h($values['MAIL_PORT']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_USERNAME">MAIL_USERNAME</label>
                            <input id="MAIL_USERNAME" name="MAIL_USERNAME" value="<?= h($values['MAIL_USERNAME']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_PASSWORD">MAIL_PASSWORD</label>
                            <input id="MAIL_PASSWORD" name="MAIL_PASSWORD" type="password" value="<?= h($values['MAIL_PASSWORD']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_FROM_ADDRESS">MAIL_FROM_ADDRESS</label>
                            <input id="MAIL_FROM_ADDRESS" name="MAIL_FROM_ADDRESS" value="<?= h($values['MAIL_FROM_ADDRESS']) ?>">
                        </div>
                        <div class="field">
                            <label for="MAIL_FROM_NAME">MAIL_FROM_NAME</label>
                            <input id="MAIL_FROM_NAME" name="MAIL_FROM_NAME" value="<?= h($values['MAIL_FROM_NAME']) ?>">
                        </div>
                        <div class="field">
                            <label for="OIDC_DEFAULT_PROVIDER">OIDC_DEFAULT_PROVIDER</label>
                            <input id="OIDC_DEFAULT_PROVIDER" name="OIDC_DEFAULT_PROVIDER" value="<?= h($values['OIDC_DEFAULT_PROVIDER']) ?>">
                        </div>
                        <div class="field">
                            <label>OIDC_ENABLED</label>
                            <label class="checkbox">
                                <input name="OIDC_ENABLED" type="checkbox"<?= $values['OIDC_ENABLED'] === 'true' ? ' checked' : '' ?>>
                                <span>OIDC aktivieren</span>
                            </label>
                        </div>
                        <div class="field full">
                            <label for="ADMIN_DISPLAY_NAME">Global-Admin Name</label>
                            <input id="ADMIN_DISPLAY_NAME" name="ADMIN_DISPLAY_NAME" value="<?= h($adminValues['ADMIN_DISPLAY_NAME']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="ADMIN_EMAIL">Global-Admin E-Mail</label>
                            <input id="ADMIN_EMAIL" name="ADMIN_EMAIL" type="email" value="<?= h($adminValues['ADMIN_EMAIL']) ?>" required>
                        </div>
                        <div class="field">
                            <label for="ADMIN_PASSWORD">Global-Admin Passwort</label>
                            <input id="ADMIN_PASSWORD" name="ADMIN_PASSWORD" type="password" value="<?= h($adminValues['ADMIN_PASSWORD']) ?>" required>
                        </div>
                    </div>

                    <div class="stack">
                        <label class="checkbox">
                            <input name="run_migrations" type="checkbox"<?= $requiredExtensionLoaded ? '' : ' disabled' ?>>
                            <span>Schema direkt über PHP für <?= h($activeConnectionLabel) ?> anlegen<?= $requiredExtensionLoaded ? '' : ' (' . h($requiredExtension) . ' fehlt aktuell)' ?></span>
                        </label>
                        <label class="checkbox">
                            <input name="lock_installer" type="checkbox" checked>
                            <span>Installer nach erfolgreicher Einrichtung sperren</span>
                        </label>
                    </div>

                    <div class="actions">
                        <button type="submit">Installation starten</button>
                        <?php if ($bundlePath !== null): ?>
                            <a class="link-button" href="?download=bundle">SQL-Bundle herunterladen</a>
                        <?php endif; ?>
                    </div>
                </form>
            <?php endif; ?>
        </section>

        <aside class="panel">
            <h2>Status</h2>
            <ul class="status-list">
                <li>PHP Version: <code><?= h(PHP_VERSION) ?></code></li>
                <li>SaaS-App Pfad: <code><?= h($saasAppPath) ?></code></li>
                <li>SaaS-App sichtbar: <code><?= $saasAppVisible ? 'ja' : 'nein' ?></code></li>
                <li>open_basedir: <code><?= h($openBaseDir !== '' ? $openBaseDir : 'nicht gesetzt') ?></code></li>
                <li>DB_CONNECTION: <code><?= h($activeConnection) ?></code></li>
                <li>Ziel-Datenbank: <code><?= h($activeConnectionLabel) ?></code></li>
                <li>.env.example: <code><?= is_file(scripts_env_example_path()) ? 'vorhanden' : 'fehlt' ?></code></li>
                <li>.env: <code><?= is_file(scripts_env_path()) ? 'vorhanden' : 'fehlt' ?></code></li>
                <li>.env Status: <code><?= h(scripts_path_state_label($envPathState)) ?></code></li>
                <li>.env Zielpfad: <code><?= h($envPathState['path']) ?></code></li>
                <li>.env Elternordner: <code><?= h($envPathState['parent']) ?></code></li>
                <li>.env Elternordner beschreibbar: <code><?= $envPathState['parent_writable'] ? 'ja' : 'nein' ?></code></li>
                <li><?= h($requiredExtension) ?>: <code><?= $requiredExtensionLoaded ? 'aktiv' : 'nicht geladen' ?></code></li>
                <li>Installer-Lock: <code><?= $locked ? 'aktiv' : 'offen' ?></code></li>
                <li>Lock-Datei Status: <code><?= h(scripts_path_state_label($lockPathState)) ?></code></li>
                <li>Bundle-Pfad: <code><?= h(scripts_bundle_output_path()) ?></code></li>
                <li>Bundle-Ziel Status: <code><?= h(scripts_path_state_label($bundlePathState)) ?></code></li>
            </ul>

            <?php if ($bundlePath !== null): ?>
                <p style="margin-top: 18px;">Erzeugtes Bundle: <code><?= h($bundlePath) ?></code></p>
            <?php endif; ?>

            <?php if ($executedMigrations !== []): ?>
                <p style="margin-top: 18px;">Ausgeführte Migrationen:</p>
                <ul class="status-list">
                    <?php foreach ($executedMigrations as $file): ?>
                        <li><code><?= h($file) ?></code></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </aside>
    </section>
</main>
</body>
</html>
