<?php
/**
 * Volume Discounts Display Template Partial
 * Shows available volume discount tiers for a product.
 * @param int $product_id Product ID
 */
if (!defined('ABSPATH')) exit;

$instinctor_product_id = $product_id ?? get_the_ID();
$instinctor_tiers = \Instinctor\Services\VolumeDiscountService::getProductTiers($instinctor_product_id);

if (!empty($instinctor_tiers)):
    usort($instinctor_tiers, function($a, $b) {
        return $a['min_quantity'] - $b['min_quantity'];
    });
    $instinctor_tiers_data = array_map(function($tier) {
        return ['min' => intval($tier['min_quantity']), 'discount' => floatval($tier['discount_percent'])];
    }, $instinctor_tiers);
?>
<div class="instinctor-volume-discount"
     data-product-id="<?php echo esc_attr($instinctor_product_id); ?>"
     data-tiers="<?php echo esc_attr(wp_json_encode($instinctor_tiers_data)); ?>">
    <div class="instinctor-volume-tiers">
        <?php foreach ($instinctor_tiers as $instinctor_tier): ?>
        <span class="instinctor-volume-tier" data-min="<?php echo esc_attr($instinctor_tier['min_quantity']); ?>">
            <?php echo esc_html($instinctor_tier['min_quantity']); ?>+ &mdash; <?php echo esc_html($instinctor_tier['discount_percent']); ?>% off
        </span>
        <?php endforeach; ?>
    </div>
    <div class="instinctor-current-discount" style="display:none;"></div>
    <div class="instinctor-next-tier" style="display:none;"></div>
</div>
<?php endif; ?>
