<?php
/**
 * Product List Item Helper
 * Generates small product list item HTML following Cartzilla markup exactly
 */

function renderProductListItem($product, $b2bGroup = null) {
    require_once __DIR__ . '/Product.php';
    require_once __DIR__ . '/B2B.php';
    $productObj = new Product();
    $b2b = new B2B();
    
    // Get product price (with B2B discount if applicable)
    $b2bGroupId = ($b2bGroup && is_array($b2bGroup) && isset($b2bGroup['id'])) ? $b2bGroup['id'] : ($b2bGroup ? $b2bGroup : null);
    $price = $b2b->calculateProductPrice($product, $b2bGroupId);
    $hasSale = !empty($product['sale_price']) && $product['sale_price'] < $product['price'];
    
    // Get product image
    $image = $product['image'] ?? '/assets/img/shop/electronics/thumbs/01.png';
    if (!str_starts_with($image, 'http') && !str_starts_with($image, '/')) {
        if (str_starts_with($image, 'assets/')) {
            $image = '/' . $image;
        } else {
            $image = '/assets/img/shop/electronics/thumbs/' . $image;
        }
    }
    
    // Get average rating (placeholder - you can implement this)
    $avgRating = 4; // TODO: Calculate from reviews table
    $reviewCount = 0; // TODO: Get from reviews table
    
    // Generate stars HTML
    $starsHtml = '';
    for ($i = 1; $i <= 5; $i++) {
        if ($i <= floor($avgRating)) {
            $starsHtml .= '<i class="ci-star-filled text-warning"></i>';
        } elseif ($i == ceil($avgRating) && $avgRating != floor($avgRating)) {
            $starsHtml .= '<i class="ci-star-half text-warning"></i>';
        } else {
            $starsHtml .= '<i class="ci-star text-body-tertiary opacity-75"></i>';
        }
    }
    
    $productUrl = '/produs/' . htmlspecialchars($product['slug']);
    
    ob_start();
    ?>
            <!-- Item -->
            <div class="position-relative animate-underline d-flex align-items-center ps-xl-3">
              <div class="ratio ratio-1x1 flex-shrink-0" style="width: 110px">
                <img src="<?php echo htmlspecialchars($image); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
              </div>
              <div class="w-100 min-w-0 ps-2 ps-sm-3">
                <?php if ($reviewCount > 0): ?>
                <div class="d-flex align-items-center gap-2 mb-2">
                  <div class="d-flex gap-1 fs-xs">
                    <?php echo $starsHtml; ?>
                  </div>
                  <span class="text-body-tertiary fs-xs"><?php echo $reviewCount; ?></span>
                </div>
                <?php endif; ?>
                <h4 class="mb-2">
                  <a class="stretched-link d-block fs-sm fw-medium text-truncate" href="<?php echo $productUrl; ?>">
                    <span class="animate-target"><?php echo htmlspecialchars($product['name']); ?></span>
                  </a>
                </h4>
                <div class="h5 mb-0">
                  <?php echo number_format($price, 2); ?> RON
                  <?php if ($hasSale && $price == $product['sale_price']): ?>
                  <del class="text-body-tertiary fs-sm fw-normal"><?php echo number_format($product['price'], 2); ?> RON</del>
                  <?php endif; ?>
                </div>
              </div>
            </div>
    <?php
    return ob_get_clean();
}

