<?php
require_once 'auth.php';

if (is_logged_in()) {
    header('Location: app.php');
    exit;
}

$error = '';
$success = '';

if (isset($_GET['registered']) && $_GET['registered'] === 'true') {
    $success = 'Registration successful! You can now log in.';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = 'Please fill in all fields.';
    } else {
        try {
            if (login_user($username, $password)) {
                header('Location: app.php');
                exit;
            } else {
                $error = 'Invalid username or password.';
            }
        } catch (PDOException $e) {
            error_log("Database connection error during login: " . $e->getMessage());
            $error = "A server error occurred. Please try again later.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sign In - FinMox</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; }
    </style>
</head>
<body class="bg-white text-gray-900">

    <!-- HEADER -->
    <header class="max-w-7xl mx-auto px-6 py-5 flex items-center justify-between text-sm">
        <div class="font-bold text-xl">FinMox</div>
        <nav class="hidden md:flex gap-8">
            <a href="index.php" class="text-gray-600 hover:text-black font-semibold">Home</a>
            <a href="problem.php" class="text-gray-600 hover:text-black">Problem</a>
            <a href="product.php" class="text-gray-600 hover:text-black">Why FinMox</a>
            <a href="how_it_works.php" class="text-gray-600 hover:text-black">How It Works</a>
            <a href="roi.php" class="text-gray-600 hover:text-black">ROI</a>
            <a href="pricing.php" class="text-gray-600 hover:text-black">Pricing</a>
        </nav>
        <div class="flex items-center gap-4">
            <a href="login.php" class="text-gray-600 hover:text-black">Sign In</a>
            <a href="apply.php" class="bg-black text-white px-5 py-2.5 rounded-lg">Apply for Access</a>
        </div>
    </header>

    <!-- LOGIN FORM -->
    <main class="max-w-md mx-auto px-6 py-12">
        <div class="text-center">
            <h1 class="text-3xl font-bold">Sign In</h1>
            <p class="mt-2 text-gray-600">Access your FinMox workspace.</p>
        </div>

        <div class="mt-8">
            <?php if ($success): ?>
                <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
                    <span class="block sm:inline"><?php echo $success; ?></span>
                </div>
            <?php endif; ?>
            <?php if ($error): ?>
                <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
                    <span class="block sm:inline"><?php echo $error; ?></span>
                </div>
            <?php endif; ?>

            <form action="login.php" method="POST" class="space-y-6">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700">Username</label>
                    <div class="mt-1">
                        <input type="text" id="username" name="username" required class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
                    </div>
                </div>

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700">Password</label>
                    <div class="mt-1">
                        <input type="password" id="password" name="password" required class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
                    </div>
                </div>

                <div>
                    <button type="submit" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-black hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                        Sign In
                    </button>
                </div>
            </form>
            <div class="mt-6 text-center">
                <p class="text-sm">
                    Don't have an account?
                    <a href="apply.php" class="font-medium text-blue-600 hover:text-blue-500">
                        Apply for access
                    </a>
                </p>
            </div>
        </div>
    </main>

    <?php include '_footer.php'; ?>

</body>
</html>
