<?php
require_once __DIR__ . '/includes/header.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $pdo = db();
    // Check if user exists
    $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
    $stmt->execute([$email]);
    if ($stmt->fetch()) {
        $error = "Email already registered.";
    } else {
        $hashed = password_hash($password, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
        if ($stmt->execute([$name, $email, $hashed])) {
            $_SESSION['user_id'] = $pdo->lastInsertId();
            $_SESSION['user_name'] = $name;
            $_SESSION['user_email'] = $email;
            $_SESSION['role'] = 'user';
            header('Location: dashboard.php');
            exit;
        } else {
            $error = "Registration failed. Please try again.";
        }
    }
}
?>

<div class="container" style="display: flex; justify-content: center; align-items: center; min-height: 80vh; padding: 4rem 0;">
    <div class="box" style="width: 100%; max-width: 550px; padding: 4.5rem;">
        <div class="text-center mb-3">
            <h2 class="fw-black" style="font-size: 2.8rem; color: #fff; margin-bottom: 0.5rem; line-height: 1.1;">Join AfgCars</h2>
            <p class="text-secondary fw-bold" style="font-size: 1.1rem;">Create your premium automotive portal today</p>
        </div>
        
        <?php if ($error): ?>
            <div class="alert alert-error mb-2 text-center" style="justify-content: center;">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Full Legal Name</label>
                <input type="text" name="name" class="form-control" required placeholder="John Doe">
            </div>
            <div class="form-group">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Email Address</label>
                <input type="email" name="email" class="form-control" required placeholder="name@example.com">
            </div>
            <div class="form-group">
                <label class="text-secondary fw-black mb-1" style="text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;">Security Password</label>
                <input type="password" name="password" class="form-control" required placeholder="••••••••">
            </div>
            <button type="submit" class="btn btn-primary w-full mt-2 fw-black" style="padding: 1.3rem; font-size: 1.1rem; letter-spacing: 1px;">CREATE PREMIUM ACCOUNT</button>
        </form>
        
        <p class="text-center mt-3 text-secondary fw-bold" style="font-size: 1rem;">
            Already registered? <a href="login.php" class="text-gold" style="text-decoration: none; border-bottom: 1px solid var(--primary-color);">Sign in here</a>
        </p>
    </div>
</div>

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