<?php
require_once __DIR__ . '/../includes/role_middleware.php';
requireRole('Dealer');

require_once __DIR__ . '/../includes/header.php';
$pdo = db();
$dealerId = $_SESSION['user_id'];

// Stats
$myCars = $pdo->prepare("SELECT COUNT(*) FROM cars WHERE dealer_id = ?");
$myCars->execute([$dealerId]);
$myCarsCount = $myCars->fetchColumn();

$mySales = $pdo->prepare("SELECT COUNT(*) FROM sales s JOIN cars c ON s.car_id = c.id WHERE c.dealer_id = ?");
$mySales->execute([$dealerId]);
$mySalesCount = $mySales->fetchColumn();
?>

<div style="padding: 2rem;">
    <div class="page-header">
        <h1>Dealer Dashboard</h1>
        <span>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?></span>
    </div>

    <div class="card-grid" style="grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem;">
        <div class="stat-card" style="padding: 1.5rem; background: var(--card-bg); border: 1px solid var(--border-color); border-radius: var(--border-radius);">
            <div style="font-size: 2rem; font-weight: bold;"><?php echo $myCarsCount; ?></div>
            <div style="color: var(--text-secondary);">My Inventory</div>
        </div>
        <div class="stat-card" style="padding: 1.5rem; background: var(--card-bg); border: 1px solid var(--border-color); border-radius: var(--border-radius);">
            <div style="font-size: 2rem; font-weight: bold;"><?php echo $mySalesCount; ?></div>
            <div style="color: var(--text-secondary);">My Sales</div>
        </div>
    </div>

    <div style="background: var(--card-bg); padding: 2rem; border-radius: var(--border-radius); border: 1px solid var(--border-color);">
        <h3>Manage Inventory</h3>
        <p>You can add new cars to your branch inventory here.</p>
        <a href="/admin/cars.php" class="btn-sm btn-primary">Go to Inventory Management</a>
        <!-- Ideally this would point to a dealer-specific car management page, but reusing admin/cars.php with permission checks is a good MVP step if implemented, otherwise a dedicated page is better. For now, let's keep it simple. -->
    </div>
</div>

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