<?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;">
    <div class="box" style="width: 100%; max-width: 500px; padding: 4rem;">
        <div style="text-align: center; margin-bottom: 3rem;">
            <h2 style="font-size: 2.2rem; font-weight: 900; margin-bottom: 0.5rem;">Join AfgCars</h2>
            <p style="color: var(--text-secondary);">Create your premium account today</p>
        </div>
        
        <?php if ($error): ?>
            <div class="glass" style="padding: 1rem; border-color: rgba(255, 71, 87, 0.3); background: rgba(255, 71, 87, 0.05); color: var(--danger); margin-bottom: 1.5rem; border-radius: 12px; font-size: 0.9rem; font-weight: 600; text-align: center;">
                <?= $error ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="form-group">
                <label style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">Full Name</label>
                <input type="text" name="name" class="form-control" required placeholder="John Doe" style="margin-top: 0.5rem;">
            </div>
            <div class="form-group">
                <label style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">Email Address</label>
                <input type="email" name="email" class="form-control" required placeholder="name@example.com" style="margin-top: 0.5rem;">
            </div>
            <div class="form-group">
                <label style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-secondary); font-weight: 700;">Password</label>
                <input type="password" name="password" class="form-control" required placeholder="••••••••" style="margin-top: 0.5rem;">
            </div>
            <button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1.5rem; padding: 1.1rem;">Create Account</button>
        </form>
        
        <p style="text-align: center; margin-top: 2.5rem; color: var(--text-secondary); font-size: 0.95rem;">
            Already have an account? <a href="login.php" style="color: var(--primary-color); font-weight: 700; text-decoration: none;">Sign in</a>
        </p>
    </div>
</div>

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