<?php
require_once __DIR__ . '/includes/Database.php';
require_once __DIR__ . '/includes/Language.php';

// Определя езика: от URL, сесия или по подразбиране 'en'
$lang_code = $_GET['lang'] ?? $_SESSION['lang'] ?? 'en';
$lang = new Language($lang_code);

$pdo = Database::getInstance();
$token = $_GET['token'] ?? '';

if (!$token) {
    $message = '<div class="alert alert-danger">' . $lang->get('invalid_token') . '</div>';
} else {
    $stmt = $pdo->prepare("SELECT id FROM users WHERE verification_token = ? AND email_verified = 0");
    $stmt->execute([$token]);
    $user = $stmt->fetch();

    if ($user) {
        $pdo->prepare("UPDATE users SET email_verified = 1, verification_token = NULL WHERE id = ?")
            ->execute([$user['id']]);
        $message = '<div class="alert alert-success">' . $lang->get('email_verified_success') . ' <a href="/login.php">' . $lang->get('login_now') . '</a>.</div>';
    } else {
        $message = '<div class="alert alert-danger">' . $lang->get('token_invalid_or_used') . '</div>';
    }
}
?>

<!DOCTYPE html>
<html lang="<?= htmlspecialchars($lang_code) ?>">
<head>
    <meta charset="UTF-8">
    <title><?= $lang->get('email_verification') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <?= $lang->get('email_verification') ?>
                    </div>
                    <div class="card-body">
                        <?= $message ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>