<?php
/**
 * Mysterx rewards list template
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/mysterx-rewards.php.
 *
 * @package Mysterx/templates
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

global $product;

$product_id = $product->get_id();
$rewards    = mysterx_get_items_for_product( $product_id );

if ( empty( $rewards ) ) {
	return;
}

$show_chances = Mysterx_Settings::get_option( 'show_winning_chances', '1' );
$show_value   = Mysterx_Settings::get_option( 'show_reward_value', '1' );
$show_rarity  = Mysterx_Settings::get_option( 'show_reward_rarity', '1' );

// Calculate total weight for probabilities if needed.
$total_weight  = 0;
$opened_counts = $product->get_count_of_opened_rewards();

if ( '1' === $show_chances ) {
	foreach ( $rewards as $reward ) {
		$qty            = intval( $reward['qty'] );
		$already_opened = isset( $opened_counts[ $reward['reward_id'] ] ) ? intval( $opened_counts[ $reward['reward_id'] ]->total_used ) : 0;
		$is_sold_out    = ( $qty > 0 && $already_opened >= $qty );

		if ( ! $is_sold_out ) {
			$total_weight += intval( $reward['weight'] );
		}
	}
}

?>
<div class="mysterx-rewards-container">
	<h3 class="mysterx-rewards-title"><?php esc_html_e( 'Possible Rewards', 'mysterx-mystery-boxes-for-woocommerce' ); ?></h3>
	<div class="mysterx-rewards-grid">
		<?php
		foreach ( $rewards as $reward ) :
			$reward_product = wc_get_product( $reward['reward_id'] );
			if ( ! $reward_product ) {
				continue;
			}

			$name           = $reward_product->get_name();
			$image          = $reward_product->get_image( 'thumbnail' );
			$weight         = intval( $reward['weight'] );
			$qty            = intval( $reward['qty'] );
			$already_opened = isset( $opened_counts[ $reward['reward_id'] ] ) ? intval( $opened_counts[ $reward['reward_id'] ]->total_used ) : 0;
			$is_sold_out    = ( $qty > 0 && $already_opened >= $qty );
			$rarity         = mysterx_get_rarity_class_by_weight( $weight, $product_id, $reward['reward_id'] );

			// Probability calculation.
			$probability = ( $total_weight > 0 && ! $is_sold_out ) ? round( ( $weight / $total_weight ) * 100, 2 ) : 0;
			?>
			<a href="<?php echo esc_url( $reward_product->get_permalink() ); ?>" class="mysterx-reward-item rarity-<?php echo esc_attr( $rarity ); ?> <?php echo $is_sold_out ? 'mysterx-sold-out' : ''; ?>">
				<?php if ( $is_sold_out ) : ?>
					<span class="mysterx-sold-out-badge"><?php esc_html_e( 'Gone', 'mysterx-mystery-boxes-for-woocommerce' ); ?></span>
				<?php endif; ?>
				<div class="mysterx-reward-image">
					<?php echo wp_kses_post( $image ); ?>
				</div>
				<div class="mysterx-reward-info">
					<span class="mysterx-reward-name"><?php echo esc_html( $name ); ?></span>
					<div class="mysterx-reward-meta">
						<?php if ( '1' === $show_rarity ) : ?>
							<span class="mysterx-reward-rarity-badge"><?php echo esc_html( ucfirst( $rarity ) ); ?></span>
						<?php endif; ?>
						<?php if ( '1' === $show_value ) : ?>
							<span class="mysterx-reward-price"><?php echo wp_kses_post( $reward_product->get_price_html() ); ?></span>
						<?php endif; ?>
						<?php if ( '1' === $show_chances && ! $is_sold_out ) : ?>
							<span class="mysterx-reward-chance"><?php echo esc_html( $probability ); ?>%</span>
						<?php endif; ?>
					</div>
				</div>
			</a>
		<?php endforeach; ?>
	</div>
</div>
