<?php
require_once '../admin/includes/branding.php';
require_once '../admin/includes/config.php';

if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in'] || $_SESSION['user_type'] !== 'driver') {
    header('Location: ../user/user_authentication.html');
    exit;
}

$pdo = getDBConnection();
$user_id = $_SESSION['user_id'];

// Get driver info
$stmt = $pdo->prepare("SELECT id FROM drivers WHERE user_id = ?");
$stmt->execute([$user_id]);
$driver = $stmt->fetch();

if (!$driver) {
    echo "Driver profile not found.";
    exit;
}

$driver_id = $driver['id'];

// Get assigned bookings
$stmt = $pdo->prepare("
    SELECT b.*, v.make, v.model, u.first_name as client_first_name, u.last_name as client_last_name
    FROM bookings b
    JOIN vehicles v ON b.vehicle_id = v.id
    JOIN users u ON b.user_id = u.id
    WHERE b.driver_id = ?
    ORDER BY b.pickup_date DESC
");
$stmt->execute([$driver_id]);
$bookings = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html class="dark" lang="en">
<head>
    <meta charset="utf-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title>Driver Dashboard - <?= htmlspecialchars(getBrandName()) ?></title>
    <script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:wght,FILL@100..700,0..1&display=swap" rel="stylesheet"/>
    <script src="../admin/includes/tailwind_config.js"></script>
    <link rel="stylesheet" href="../admin/includes/responsive.css">
</head>
<body class="bg-background-light dark:bg-background-dark text-gray-900 dark:text-white min-h-screen">
    <header class="sticky top-0 z-50 bg-background-light/80 dark:bg-background-dark/80 backdrop-blur-md border-b border-gray-200 dark:border-gray-800">
        <div class="flex items-center justify-between px-4 py-4 max-w-7xl mx-auto">
            <h1 class="text-clamp-lg font-bold">Driver Dashboard</h1>
            <a href="../user/logout.php" class="text-rose-500 text-sm font-bold">Logout</a>
        </div>
    </header>

    <main class="responsive-container p-4 py-8 space-y-6">
        <h2 class="text-clamp-xl font-bold">My Assigned Bookings</h2>

        <?php if (empty($bookings)): ?>
            <div class="text-center py-20 bg-gray-100 dark:bg-gray-800 rounded-2xl">
                <span class="material-symbols-outlined text-4xl text-gray-400">calendar_today</span>
                <p class="text-gray-500 mt-2">No bookings assigned yet.</p>
            </div>
        <?php else: ?>
            <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4 5xl:grid-cols-6 8xl:grid-cols-10 gap-4">
                <?php foreach ($bookings as $b): ?>
                    <div class="bg-white dark:bg-gray-900/50 rounded-2xl p-6 border border-gray-200 dark:border-gray-800 shadow-sm">
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <p class="text-[10px] text-gray-500 font-bold uppercase">Client</p>
                                <h3 class="font-bold"><?= htmlspecialchars($b['client_first_name'] . ' ' . $b['client_last_name']) ?></h3>
                            </div>
                            <span class="px-3 py-1 rounded-full text-[10px] font-bold uppercase bg-primary/10 text-primary">
                                <?= $b['booking_status'] ?>
                            </span>
                        </div>
                        <div class="grid grid-cols-2 gap-4 text-sm mb-6">
                            <div>
                                <p class="text-[10px] text-gray-500 font-bold uppercase">Vehicle</p>
                                <p><?= htmlspecialchars($b['make'] . ' ' . $b['model']) ?></p>
                            </div>
                            <div>
                                <p class="text-[10px] text-gray-500 font-bold uppercase">Pickup Date</p>
                                <p><?= date('M d, Y', strtotime($b['pickup_date'])) ?></p>
                            </div>
                        </div>
                        <a href="messages.php?booking_id=<?= $b['id'] ?>" class="w-full flex items-center justify-center gap-2 bg-primary text-white font-bold py-3 rounded-xl shadow-lg shadow-primary/30">
                            <span class="material-symbols-outlined text-sm">chat</span>
                            Chat with Client
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </main>
    <?php include '../admin/includes/floating_chat_include.php'; ?>
</body>
</html>
