<?php
/**
 * Product Badges Template Partial
 * 
 * @param object|array $product Product object or data
 */

if (!defined('ABSPATH')) exit;

$instinctor_active_addons = \Instinctor\Core\Plugin::getActiveAddons();
$instinctor_has_badges = true; // Product badges are always-on core feature

if ($instinctor_has_badges):

$instinctor_settings = \Instinctor\Services\ProductBadgeService::getSettings();
$instinctor_badges = [];

// Get product data - use local variable to avoid overwriting $product
$instinctor_product_data = is_object($product) ? (array) $product : $product;

$instinctor_product_id = $instinctor_product_data['id'] ?? 0;
$instinctor_price = floatval($instinctor_product_data['price'] ?? 0);
$instinctor_sale_price = floatval($instinctor_product_data['sale_price'] ?? 0);
$instinctor_stock_quantity = intval($instinctor_product_data['stock_quantity'] ?? 0);
$instinctor_created_at = $instinctor_product_data['created_at'] ?? '';
$instinctor_is_bestseller = !empty($instinctor_product_data['is_bestseller']);
$instinctor_custom_badge = $instinctor_product_data['custom_badge'] ?? '';
$instinctor_custom_badge_color = $instinctor_product_data['custom_badge_color'] ?? '#6b7280';
$instinctor_is_preorder = !empty($instinctor_product_data['is_preorder']);

$instinctor_is_featured = !empty($instinctor_product_data['is_featured']);

// Featured badge
if ($instinctor_is_featured) {
    $instinctor_badges[] = [
        'text' => $instinctor_settings['featured_text'] ?? 'Featured',
        'class' => 'instinctor-badge-featured',
    ];
}

// New badge
if (!empty($instinctor_settings['auto_new']) && $instinctor_created_at) {
    $instinctor_days_old = (time() - strtotime($instinctor_created_at)) / 86400;
    if ($instinctor_days_old <= ($instinctor_settings['new_days'] ?? 14)) {
        $instinctor_badges[] = [
            'text' => $instinctor_settings['new_text'] ?? 'New',
            'class' => 'instinctor-badge-new',
        ];
    }
}

// Sale badge
if (!empty($instinctor_settings['auto_sale']) && $instinctor_sale_price > 0 && $instinctor_sale_price < $instinctor_price) {
    if (!empty($instinctor_settings['sale_percent'])) {
        $instinctor_percent = round((($instinctor_price - $instinctor_sale_price) / $instinctor_price) * 100);
        $instinctor_badges[] = [
            'text' => '-' . $instinctor_percent . '%',
            'class' => 'instinctor-badge-sale',
        ];
    } else {
        $instinctor_badges[] = [
            'text' => $instinctor_settings['sale_text'] ?? 'Sale',
            'class' => 'instinctor-badge-sale',
        ];
    }
}

// Low stock badge
if (!empty($instinctor_settings['auto_low_stock']) && $instinctor_stock_quantity > 0) {
    $instinctor_threshold = $instinctor_settings['low_stock_threshold'] ?? 5;
    if ($instinctor_stock_quantity <= $instinctor_threshold) {
        $instinctor_text = $instinctor_settings['low_stock_text'] ?? 'Only %d left';
        $instinctor_badges[] = [
            'text' => sprintf($instinctor_text, $instinctor_stock_quantity),
            'class' => 'instinctor-badge-low-stock',
        ];
    }
}

// Pre-order badge
if ($instinctor_is_preorder) {
    $instinctor_badges[] = [
        'text' => 'Pre-Order',
        'class' => 'instinctor-badge-preorder',
    ];
}

// Bestseller badge
if ($instinctor_is_bestseller) {
    $instinctor_badges[] = [
        'text' => $instinctor_settings['bestseller_text'] ?? 'Best Seller',
        'class' => 'instinctor-badge-bestseller',
    ];
}

// Custom badge
if (!empty($instinctor_custom_badge)) {
    $instinctor_badges[] = [
        'text' => $instinctor_custom_badge,
        'class' => 'instinctor-badge-custom',
        'style' => 'background-color: ' . esc_attr($instinctor_custom_badge_color) . ';',
    ];
}

if (!empty($instinctor_badges)):
?>

<div class="instinctor-product-badges">
    <?php foreach ($instinctor_badges as $instinctor_badge): ?>
    <span class="instinctor-product-badge <?php echo esc_attr($instinctor_badge['class']); ?>"<?php if (!empty($instinctor_badge['style'])): ?> style="<?php echo esc_attr($instinctor_badge['style']); ?>"<?php endif; ?>>
        <?php echo esc_html($instinctor_badge['text']); ?>
    </span>
    <?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>
