<?php

if ( ! defined( 'ABSPATH' ) ) { return; }

if ( ! function_exists( 'is_singular' ) || ! is_singular( 'product' ) ) { return; }
if ( ! class_exists( 'AIEO_DMM_Free_Shipping' ) ) { return; }
if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return; }

$module = new AIEO_DMM_Free_Shipping();
$s      = $module->get_settings();

if ( ($s['enabled'] ?? 'yes') !== 'yes' )       { return; }
if ( ($s['display_product'] ?? 'no') !== 'yes' ) { return; }

$threshold = (float) ( $s['free_shipping_threshold'] ?? 0 );
if ( $threshold <= 0 ) { return; }

global $product;
$pid = ( $product instanceof WC_Product ) ? (int) $product->get_id() : (int) get_the_ID();
if ( $pid <= 0 ) { return; }

if ( class_exists( 'AIEO_DMM_Pricing_Engine' ) ) {
    $engine = AIEO_DMM_Pricing_Engine::instance();
    if ( method_exists( $engine, 'is_excluded_from_free_shipping' )
         && $engine->is_excluded_from_free_shipping( $pid ) ) {
        return;
    }
}

$include_tax = ($s['include_tax'] ?? 'no') === 'yes';
$cart_total  = $include_tax
    ? (float) WC()->cart->get_subtotal() + (float) WC()->cart->get_subtotal_tax()
    : (float) WC()->cart->get_subtotal();

$remaining = max( 0.0, $threshold - $cart_total );
$qualified = $remaining <= 0;
$percent   = $qualified ? 100 : (int) max( 0, min( 100, ($cart_total / $threshold) * 100 ) );

$msg_template = $qualified
    ? (string) ( $s['msg_success']  ?? '' )
    : ( $cart_total <= 0
        ? (string) ( $s['msg_initial']  ?? '' )
        : (string) ( $s['msg_progress'] ?? '' ) );

$amount_html    = function_exists( 'wc_price' ) ? wc_price( $threshold ) : esc_html( (string) $threshold );
$remaining_html = function_exists( 'wc_price' ) ? wc_price( $remaining ) : esc_html( (string) $remaining );

$message_html = strtr(
    $msg_template,
    array(
        '{amount}'    => $amount_html,
        '{remaining}' => $remaining_html,
    )
);

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'             => 'aieo-fs-pdp aieo-fs-pdp--' . ( $qualified ? 'qualified' : 'progress' ),
        'data-aieo-fs-pdp'  => '1',
        'data-product-id'   => (string) $pid,
        'data-threshold'    => (string) $threshold,
        'data-include-tax'  => $include_tax ? '1' : '0',
    ) )
    : 'class="aieo-fs-pdp aieo-fs-pdp--' . esc_attr( $qualified ? 'qualified' : 'progress' )
        . '" data-aieo-fs-pdp="1" data-product-id="' . esc_attr( (string) $pid )
        . '" data-threshold="' . esc_attr( (string) $threshold )
        . '" data-include-tax="' . ( $include_tax ? '1' : '0' ) . '"';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>
    <div class="aieo-fs-pdp__msg" data-aieo-fs-pdp-msg><?php echo $message_html; ?></div>
    <div class="aieo-fs-pdp__bar" role="progressbar"
         aria-valuemin="0" aria-valuemax="100"
         aria-valuenow="<?php echo esc_attr( (string) $percent ); ?>">
        <span class="aieo-fs-pdp__fill" data-aieo-fs-pdp-fill style="width: <?php echo esc_attr( (string) $percent ); ?>%"></span>
    </div>
</div>
<?php
echo ob_get_clean(); 
