<?php
session_start();
require_once __DIR__ . '/db/config.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

if (($_SESSION['role'] ?? '') === 'admin') {
    header('Location: admin_dashboard.php');
    exit;
}

$pdo = db();
$userId = $_SESSION['user_id'];

// Fetch user's purchases
$purchases = $pdo->prepare("
    SELECT p.*, c.brand, c.model, c.year, c.price 
    FROM purchases p 
    JOIN cars c ON p.car_id = c.id 
    WHERE p.user_id = ? 
    ORDER BY p.created_at DESC
");
$purchases->execute([$userId]);
$myPurchases = $purchases->fetchAll();

// Fetch user's listings
$listings = $pdo->prepare("
    SELECT * FROM cars 
    WHERE user_id = ? AND deleted_at IS NULL 
    ORDER BY created_at DESC
");
$listings->execute([$userId]);
$myCars = $listings->fetchAll();

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

<div class="container" style="padding-top: 3rem;">
    <div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 3rem; border-bottom: 1px solid var(--glass-border); padding-bottom: 2rem;">
        <div>
            <h1 style="font-size: 2.5rem; font-weight: 900;">Welcome, <?= htmlspecialchars($_SESSION['user_name']) ?></h1>
            <p style="color: var(--text-secondary);">Manage your car listings and view your purchase history.</p>
        </div>
        <a href="logout.php" class="btn btn-outline" style="border-color: var(--danger); color: var(--danger);">Sign Out</a>
    </div>

    <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 3rem;">
        <!-- Listings -->
        <div>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
                <h2 style="font-weight: 800;">My Listings</h2>
                <a href="add_car.php" class="btn btn-primary btn-sm" style="padding: 0.6rem 1.2rem; font-size: 0.8rem;">+ List New Car</a>
            </div>
            
            <?php if (empty($myCars)): ?>
                <div class="glass" style="padding: 4rem; text-align: center;">
                    <p style="color: var(--text-secondary); margin-bottom: 1.5rem;">You haven't listed any cars yet.</p>
                    <a href="add_car.php" class="btn btn-outline">Start Selling</a>
                </div>
            <?php else: ?>
                <div class="grid" style="grid-template-columns: 1fr;">
                    <?php foreach ($myCars as $car): ?>
                        <div class="glass" style="padding: 1.5rem; display: flex; justify-content: space-between; align-items: center;">
                            <div>
                                <h3 style="margin-bottom: 0.3rem;"><?= htmlspecialchars($car['brand'] . ' ' . $car['model']) ?></h3>
                                <span class="badge badge-<?= $car['status'] === 'approved' ? 'success' : ($car['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                    <?= ucfirst($car['status']) ?>
                                </span>
                                <span style="margin-left: 1rem; color: var(--text-secondary); font-size: 0.9rem;">$<?= number_format($car['price']) ?></span>
                            </div>
                            <div style="display: flex; gap: 1rem;">
                                <a href="car_detail.php?id=<?= $car['id'] ?>" class="btn btn-outline" style="padding: 0.5rem 1rem; font-size: 0.8rem;">View</a>
                                <a href="edit_car.php?id=<?= $car['id'] ?>" class="btn btn-auth" style="padding: 0.5rem 1rem; font-size: 0.8rem;">Edit</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Purchases -->
        <div>
            <h2 style="font-weight: 800; margin-bottom: 2rem;">Recent Activity</h2>
            <div class="glass" style="padding: 2rem;">
                <h4 style="margin-bottom: 1.5rem; color: var(--primary-color);">Purchase History</h4>
                <?php if (empty($myPurchases)): ?>
                    <p style="color: var(--text-secondary); font-size: 0.9rem;">No purchases found.</p>
                <?php else: ?>
                    <div style="display: flex; flex-direction: column; gap: 1.5rem;">
                        <?php foreach ($myPurchases as $p): ?>
                            <div style="border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 1rem;">
                                <div style="display: flex; justify-content: space-between; margin-bottom: 0.3rem;">
                                    <span style="font-weight: 700; font-size: 0.9rem;"><?= htmlspecialchars($p['brand'] . ' ' . $p['model']) ?></span>
                                    <span style="font-size: 0.8rem; color: var(--primary-color); font-weight: 700;">$<?= number_format($p['price']) ?></span>
                                </div>
                                <div style="display: flex; justify-content: space-between; align-items: center;">
                                    <span style="font-size: 0.75rem; color: var(--text-secondary);"><?= date('M d, Y', strtotime($p['created_at'])) ?></span>
                                    <span class="badge badge-success" style="font-size: 0.65rem;">Completed</span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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