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

$pdo = Database::getInstance();
$auth = new Auth($pdo);
$lang = new Language($_SESSION['lang'] ?? 'en');

// Функция за извличане на настройка (локална, без да променяме други файлове)
function getSetting($pdo, $name, $default = '') {
    $stmt = $pdo->prepare("SELECT value FROM settings WHERE name = ?");
    $stmt->execute([$name]);
    return $stmt->fetchColumn() ?: $default;
}

$error = '';
$success = false;

if ($_POST['register'] ?? false) {
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $password2 = $_POST['password2'] ?? '';

    // Валидация
    if (empty($username)) {
        $error = $lang->get('username_required');
    } elseif (empty($email)) {
        $error = $lang->get('email_required');
    } elseif (empty($password)) {
        $error = $lang->get('password_required');
    } elseif ($password !== $password2) {
        $error = $lang->get('passwords_dont_match');
    } elseif (strlen($password) < 6) {
        $error = $lang->get('password_too_short');
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = $lang->get('invalid_email_format');
    } elseif (strlen($username) < 3 || strlen($username) > 20) {
        $error = $lang->get('username_length_invalid');
    } else {
        try {
            // Проверка за съществуващ потребител или имейл
            $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
            $stmt->execute([$username, $email]);
            if ($stmt->fetch()) {
                $error = $lang->get('user_or_email_exists');
            } else {
                // 🔐 Хеширане на паролата
                $hashedPass = password_hash($password, PASSWORD_DEFAULT);
                if ($hashedPass === false) {
                    throw new Exception('Password hashing failed');
                }

                // Проверка дали имейл валидацията е включена
                $email_verification_enabled = (getSetting($pdo, 'email_verification', 'disabled') === 'enabled');
                $email_verified = $email_verification_enabled ? 0 : 1;
                $verification_token = $email_verification_enabled ? bin2hex(random_bytes(32)) : null;

                // Вмъкване в базата
                $stmt = $pdo->prepare("INSERT INTO users (username, email, password, `rank`, `language`, `style`, email_verified, verification_token) VALUES (?, ?, ?, 2, ?, 'light', ?, ?)");
                $stmt->execute([$username, $email, $hashedPass, $_SESSION['lang'] ?? 'en', $email_verified, $verification_token]);

                // Ако валидацията е включена — изпращаме имейл
                if ($email_verification_enabled) {
                    // Ще изпратя функцията sendVerificationEmail() отделно
                    require_once __DIR__ . '/includes/Email.php';
                    $site_url = getSetting($pdo, 'site_url', 'http://localhost');
                    $user_lang = $_SESSION['lang'] ?? 'en';
                    sendVerificationEmail($email, $username, $verification_token, $site_url, $user_lang);
                }

                $success = true;
            }
        } catch (Exception $e) {
            $error = "Грешка: " . htmlspecialchars($e->getMessage());
            error_log("REGISTRATION ERROR: " . $e->getMessage());
            // $error = $lang->get('registration_error');
        }
    }
}

require_once __DIR__ . '/templates/header.php';
?>

<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h3 class="text-center"><?= htmlspecialchars($lang->get('register')) ?></h3>
            </div>
            <div class="card-body">
                <?php if ($success): ?>
                    <div class="alert alert-success">
                        <?= htmlspecialchars($lang->get('registration_success')) ?><br>
                        <a href="login.php"><?= htmlspecialchars($lang->get('go_to_login')) ?></a>
                    </div>
                <?php else: ?>
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?= $error ?></div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label"><?= htmlspecialchars($lang->get('username')) ?> *</label>
                            <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" required minlength="3" maxlength="20">
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><?= htmlspecialchars($lang->get('email')) ?> *</label>
                            <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><?= htmlspecialchars($lang->get('password')) ?> *</label>
                            <input type="password" name="password" class="form-control" required minlength="6">
                            <div class="form-text"><?= htmlspecialchars($lang->get('password_help')) ?></div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><?= htmlspecialchars($lang->get('confirm_password')) ?> *</label>
                            <input type="password" name="password2" class="form-control" required>
                        </div>
                        <input type="hidden" name="register" value="1">
                        <button type="submit" class="btn btn-success w-100"><?= htmlspecialchars($lang->get('register')) ?></button>
                    </form>

                    <div class="text-center mt-3">
                        <a href="login.php"><?= htmlspecialchars($lang->get('already_have_account')) ?></a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/templates/footer.php';