<?php
defined('ABSPATH') || exit;

class BundlePress_Volume_Handler
{
    public static function get_price($product_id, $bundle_id, $cart_item, $cart = null)
    {
        $fresh_product = wc_get_product($product_id);
        if (!$fresh_product) return (float) $cart_item['data']->get_regular_price();
        $base_price = (float) $fresh_product->get_price();

        $bundle = BundlePress_DB::get_bundle_data($bundle_id);
        if (!$bundle || $bundle['type'] !== 'volume') {
            return $base_price;
        }

        // --- Calculate Total Bundle Quantity in Cart ---
        $total_bundle_qty = 0;
        $parent_key = isset($cart_item['_bundlepress_parent']) ? $cart_item['_bundlepress_parent'] : '';
        $item_key   = isset($cart_item['key']) ? $cart_item['key'] : '';

        if ($cart) {
            foreach ($cart->get_cart() as $inner_key => $inner_item) {
                $inner_bundle_id = 0;
                if (isset($inner_item['_bundlepress_bundle'])) {
                    $inner_bundle_id = absint($inner_item['_bundlepress_bundle']);
                } elseif (isset($inner_item['_bundlepress_parent'])) {
                    // This logic depends on BundlePress_Bundle_Display::get_bundle_id_for_item
                    // But here we can just check if they share the same parent or if it refers to this item
                    if ($inner_item['_bundlepress_parent'] === $parent_key || $inner_key === $parent_key || $inner_item['_bundlepress_parent'] === $item_key) {
                        $inner_bundle_id = $bundle_id;
                    }
                }

                if ($inner_bundle_id === (int) $bundle_id) {
                    $total_bundle_qty += $inner_item['quantity'];
                }
            }
        } else {
            $total_bundle_qty = (int) $cart_item['quantity'];
        }

        $tiers = isset($bundle['settings']['tiers']) ? $bundle['settings']['tiers'] : [];
        
        // Find the best matching tier for the TOTAL quantity
        $best_tier    = null;
        $best_min_qty = 0;

        foreach ($tiers as $tier) {
            $min_qty = isset($tier['min_qty']) ? absint($tier['min_qty']) : 1;
            if ($total_bundle_qty >= $min_qty && $min_qty >= $best_min_qty) {
                $best_min_qty = $min_qty;
                $best_tier    = $tier;
            }
        }

        if (!$best_tier || empty($best_tier['discount']) || (float) $best_tier['discount'] <= 0) {
            return $base_price;
        }

        $discount      = (float) $best_tier['discount'];
        $discount_type = isset($best_tier['discount_type']) ? $best_tier['discount_type'] : 'percentage';

        if ($discount_type === 'fixed') {
            // Fixed amount is spread across ALL units in the bundle group: total discount ÷ total_bundle_qty
            $discount_per_item = $discount / max(1, $total_bundle_qty);
            return max(0, $base_price - $discount_per_item);
        }

        // Percentage discount
        return $base_price * (1 - ($discount / 100));
    }
}
