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

class BundlePress_FBT_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'] !== 'fbt') {
            return $base_price;
        }

        $discount_type  = $bundle['settings']['discount_type'] ?? '';
        $discount_value = (float)($bundle['settings']['discount_value'] ?? 0);

        if ($discount_type === 'percentage') {
            return $base_price * (1 - ($discount_value / 100));
        } elseif ($discount_type === 'fixed') {
            return max(0, $base_price - $discount_value);
        }

        return $base_price;
    }
}
