<?php

require_once __DIR__ . '/partials/header.php';

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

$error = null;
$success = null;

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

    if (empty($name) || empty($email) || empty($password)) {
        $error = 'All fields are required.';
    } elseif ($password !== $password_confirm) {
        $error = 'Passwords do not match.';
    } else {
        $pdo = db();
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = :email");
        $stmt->execute(['email' => $email]);
        if ($stmt->fetch()) {
            $error = 'Email already in use.';
        } else {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (:name, :email, :password, 'coach')");
            if ($stmt->execute(['name' => $name, 'email' => $email, 'password' => $hashed_password])) {
                $success = 'Registration successful! You can now <a href="login.php" class="fw-bold text-decoration-none">login</a>.';
            } else {
                $error = 'An error occurred. Please try again.';
            }
        }
    }
}
?>

<div class="container-fluid vh-100 d-flex justify-content-center align-items-center bg-light-gradient">
    <div class="col-11 col-sm-8 col-md-6 col-lg-5 col-xl-4">
        <div class="card shadow-lg border-0 rounded-4">
            <div class="card-body p-4 p-sm-5">
                <div class="text-center mb-4">
                    <h1 class="h2 fw-bold">Create Your Account</h1>
                    <p class="text-muted">Join Drillex to start creating and sharing drills.</p>
                </div>

                <?php if ($error) : ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>

                <?php if ($success) : ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php else : ?>
                    <form action="register.php" method="POST">
                        <div class="form-floating mb-3">
                            <input type="text" class="form-control" id="name" name="name" placeholder="Your Name" required>
                            <label for="name">Name</label>
                        </div>
                        <div class="form-floating mb-3">
                            <input type="email" class="form-control" id="email" name="email" placeholder="name@example.com" required>
                            <label for="email">Email address</label>
                        </div>
                        <div class="form-floating mb-3">
                            <input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
                            <label for="password">Password</label>
                        </div>
                        <div class="form-floating mb-4">
                            <input type="password" class="form-control" id="password_confirm" name="password_confirm" placeholder="Confirm Password" required>
                            <label for="password_confirm">Confirm Password</label>
                        </div>
                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary btn-lg rounded-pill">Create Account</button>
                        </div>
                    </form>
                <?php endif; ?>

                <div class="text-center mt-4">
                    <p class="text-muted mb-2">or</p>
                    <a href="hybridauth_login.php?provider=Google" class="btn btn-outline-dark btn-lg rounded-pill w-100">
                        <i class="bi bi-google me-2"></i> Sign in with Google
                    </a>
                </div>
                <div class="text-center mt-4">
                     <p class="mb-0">Already have an account? <a href="login.php" class="fw-bold text-decoration-none">Login</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/partials/footer.php'; ?>