<?php
require_once 'includes/auth.php';

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

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

    if ($password !== $confirm_password) {
        $error = "Passwords do not match";
    } else {
        $pdo = db();
        // Check if username exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
        $stmt->execute([$username, $email]);
        if ($stmt->fetch()) {
            $error = "Username or email already exists";
        } else {
            // Register user
            $hash = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'Customer')");
            try {
                $stmt->execute([$username, $email, $hash]);
                $success = "Registration successful!";
            } catch (PDOException $e) {
                $error = "Registration failed: " . $e->getMessage();
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - Car Market</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
        .register-container {
            width: 100%;
            max-width: 400px;
            padding: 2.5rem;
            background: var(--card-bg);
            border-radius: var(--border-radius);
            box-shadow: 0 8px 32px rgba(0,0,0,0.4);
            border: 1px solid var(--border-color);
        }
        .form-group { margin-bottom: 1.2rem; }
        label { display: block; margin-bottom: 0.4rem; color: var(--text-secondary); font-size: 0.9rem; }
        input {
            width: 100%;
            padding: 0.8rem;
            background: var(--bg-color);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            color: var(--text-primary);
            font-size: 1rem;
        }
        input:focus { outline: none; border-color: var(--accent-color); }
        button {
            width: 100%;
            padding: 0.8rem;
            background: var(--accent-color);
            border: none;
            border-radius: 6px;
            color: var(--bg-color);
            font-weight: 700;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.2s;
            margin-top: 1rem;
        }
        button:hover { background: var(--accent-hover); transform: translateY(-1px); }
        .error {
            background: rgba(255, 68, 68, 0.1);
            color: #ff4444;
            padding: 0.8rem;
            border-radius: 6px;
            margin-bottom: 1.5rem;
            text-align: center;
            font-size: 0.9rem;
        }
        .success {
            background: rgba(0, 200, 81, 0.1);
            color: #00C851;
            padding: 0.8rem;
            border-radius: 6px;
            margin-bottom: 1.5rem;
            text-align: center;
            font-size: 0.9rem;
        }
        h2 { text-align: center; margin-bottom: 2rem; color: var(--accent-color); font-size: 1.8rem; }
        .links { text-align: center; margin-top: 1.5rem; font-size: 0.9rem; color: var(--text-secondary); }
        .links a { color: var(--accent-color); text-decoration: none; font-weight: 600; }
        .links a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="register-container">
        <h2>Create Account</h2>
        <?php if ($error): ?>
            <div class="error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        <?php if ($success): ?>
            <div class="success"><?php echo htmlspecialchars($success); ?></div>
            <p style="text-align: center;"><a href="login.php" class="btn" style="color: var(--accent-color);">Proceed to Login</a></p>
        <?php else: ?>
            <form method="POST">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" required>
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" required>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" required>
                </div>
                <div class="form-group">
                    <label>Confirm Password</label>
                    <input type="password" name="confirm_password" required>
                </div>
                <button type="submit">Sign Up</button>
                <div class="links">
                    Already have an account? <a href="login.php">Sign In</a><br>
                    <a href="/index.php" style="font-size: 0.8rem; opacity: 0.7;">Back to Home</a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
