<?php
// public/register.php
declare(strict_types=1);

require __DIR__ . '/../src/auth.php';

$lang = auth_get_lang();
$errors = [];
$globalError = '';
$result = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!auth_verify_csrf($_POST['_csrf'] ?? null)) {
        $globalError = 'Sicherheitsfehler. Bitte Formular erneut absenden.';
    } else {
        $email            = $_POST['email'] ?? '';
        $username         = $_POST['username'] ?? '';
        $fullName         = $_POST['full_name'] ?? '';
        $password         = $_POST['password'] ?? '';
        $passwordConfirm  = $_POST['password_confirm'] ?? '';
        $preferredLang    = $_POST['preferred_lang'] ?? $lang;

        $result = auth_register_user(
            $email,
            $username,
            $fullName,
            $password,
            $passwordConfirm,
            $preferredLang
        );

        if ($result['success'] ?? false) {
            // Direkt nach Account-Seite
            header('Location: /account.php');
            exit;
        }

        $errors = $result['errors'] ?? [];
    }
}

$csrfToken = auth_csrf_token();
?>
<!DOCTYPE html>
<html lang="<?php echo htmlspecialchars($lang, ENT_QUOTES, 'UTF-8'); ?>">
<head>
    <meta charset="UTF-8">
    <title>Registrieren – usbcheck.it</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Fonts: Montserrat + Inter -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500&family=Montserrat:wght@600;700&display=swap" rel="stylesheet">

    <!-- Main stylesheet -->
    <link rel="stylesheet" href="/assets/css/main.css?v=1">
</head>
<body>
<?php include __DIR__ . '/partials/header.php'; ?>

<main class="auth-page">
    <div class="container">
        <div class="auth-layout">
            <div class="auth-card">
                <h1 class="auth-title">Konto erstellen</h1>
                <p class="auth-subtitle">
                    Erstelle einen kostenlosen Account, um deine USB-Tests zu speichern und mehrere Sticks zu verwalten.
                </p>

                <?php if ($globalError): ?>
                    <div class="auth-flash-error">
                        <?php echo htmlspecialchars($globalError, ENT_QUOTES, 'UTF-8'); ?>
                    </div>
                <?php endif; ?>

                <form method="post" novalidate>
                    <input type="hidden" name="_csrf" value="<?php echo htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8'); ?>">

                    <div class="form-row">
                        <label class="form-label" for="email">E-Mail-Adresse</label>
                        <input
                            class="form-input"
                            type="email"
                            id="email"
                            name="email"
                            required
                            value="<?php echo htmlspecialchars($_POST['email'] ?? '', ENT_QUOTES, 'UTF-8'); ?>"
                        >
                        <?php if (!empty($errors['email'])): ?>
                            <div class="form-error"><?php echo htmlspecialchars($errors['email'], ENT_QUOTES, 'UTF-8'); ?></div>
                        <?php endif; ?>
                    </div>

                    <div class="form-row">
                        <label class="form-label" for="username">Benutzername</label>
                        <input
                            class="form-input"
                            type="text"
                            id="username"
                            name="username"
                            required
                            value="<?php echo htmlspecialchars($_POST['username'] ?? '', ENT_QUOTES, 'UTF-8'); ?>"
                        >
                        <?php if (!empty($errors['username'])): ?>
                            <div class="form-error"><?php echo htmlspecialchars($errors['username'], ENT_QUOTES, 'UTF-8'); ?></div>
                        <?php endif; ?>
                        <div class="form-help">
                            3–32 Zeichen, Buchstaben/Zahlen/._- erlaubt.
                        </div>
                    </div>

                    <div class="form-row">
                        <label class="form-label" for="full_name">Vollständiger Name</label>
                        <input
                            class="form-input"
                            type="text"
                            id="full_name"
                            name="full_name"
                            required
                            value="<?php echo htmlspecialchars($_POST['full_name'] ?? '', ENT_QUOTES, 'UTF-8'); ?>"
                        >
                        <?php if (!empty($errors['full_name'])): ?>
                            <div class="form-error"><?php echo htmlspecialchars($errors['full_name'], ENT_QUOTES, 'UTF-8'); ?></div>
                        <?php endif; ?>
                    </div>

                    <div class="form-row">
                        <label class="form-label" for="password">Passwort</label>
                        <input
                            class="form-input"
                            type="password"
                            id="password"
                            name="password"
                            required
                        >
                        <?php if (!empty($errors['password'])): ?>
                            <div class="form-error"><?php echo htmlspecialchars($errors['password'], ENT_QUOTES, 'UTF-8'); ?></div>
                        <?php endif; ?>
                        <div class="form-help">
                            Mindestens 10 Zeichen. Bitte ein sicheres Passwort wählen.
                        </div>
                    </div>

                    <div class="form-row">
                        <label class="form-label" for="password_confirm">Passwort wiederholen</label>
                        <input
                            class="form-input"
                            type="password"
                            id="password_confirm"
                            name="password_confirm"
                            required
                        >
                        <?php if (!empty($errors['password_confirm'])): ?>
                            <div class="form-error"><?php echo htmlspecialchars($errors['password_confirm'], ENT_QUOTES, 'UTF-8'); ?></div>
                        <?php endif; ?>
                    </div>

                    <div class="form-row">
                        <label class="form-label" for="preferred_lang">Bevorzugte Sprache</label>
                        <select class="form-select" id="preferred_lang" name="preferred_lang">
                            <?php
                            $selLang = $_POST['preferred_lang'] ?? $lang;
                            $opts = [
                                'de' => 'Deutsch',
                                'en' => 'English',
                                'it' => 'Italiano',
                                'fr' => 'Français',
                            ];
                            foreach ($opts as $code => $label) {
                                $selected = ($code === $selLang) ? 'selected' : '';
                                echo '<option value="' . htmlspecialchars($code, ENT_QUOTES, 'UTF-8') . '" ' . $selected . '>'
                                    . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '</option>';
                            }
                            ?>
                        </select>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            Konto erstellen
                        </button>
                        <a class="auth-link" href="/login.php">
                            Bereits ein Konto? Login
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</main>

<?php include __DIR__ . '/partials/footer.php'; ?>

<script src="/assets/js/lang.js?v=1"></script>
</body>
</html>
