<?php
// includes/item-card.php - FIXED TO WORK WITH YOUR EXISTING ITEM MODEL
// Usage: Include this file inside a loop where $item is available

$item_url = 'item-details.php?slug=' . $item['slug'];
$rating = $item['average_rating'] ?? 0; // Your model uses average_rating
$reviews_count = $item['total_reviews'] ?? 0; // Your model uses total_reviews

// Build location text
$location_parts = array_filter([
    $item['location'] ?? '',
    $item['city'] ?? '',
    $item['state'] ?? ''
]);
$location_text = implode(', ', $location_parts);

// Get first image from JSON images array
$images = json_decode($item['images'] ?? '[]', true);
$main_image = !empty($images) ? $images[0] : 'assets/images/default-item.jpg';

// Get price
$price_per_day = $item['daily_rate'] ?? $item['price_per_day'] ?? 0;

// Check if featured
$is_featured = $item['featured'] ?? $item['is_featured'] ?? 0;
?>

<div class="card border-0 shadow-sm rounded-4 h-100 card-hover">
    <!-- Item Image -->
    <div class="position-relative overflow-hidden rounded-top-4">
        <a href="<?= $item_url ?>">
            <img src="<?= $main_image ?>" alt="<?= htmlspecialchars($item['title']) ?>" 
                 class="card-img-top" style="height: 220px; object-fit: cover;">
        </a>
        
        <!-- Favorite Button -->
        <button class="btn btn-sm btn-light rounded-circle position-absolute top-0 end-0 m-3 favorite-btn" 
                data-item-id="<?= $item['id'] ?>"
                style="width: 40px; height: 40px;">
            <i class="far fa-heart"></i>
        </button>
        
        <!-- Featured Badge -->
        <?php if ($is_featured): ?>
        <span class="badge bg-warning text-dark position-absolute top-0 start-0 m-3">
            <i class="fas fa-star me-1"></i>Featured
        </span>
        <?php endif; ?>
        
        <!-- Price Badge -->
        <div class="position-absolute bottom-0 start-0 m-3">
            <span class="badge bg-primary fs-6 px-3 py-2">
                ₦<?= number_format($price_per_day, 0) ?>/day
            </span>
        </div>
    </div>
    
    <div class="card-body">
        <!-- Category -->
        <div class="mb-2">
            <a href="items.php?category=<?= $item['category_id'] ?>" 
               class="badge bg-light text-primary text-decoration-none">
                <?= htmlspecialchars($item['category_name']) ?>
            </a>
        </div>
        
        <!-- Title -->
        <h5 class="card-title mb-2">
            <a href="<?= $item_url ?>" class="text-dark text-decoration-none" 
               title="<?= htmlspecialchars($item['title']) ?>">
                <?= strlen($item['title']) > 50 ? substr(htmlspecialchars($item['title']), 0, 50) . '...' : htmlspecialchars($item['title']) ?>
            </a>
        </h5>
        
        <!-- Rating -->
        <div class="mb-2">
            <?php if ($rating > 0): ?>
            <div class="d-flex align-items-center gap-1">
                <div class="text-warning">
                    <?php for ($i = 1; $i <= 5; $i++): ?>
                        <?php if ($i <= floor($rating)): ?>
                            <i class="fas fa-star"></i>
                        <?php elseif ($i - 0.5 <= $rating): ?>
                            <i class="fas fa-star-half-alt"></i>
                        <?php else: ?>
                            <i class="far fa-star"></i>
                        <?php endif; ?>
                    <?php endfor; ?>
                </div>
                <span class="text-muted small">
                    <?= number_format($rating, 1) ?> (<?= $reviews_count ?>)
                </span>
            </div>
            <?php else: ?>
            <div class="text-muted small">
                <i class="far fa-star"></i> No reviews yet
            </div>
            <?php endif; ?>
        </div>
        
        <!-- Location -->
        <div class="text-muted small mb-2">
            <i class="fas fa-map-marker-alt me-1"></i>
            <?= $location_text ? htmlspecialchars($location_text) : 'Location not specified' ?>
        </div>
        
        <!-- Vendor -->
        <div class="text-muted small mb-3">
            <i class="fas fa-store me-1"></i>
            <?= htmlspecialchars($item['vendor_name']) ?>
        </div>
        
        <!-- View Button -->
        <a href="<?= $item_url ?>" class="btn btn-outline-primary btn-sm w-100">
            View Details <i class="fas fa-arrow-right ms-1"></i>
        </a>
    </div>
</div>