<?php
declare(strict_types=1);

require_once __DIR__ . '/app/Helpers/bootstrap.php';
$user = require_login();

$selected = language();
$languages = triptale_available_languages();
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    $selected = (string)($_POST['language'] ?? '');
    if (!array_key_exists($selected, $languages)) {
        $error = t('language.error_invalid');
    } else {
        try {
            db()->prepare('UPDATE ' . table('users') . ' SET preferred_language = :language WHERE id = :id')
                ->execute([':language' => $selected, ':id' => $user['id']]);
            $_SESSION['language'] = $selected;
            header('Location: /language.php?saved=1');
            exit;
        } catch (Throwable $exception) {
            $error = 'Die Sprache konnte noch nicht gespeichert werden. Bitte zuerst die SQL-Datei importieren.';
        }
    }
}
?>
<!doctype html>
<html lang="<?= htmlspecialchars($selected, ENT_QUOTES, 'UTF-8') ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title><?= htmlspecialchars(t('language.title'), ENT_QUOTES, 'UTF-8') ?> · TripTale</title>
    <link rel="stylesheet" href="/assets/triptale-ui.css?v=19">
    <style>
        body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;color:#17202a}
        main{max-width:620px;margin:32px auto;padding:28px;background:#fff;border-radius:14px;box-shadow:0 4px 18px #0001}
        .button,button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700;cursor:pointer}
        .back{background:#e3ebee;color:#126e82}.choice{display:block;margin:14px 0;padding:16px;border:1px solid #cbd5dc;border-radius:10px;cursor:pointer}.choice:has(input:checked){border:2px solid #126e82;background:#eef7f8}.choice input{margin-right:9px}.save{margin-top:12px}.success,.error{padding:12px;border-radius:8px}.success{background:#e6f5e9;color:#1d6831}.error{background:#ffe6e3;color:#8b2424}
        @media(max-width:600px){main{margin:16px;padding:18px}}
    </style>
</head>
<body>
<main>
    <a class="button back" href="/profile.php"><?= htmlspecialchars(t('language.back'), ENT_QUOTES, 'UTF-8') ?></a>
    <h1><?= htmlspecialchars(t('language.title'), ENT_QUOTES, 'UTF-8') ?></h1>
    <p><?= htmlspecialchars(t('language.intro'), ENT_QUOTES, 'UTF-8') ?></p>
    <?php if (isset($_GET['saved'])): ?><p class="success"><?= htmlspecialchars(t('language.saved'), ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
    <?php if ($error): ?><p class="error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
    <form method="post">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(), ENT_QUOTES, 'UTF-8') ?>">
        <?php foreach ($languages as $code => $label): ?>
            <label class="choice"><input type="radio" name="language" value="<?= htmlspecialchars($code, ENT_QUOTES, 'UTF-8') ?>" <?= $selected === $code ? 'checked' : '' ?>><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></label>
        <?php endforeach; ?>
        <button class="save" type="submit"><?= htmlspecialchars(t('language.save'), ENT_QUOTES, 'UTF-8') ?></button>
    </form>
</main>
</body>
</html>
