<?php
declare(strict_types=1);
if (!is_file(__DIR__ . '/config/config.php')) { require __DIR__ . '/installer-ui.php'; exit; }
require_once __DIR__ . '/app/Helpers/bootstrap.php';

header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
$error = null;
try {
    if ((int)db()->query('SELECT COUNT(*) FROM ' . table('users'))->fetchColumn() > 0) {
        header('Location: /login.php');
        exit;
    }

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $firstName = trim((string)($_POST['first_name'] ?? ''));
        $lastName = trim((string)($_POST['last_name'] ?? ''));
        $email = trim((string)($_POST['email'] ?? ''));
        $password = (string)($_POST['password'] ?? '');

        if ($firstName === '' || $lastName === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error = 'Bitte Name und E-Mail-Adresse vollständig eingeben.';
        } elseif (strlen($password) < 12) {
            $error = 'Das Passwort muss mindestens 12 Zeichen lang sein.';
        } else {
            $statement = db()->prepare('INSERT INTO ' . table('users') . ' (first_name, last_name, email, password_hash, role, status) VALUES (:first_name, :last_name, :email, :password_hash, \'main_admin\', \'active\')');
            $statement->execute([
                ':first_name' => $firstName,
                ':last_name' => $lastName,
                ':email' => $email,
                ':password_hash' => password_hash($password, PASSWORD_DEFAULT),
            ]);
            header('Location: /login.php?created=1');
            exit;
        }
    }
} catch (Throwable $exception) {
    $error = 'Die Einrichtung konnte nicht abgeschlossen werden. Prüfe bitte die Datenbankverbindung und ob die SQL-Tabellen importiert sind.';
}
?>
<!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>TripTale einrichten</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:520px;margin:auto;background:white;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}label{display:block;margin-top:14px;font-weight:600}input{box-sizing:border-box;width:100%;margin-top:6px;padding:11px;border:1px solid #b9c1c8;border-radius:7px}button{margin-top:22px;padding:12px 16px;border:0;border-radius:7px;background:#126e82;color:white;font-weight:700}.error{background:#ffe6e3;padding:12px;border-radius:7px}</style></head><body><main><h1>TripTale einrichten</h1><p>Lege jetzt den einmaligen Main-Admin an.</p><?php if ($error): ?><p class="error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?><form method="post"><label>Vorname<input name="first_name" required></label><label>Nachname<input name="last_name" required></label><label>E-Mail<input name="email" type="email" required></label><label>Passwort (mindestens 12 Zeichen)<input name="password" type="password" minlength="12" required></label><button type="submit">Main-Admin anlegen</button></form></main></body></html>
