<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/header.php';

$id = $_GET['id'] ?? 0;
$car = null;

try {
    $db = db();
    $stmt = $db->prepare("SELECT c.*, b.name as branch_name, b.address as branch_address, b.phone as branch_phone 
                          FROM cars c 
                          LEFT JOIN branches b ON c.branch_id = b.id 
                          WHERE c.id = ?");
    $stmt->execute([$id]);
    $car = $stmt->fetch();
} catch (Exception $e) {
    // Log error if needed
}

if (!$car) {
    echo "<div class='container' style='padding: 4rem; text-align: center;'>
            <h2>Car not found</h2>
            <p>The vehicle you are looking for does not exist or has been removed.</p>
            <a href='marketplace.php' class='btn'>Browse Inventory</a>
          </div>";
    require_once __DIR__ . '/includes/footer.php';
    exit;
}
?>

<section class="container" style="padding-top: 4rem;">
    <div class="grid" style="grid-template-columns: 1.5fr 1fr; gap: 3rem; align-items: start;">
        
        <!-- Left Column: Images -->
        <div>
            <div class="car-image" style="height: 500px; margin-bottom: 1rem;">
                <?php if ($car['is_featured']): ?>
                    <span class="car-badge">FEATURED</span>
                <?php endif; ?>
                <img src="<?= htmlspecialchars($car['image_url']) ?>" alt="<?= htmlspecialchars($car['brand']) ?>" style="width: 100%; height: 100%; object-fit: cover; border-radius: var(--border-radius);">
            </div>
            <!-- Placeholder for additional gallery images if we had them -->
            <div class="grid" style="grid-template-columns: repeat(4, 1fr); gap: 1rem;">
                <!-- Just repeating main image for gallery effect since we only have one -->
                <img src="<?= htmlspecialchars($car['image_url']) ?>" style="height: 80px; width: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; opacity: 0.7;">
                <img src="<?= htmlspecialchars($car['image_url']) ?>" style="height: 80px; width: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; opacity: 0.7;">
                <img src="<?= htmlspecialchars($car['image_url']) ?>" style="height: 80px; width: 100%; object-fit: cover; border-radius: 8px; cursor: pointer; opacity: 0.7;">
            </div>
        </div>

        <!-- Right Column: Details -->
        <div>
            <div style="margin-bottom: 1rem; color: var(--accent-color); font-weight: 700; text-transform: uppercase; letter-spacing: 1px;">
                <?= htmlspecialchars($car['status']) ?>
            </div>
            
            <h1 style="font-size: 2.5rem; margin-bottom: 0.5rem; line-height: 1.2;">
                <?= htmlspecialchars($car['year'] . ' ' . $car['brand'] . ' ' . $car['model']) ?>
            </h1>
            
            <div style="font-size: 2rem; font-weight: 800; color: var(--accent-color); margin-bottom: 2rem;">
                $<?= number_format((float)$car['price'], 0) ?>
            </div>

            <div style="background: var(--surface-color); padding: 2rem; border-radius: var(--border-radius); border: 1px solid var(--border-color); margin-bottom: 2rem;">
                <h3 style="margin-bottom: 1.5rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem;">Vehicle Specifications</h3>
                
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem;">
                    <div>
                        <div style="color: var(--text-secondary); font-size: 0.9rem;">Mileage</div>
                        <div style="font-weight: 600;"><?= number_format((float)$car['mileage'], 0) ?> km</div>
                    </div>
                    <div>
                        <div style="color: var(--text-secondary); font-size: 0.9rem;">Transmission</div>
                        <div style="font-weight: 600;"><?= htmlspecialchars($car['transmission'] ?? 'Automatic') ?></div>
                    </div>
                    <div>
                        <div style="color: var(--text-secondary); font-size: 0.9rem;">Fuel Type</div>
                        <div style="font-weight: 600;"><?= htmlspecialchars($car['fuel_type'] ?? 'Petrol') ?></div>
                    </div>
                    <div>
                        <div style="color: var(--text-secondary); font-size: 0.9rem;">VIN</div>
                        <div style="font-weight: 600;"><?= htmlspecialchars($car['vin']) ?></div>
                    </div>
                    <div>
                        <div style="color: var(--text-secondary); font-size: 0.9rem;">Location</div>
                        <div style="font-weight: 600;"><?= htmlspecialchars($car['branch_name'] ?? 'Main Showroom') ?></div>
                    </div>
                </div>
            </div>

            <?php if ($car['installment_available']): ?>
            <div style="background: rgba(255, 215, 0, 0.1); padding: 1.5rem; border-radius: var(--border-radius); border: 1px solid var(--accent-color); margin-bottom: 2rem;">
                <h3 style="color: var(--accent-color); margin-bottom: 0.5rem;"><i class="fas fa-calculator"></i> Installment Plan</h3>
                <p style="margin-bottom: 1rem; font-size: 0.9rem;">Flexible financing options available for this vehicle.</p>
                <div style="font-size: 1.2rem; font-weight: 700;">
                    Est. $<?= number_format($car['price'] / 60, 0) ?> / month
                    <span style="font-size: 0.9rem; font-weight: 400; color: var(--text-secondary);">(60 months)</span>
                </div>
            </div>
            <?php endif; ?>

            <div style="display: flex; gap: 1rem;">
                <a href="contact.php?subject=Purchase%20Inquiry%20-%20<?= urlencode($car['brand'] . ' ' . $car['model']) ?>&car_id=<?= $car['id'] ?>" class="btn" style="flex: 1; text-align: center;">Request to Buy</a>
                <?php if ($car['installment_available']): ?>
                    <a href="contact.php?subject=Installment%20Inquiry%20-%20<?= urlencode($car['brand'] . ' ' . $car['model']) ?>&car_id=<?= $car['id'] ?>" class="btn btn-outline" style="flex: 1; text-align: center;">Apply for Installment</a>
                <?php endif; ?>
            </div>
            
            <p style="margin-top: 1.5rem; font-size: 0.8rem; color: var(--text-secondary); text-align: center;">
                * Price excludes tax and registration fees. Contact us for final pricing.
            </p>
        </div>
    </div>
</section>

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