<?php

require_once __DIR__ . '/app/helpers.php';
require_once __DIR__ . '/app/analytics.php';
require_once __DIR__ . '/app/public-layout.php';
require_once __DIR__ . '/app/shop-products.php';

$product_id = (int) ($_GET['id'] ?? 0);
$product = $product_id > 0 ? sparrow_get_shop_product($product_id) : null;

if (!$product || empty($product['is_active'])) {
    sparrow_public_header('Product Not Found', 'shop');

    ?>
    <section class="public-card public-hero">
        <h1>Product Not Found</h1>
        <p>
            This product may no longer be available.
        </p>

        <a class="product-soft-button" href="<?php echo sparrow_escape(sparrow_url('/sparrowseed/shop.php')); ?>">
            Back to Shop
        </a>
    </section>
    <?php

    sparrow_public_footer();
    exit;
}

sparrow_record_analytics_event(
    'page_view',
    'shop_product',
    (int) $product['id'],
    $product['product_name'] ?? '',
    'Shop Product',
    '/product.php?id=' . (int) $product['id']
);

$product_css = <<<'CSS'
.public-card h1,
.public-card h2,
.public-card h3 {
    color: var(--sparrow-purple-dark);
}

.product-top-actions {
    margin-bottom: 14px;
}

.product-detail-grid {
    display: grid;
    grid-template-columns: 420px 1fr;
    gap: 22px;
    align-items: start;
}

.product-detail-image,
.product-detail-placeholder {
    width: 100%;
    border-radius: 22px;
    border: 1px solid var(--sparrow-border);
    background: var(--sparrow-soft-pink);
    box-shadow: 0 8px 30px rgba(70, 35, 60, 0.06);
}

.product-detail-image {
    aspect-ratio: 1 / 1;
    object-fit: cover;
}

.product-detail-placeholder {
    aspect-ratio: 1 / 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--sparrow-muted);
    text-align: center;
    padding: 18px;
}

.product-detail-card {
    background: var(--sparrow-card);
    border: 1px solid var(--sparrow-border);
    border-radius: 22px;
    padding: 22px;
    box-shadow: 0 8px 30px rgba(70, 35, 60, 0.06);
}

.product-detail-card h1 {
    margin-top: 0;
    margin-bottom: 12px;
}

.product-meta-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

.product-pill {
    display: inline-flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 999px;
    border: 1px solid var(--sparrow-border);
    background: #ffffff;
    color: var(--sparrow-purple-dark);
    font-size: 13px;
    font-weight: bold;
}

.product-pill.price {
    background: var(--sparrow-soft-pink);
}

.product-description {
    color: var(--sparrow-muted);
    line-height: 1.65;
}

.product-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 18px;
}

.product-soft-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border-radius: 999px;
    background: var(--sparrow-soft-pink);
    border: 1px solid var(--sparrow-border);
    color: var(--sparrow-purple-dark);
    font-weight: bold;
    font-size: 13px;
    text-decoration: none;
}

.product-soft-button:hover {
    background: #ffffff;
    text-decoration: none;
}

@media (max-width: 850px) {
    .product-detail-grid {
        grid-template-columns: 1fr;
    }
}
CSS;

sparrow_public_header($product['product_name'], 'shop', $product_css);

?>

<div class="product-top-actions">
    <a class="product-soft-button" href="<?php echo sparrow_escape(sparrow_url('/sparrowseed/shop.php')); ?>">
        ← Back to Shop
    </a>
</div>

<section class="product-detail-grid">
    <div>
        <?php if (!empty($product['image_path'])): ?>
            <img
                class="product-detail-image"
                src="<?php echo sparrow_escape(sparrow_url($product['image_path'])); ?>"
                alt="<?php echo sparrow_escape($product['product_name']); ?>"
            >
        <?php else: ?>
            <div class="product-detail-placeholder">
                Product image coming soon
            </div>
        <?php endif; ?>
    </div>

    <article class="product-detail-card">
        <h1><?php echo sparrow_escape($product['product_name']); ?></h1>

        <div class="product-meta-row">
            <span class="product-pill price">
                $<?php echo sparrow_escape(number_format((float) $product['price'], 2)); ?>
            </span>

            <span class="product-pill">
                Qty: <?php echo (int) $product['quantity_available']; ?>
            </span>

            <?php if (!empty($product['category'])): ?>
                <span class="product-pill">
                    <?php echo sparrow_escape($product['category']); ?>
                </span>
            <?php endif; ?>
        </div>

        <?php if (!empty($product['description'])): ?>
            <div class="product-description">
                <?php echo nl2br(sparrow_escape($product['description'])); ?>
            </div>
        <?php else: ?>
            <p class="product-description">
                More details are coming soon for this product.
            </p>
        <?php endif; ?>

        <div class="product-actions">
            <a class="product-soft-button" href="<?php echo sparrow_escape(sparrow_url('/sparrowseed/order.php')); ?>">
                Ask About This Item
            </a>
        </div>
    </article>
</section>

<?php sparrow_public_footer(); ?>