<?php

if ( ! defined( 'ABSPATH' ) ) { return; }
if ( ! function_exists( 'is_singular' ) || ! is_singular( 'product' ) ) { return; }

if ( ! class_exists( 'AIEO_DMM_Settings' ) || ! AIEO_DMM_Settings::get( 'lowest_price_message_enabled', false ) ) {
	return;
}

global $product;
if ( ! ( $product instanceof WC_Product ) ) {
	$product = function_exists( 'wc_get_product' ) ? wc_get_product( get_the_ID() ) : null;
}
if ( ! ( $product instanceof WC_Product ) ) {
	return;
}

$public_on_sale = false;
$public_regular = 0.0;
$pids = $product->is_type( 'variable' ) ? (array) $product->get_children() : array( $product->get_id() );
foreach ( $pids as $sub_id ) {
	
	
	$live = (float) str_replace( ',', '.', (string) get_post_meta( (int) $sub_id, '_price', true ) );
	$reg  = (float) str_replace( ',', '.', (string) get_post_meta( (int) $sub_id, '_regular_price', true ) );
	if ( $reg > 0 && ( 0.0 === $public_regular || $reg < $public_regular ) ) { $public_regular = $reg; }
	if ( $reg > 0 && $live > 0 && $live < $reg ) { $public_on_sale = true; }
}
if ( ! $public_on_sale ) {
	return;
}

$amount = null;
if ( ! class_exists( 'AIEO_Price_History' ) && defined( 'AIEO_DIR' ) ) {
	$ph_file = AIEO_DIR . 'includes/class-aieo-price-history.php';
	if ( file_exists( $ph_file ) ) { require_once $ph_file; }
}
$ph_opt     = get_option( 'aieo_price_history_settings', array() );
$ph_enabled = ! is_array( $ph_opt ) || ! array_key_exists( 'enabled', $ph_opt ) || ! empty( $ph_opt['enabled'] );
if ( $ph_enabled && class_exists( 'AIEO_Price_History' ) ) {
	$low30 = AIEO_Price_History::lowest_30d( $product->get_id() );
	if ( null !== $low30 && (float) $low30 > 0 ) {
		$amount = (float) $low30;
	}
}
if ( null === $amount ) {
	$amount = $public_regular; 
}
if ( $amount <= 0 ) {
	return;
}

$template = class_exists( 'AIEO_DMM_Settings' )
	? (string) AIEO_DMM_Settings::get( 'lowest_price_message_text', '' )
	: '';
if ( '' === trim( $template ) ) {
	return;
}

$price_html = function_exists( 'wc_price' ) ? wc_price( $amount ) : esc_html( (string) $amount );
$message    = str_replace( '{lowest_price}', $price_html, $template );

$tooltip = class_exists( 'AIEO_DMM_Settings' )
	? (string) AIEO_DMM_Settings::get( 'lowest_price_tooltip_text', '' )
	: '';

$font_size = class_exists( 'AIEO_DMM_Settings' )
	? trim( (string) AIEO_DMM_Settings::get( 'lowest_price_message_font_size', '0.75rem' ) )
	: '0.75rem';

$wrapper_args = array( 'class' => 'aieo-lowest-price' );
if ( '' !== $font_size ) {
	$wrapper_args['style'] = '--aieo-lp-size:' . $font_size . ';';
}
$wrapper = function_exists( 'get_block_wrapper_attributes' )
	? get_block_wrapper_attributes( $wrapper_args )
	: 'class="aieo-lowest-price"' . ( '' !== $font_size ? ' style="--aieo-lp-size:' . esc_attr( $font_size ) . ';"' : '' );

echo '<div ' . $wrapper . '>'; 
echo '<div class="aieo-lowest-price__text">' . wp_kses_post( $message ) . '</div>';
if ( '' !== trim( $tooltip ) ) {
	echo '<div class="aieo-lowest-price__info" tabindex="0" role="button" aria-label="' . esc_attr( wp_strip_all_tags( $tooltip ) ) . '">';
	echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="14" height="14" aria-hidden="true"><path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" d="M2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22C6.47715 22 2 17.5228 2 12ZM10.75 8.25C10.75 7.55964 11.3096 7 12 7C12.6904 7 13.25 7.55964 13.25 8.25C13.25 8.94036 12.6904 9.5 12 9.5C11.3096 9.5 10.75 8.94036 10.75 8.25ZM13 11V17H11V11H13Z"></path></svg>';
	echo '<div class="aieo-lowest-price__tooltip">' . wp_kses_post( $tooltip ) . '</div>';
	echo '</div>';
}
echo '</div>';
