<?php
session_start();
require_once "config.php";

// If already logged in, redirect
if (isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit;
}

$error = "";
$success = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $username = trim($_POST["username"] ?? "");
    $password = trim($_POST["password"] ?? "");
    $confirm  = trim($_POST["confirm"] ?? "");

    // Basic validation
    if ($username === "" || $password === "" || $confirm === "") {
        $error = "All fields are required.";
    } elseif ($password !== $confirm) {
        $error = "Passwords do not match.";
    } elseif (strlen($password) < 6) {
        $error = "Password must be at least 6 characters.";
    } else {

        // Check if username exists
        $stmt = $db->prepare("SELECT id FROM users WHERE username = ?");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            $error = "That username is already taken.";
        } else {

            // Create account
            $hash = password_hash($password, PASSWORD_DEFAULT);

            $stmt = $db->prepare("INSERT INTO users (username, password_hash) VALUES (?, ?)");
            $stmt->bind_param("ss", $username, $hash);

            if ($stmt->execute()) {

                // Auto-login
                $user_id = $stmt->insert_id;

                $_SESSION["user_id"] = $user_id;
                $_SESSION["username"] = $username;
                $_SESSION["is_admin"] = 0;

                header("Location: index.php");
                exit;

            } else {
                $error = "Something went wrong creating your account.";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Account</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="bg-gray-900 text-white flex items-center justify-center min-h-screen">

<div class="bg-gray-800 p-8 rounded-xl shadow-xl w-full max-w-md border border-pink-500/30">

    <h1 class="text-3xl font-bold text-center mb-6 text-pink-400">Create Account</h1>

    <?php if ($error): ?>
        <div class="bg-red-600/20 border border-red-500/50 text-red-300 p-3 rounded mb-4">
            <?= htmlspecialchars($error) ?>
        </div>
    <?php endif; ?>

    <form method="POST" class="space-y-4">

        <div>
            <label class="block mb-1 text-sm text-gray-300">Username</label>
            <input type="text" name="username" class="w-full px-4 py-2 rounded bg-gray-700 border border-gray-600 focus:border-pink-500 outline-none">
        </div>

        <div>
            <label class="block mb-1 text-sm text-gray-300">Password</label>
            <input type="password" name="password" class="w-full px-4 py-2 rounded bg-gray-700 border border-gray-600 focus:border-pink-500 outline-none">
        </div>

        <div>
            <label class="block mb-1 text-sm text-gray-300">Confirm Password</label>
            <input type="password" name="confirm" class="w-full px-4 py-2 rounded bg-gray-700 border border-gray-600 focus:border-pink-500 outline-none">
        </div>

        <button class="w-full py-2 bg-pink-600 hover:bg-pink-500 rounded font-bold transition">
            Create Account
        </button>

    </form>

    <p class="text-center text-sm text-gray-400 mt-4">
        Already have an account?
        <a href="login.php" class="text-pink-400 hover:underline">Log in</a>
    </p>

</div>

</body>
</html>