<?php
session_start();
include(__DIR__ . '/../config.php');
include(__DIR__ . '/../header/header.php');

if (!isset($_GET['id'])) {
    echo "<div class='container py-5'><div class='alert alert-danger'>Product not found.</div></div>";
    include(__DIR__ . '/../footer/footer.php');
    exit();
}

$id = (int) $_GET['id'];

$stmt = $pdo->prepare("SELECT * FROM products WHERE id=?");
$stmt->execute([$id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$product) {
    echo "<div class='container py-5'><div class='alert alert-danger'>Product not found.</div></div>";
    include(__DIR__ . '/../footer/footer.php');
    exit();
}

$image_src = '../images/lapi.jpg';
if (!empty($product['image']) && file_exists(__DIR__ . '/../uploads/' . $product['image'])) {
    $image_src = '../uploads/' . $product['image'];
}
$stock = (int) ($product['stock'] ?? 0);
?>

<section class="py-5">
    <div class="container py-4">
        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger">
                <?= htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); ?>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success">
                <?= htmlspecialchars($_SESSION['success']); unset($_SESSION['success']); ?>
            </div>
        <?php endif; ?>

        <div class="product-detail-card">
            <div class="row g-4 align-items-center">
                <div class="col-lg-6">
                    <img src="<?= htmlspecialchars($image_src); ?>" class="product-detail-image" alt="<?= htmlspecialchars($product['name']); ?>">
                </div>

                <div class="col-lg-6">
                    <span class="stock-pill-static <?= $stock > 0 ? 'in-stock' : 'out-stock'; ?>">
                        <?= $stock > 0 ? $stock . ' in stock' : 'Out of stock'; ?>
                    </span>
                    <h2 class="fw-bold mt-3 mb-3"><?= htmlspecialchars($product['name']); ?></h2>
                    <p class="text-muted"><?= nl2br(htmlspecialchars($product['description'] ?? '')); ?></p>
                    <h3 class="text-primary fw-bold mt-4">KES <?= number_format($product['price']); ?></h3>

                    <form method="POST" action="cart.php" class="mt-4">
                        <input type="hidden" name="product_id" value="<?= $product['id']; ?>">

                        <?php if ($stock > 0): ?>
                            <div class="mb-3" style="max-width: 170px;">
                                <label class="form-label fw-semibold">Quantity</label>
                                <input type="number" name="quantity" value="1" min="1" max="<?= $stock; ?>" class="form-control">
                            </div>

                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-shopping-cart me-2"></i>Add to Cart
                            </button>
                        <?php else: ?>
                            <button class="btn btn-danger w-100" disabled>
                                <i class="fas fa-ban me-2"></i>Out of Stock
                            </button>
                        <?php endif; ?>
                    </form>

                    <a href="products.php" class="btn btn-outline-secondary mt-3">
                        <i class="fas fa-arrow-left me-2"></i>Back to Products
                    </a>
                </div>
            </div>
        </div>
    </div>
</section>

<?php include(__DIR__ . '/../footer/footer.php'); ?>
