<?php
/**
 * Product Card Helper
 * Generates product card HTML following Cartzilla markup exactly
 */

function renderProductCard($product, $b2bGroup = null) {
    require_once __DIR__ . '/Product.php';
    require_once __DIR__ . '/User.php';
    $productObj = new Product();
    $userObj = new User();
    
    // Get product price (with B2B discount if applicable)
    $price = $productObj->getProductPrice($product, $b2bGroup);
    $originalPrice = $product['sale_price'] ?? $product['price'];
    $hasSale = !empty($product['sale_price']) && $product['sale_price'] < $product['price'];
    
    // Calculate discount percentage
    $discountPercent = 0;
    if ($hasSale) {
        $discountPercent = round((($product['price'] - $product['sale_price']) / $product['price']) * 100);
    }
    
    // Get product image
    $image = $product['image'] ?? '/assets/img/shop/electronics/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/' . $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="col">
                <div class="product-card animate-underline hover-effect-opacity bg-body rounded">
                  <div class="position-relative">
                    <div class="position-absolute top-0 end-0 z-2 hover-effect-target opacity-0 mt-3 me-3">
                      <div class="d-flex flex-column gap-2">
                        <button type="button" class="btn btn-icon btn-secondary animate-pulse d-none d-lg-inline-flex add-to-wishlist" data-product-id="<?php echo $product['id']; ?>" aria-label="Adaugă la favorite">
                          <i class="ci-heart fs-base animate-target"></i>
                        </button>
                        <button type="button" class="btn btn-icon btn-secondary animate-rotate d-none d-lg-inline-flex" aria-label="Compară">
                          <i class="ci-refresh-cw fs-base animate-target"></i>
                        </button>
                      </div>
                    </div>
                    <div class="dropdown d-lg-none position-absolute top-0 end-0 z-2 mt-2 me-2">
                      <button type="button" class="btn btn-icon btn-sm btn-secondary bg-body" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Mai multe acțiuni">
                        <i class="ci-more-vertical fs-lg"></i>
                      </button>
                      <ul class="dropdown-menu dropdown-menu-end fs-xs p-2" style="min-width: auto">
                        <li>
                          <a class="dropdown-item add-to-wishlist" href="#!" data-product-id="<?php echo $product['id']; ?>">
                            <i class="ci-heart fs-sm ms-n1 me-2"></i>
                            Adaugă la favorite
                          </a>
                        </li>
                        <li>
                          <a class="dropdown-item" href="#!">
                            <i class="ci-refresh-cw fs-sm ms-n1 me-2"></i>
                            Compară
                          </a>
                        </li>
                      </ul>
                    </div>
                    <a class="d-block rounded-top overflow-hidden p-3 p-sm-4" href="<?php echo $productUrl; ?>">
                      <?php if ($discountPercent > 0): ?>
                      <span class="badge bg-danger position-absolute top-0 start-0 mt-2 ms-2 mt-lg-3 ms-lg-3">-<?php echo $discountPercent; ?>%</span>
                      <?php endif; ?>
                      <div class="ratio" style="--cz-aspect-ratio: calc(240 / 258 * 100%)">
                        <img src="<?php echo htmlspecialchars($image); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                      </div>
                    </a>
                  </div>
                  <div class="w-100 min-w-0 px-1 pb-2 px-sm-3 pb-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; ?>
                    <h3 class="pb-1 mb-2">
                      <a class="d-block fs-sm fw-medium text-truncate" href="<?php echo $productUrl; ?>">
                        <span class="animate-target"><?php echo htmlspecialchars($product['name']); ?></span>
                      </a>
                    </h3>
                    <div class="d-flex align-items-center justify-content-between">
                      <div class="h5 lh-1 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>
                      <button type="button" class="product-card-button btn btn-icon btn-secondary animate-slide-end ms-2 add-to-cart" data-product-id="<?php echo $product['id']; ?>" aria-label="Adaugă în coș">
                        <i class="ci-shopping-cart fs-base animate-target"></i>
                      </button>
                    </div>
                  </div>
                  <?php if (!empty($product['short_description'])): ?>
                  <div class="product-card-details position-absolute top-100 start-0 w-100 bg-body rounded-bottom shadow mt-n2 p-3 pt-1">
                    <span class="position-absolute top-0 start-0 w-100 bg-body mt-n2 py-2"></span>
                    <p class="fs-xs mb-0"><?php echo htmlspecialchars(mb_substr($product['short_description'], 0, 100)); ?><?php echo mb_strlen($product['short_description']) > 100 ? '...' : ''; ?></p>
                  </div>
                  <?php endif; ?>
                </div>
              </div>
    <?php
    return ob_get_clean();
}

