<?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 = '';

if ($_POST['login'] ?? false) {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = $lang->get('fill_all_fields');
    } else {
        // Първо проверяваме дали потребителът съществува и паролата е вярна
        $stmt = $pdo->prepare("SELECT id, username, password, email_verified FROM users WHERE username = ? OR email = ?");
        $stmt->execute([$username, $username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            // 🔹 КЛЮЧОВА ПОПРАВКА:
            // Ако email_verified е NULL или липсва → считаме го за 1 (потвърден)
            // Само ако е изрично 0 → изисква потвърждение
            $email_verified = (int)($user['email_verified'] ?? 1);

            if (getSetting($pdo, 'email_verification', 'disabled') === 'enabled' && $email_verified === 0) {
                $error = $lang->get('email_not_verified');
            } else {
                // Ръчно логин
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                $userData = $pdo->prepare("SELECT `rank`, `language`, `style` FROM users WHERE id = ?");
                $userData->execute([$user['id']]);
                $data = $userData->fetch();
                $_SESSION['rank'] = $data['rank'];
                $_SESSION['lang'] = $data['language'] ?? 'en';
                $_SESSION['style'] = $data['style'] ?? 'light';

                $pdo->prepare("UPDATE users SET last_login = NOW() WHERE id = ?")->execute([$user['id']]);

                header("Location: index.php");
                exit;
            }
        } else {
            $error = $lang->get('invalid_login');
        }
    }
}

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"><?= $lang->get('login') ?></h3>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?= $error ?></div>
                <?php endif; ?>

                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label"><?= $lang->get('username') ?></label>
                        <input type="text" name="username" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?= $lang->get('password') ?></label>
                        <input type="password" name="password" class="form-control" required>
                    </div>
                    <input type="hidden" name="login" value="1">
                    <button type="submit" class="btn btn-primary w-100"><?= $lang->get('login') ?></button>
                </form>

                <div class="text-center mt-3">
                    <a href="register.php"><?= $lang->get('no_account_register') ?></a>
                </div>
            </div>
        </div>
    </div>
</div>

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