<?php

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

/**
 * Template variables:
 *
 * @var string      $ends_at         ISO date string for redemption code expiry
 * @var string      $ends_at_label   Formatted date label (e.g. "Apr 30, 2026 (12:00 AM)")
 * @var int         $total_capacity  Total number of items available to order
 * @var array       $order_rules     Array of per-category rules: [ category_name, capacity, ordered_quantity ]
 * @var bool        $hide_active_until
 * @var bool        $hide_time_remaining
 * @var bool        $hide_total_capacity
 * @var bool        $hide_progress
 * @var bool        $is_preview      True when rendering inside Elementor editor
 */

if ( empty( $ends_at ) && empty( $is_preview ) ) {
	return;
}

$hide_active_until   = ! empty( $hide_active_until );
$hide_time_remaining = ! empty( $hide_time_remaining );
$hide_total_capacity = ! empty( $hide_total_capacity );
$hide_progress       = ! empty( $hide_progress );
?>

<div class="prodigy-rules-widget prodigy-custom-template" data-ends-at="<?php echo esc_attr( $ends_at ?? '' ); ?>" data-hide-progress="<?php echo esc_attr( $hide_progress ? '1' : '0' ); ?>">
	<div class="prodigy-rules">
		<?php if ( ! $hide_active_until ) : ?>
		<div class="prodigy-rules-active d-none d-xl-flex">
			<span class="prodigy-rules__text"><?php echo esc_html__( 'Active until', 'prodigy-commerce' ); ?>:</span>
			<span class="prodigy-rules__text-date"><?php echo esc_html( $ends_at_label ?? '' ); ?></span>
		</div>
		<?php endif; ?>

		<div class="prodigy-rules-content">
			<?php if ( ! $hide_active_until || ! $hide_time_remaining || ! $hide_total_capacity ) : ?>
			<div class="prodigy-rules-meta">
				<?php if ( ! $hide_active_until ) : ?>
				<div class="prodigy-rules-item prodigy-rules-active d-xl-none">
					<span class="prodigy-rules__text"><?php echo esc_html__( 'Active until', 'prodigy-commerce' ); ?>:</span>
					<span class="prodigy-rules__text-date"><?php echo esc_html( $ends_at_label ?? '' ); ?></span>
				</div>
				<?php endif; ?>
				<?php if ( ! $hide_time_remaining ) : ?>
				<div class="prodigy-rules-item prodigy-rules-time">
					<span class="prodigy-rules__text"><?php echo esc_html__( 'Time remaining', 'prodigy-commerce' ); ?>:</span>
					<span class="prodigy-rules__text-time prodigy-rules-countdown-js">
						<?php echo esc_html( $time_remaining ?? '' ); ?>
					</span>
				</div>
				<?php endif; ?>
				<?php if ( ! $hide_total_capacity ) : ?>
				<div class="prodigy-rules-item prodigy-rules-total">
					<span class="prodigy-rules__text"><?php echo esc_html__( 'Total capacity', 'prodigy-commerce' ); ?></span>
					<span class="prodigy-rules__text-time">
						<?php
						// translators: %d: number of items.
						echo esc_html(
							sprintf(
								_n( '%d item', '%d items', (int) ( $total_capacity ?? 0 ), 'prodigy-commerce' ),
								(int) ( $total_capacity ?? 0 )
							)
						);
						?>
					</span>
				</div>
				<?php endif; ?>
			</div>
			<?php endif; ?>

			<?php if ( ! empty( $order_rules ) ) : ?>
			<div class="prodigy-rules-list">
				<?php
				foreach ( $order_rules as $rule ) :
					$capacity         = (int) ( $rule['capacity'] ?? 10 );
					$ordered_quantity = (int) ( $rule['ordered-quantity'] ?? 0 );
					$progress         = $capacity > 0 ? min( 100, round( $ordered_quantity / $capacity * 100 ) ) : 0;
					$category_name    = $rule['category-name'] ?? '';
					?>
				<div class="prodigy-rules-row">
					<div class="prodigy-rules-row-info">
						<span><?php echo esc_html( $category_name ); ?>:</span>
						<span>
							<?php
							// translators: %1$d: ordered quantity, %2$d: capacity.
							echo esc_html(
								sprintf(
									__( '%1$d of %2$d items in cart', 'prodigy-commerce' ),
									$ordered_quantity,
									$capacity
								)
							);
							?>
						</span>
					</div>
					<?php if ( ! $hide_progress ) : ?>
					<div class="prodigy-rules-progress">
						<span style="width: <?php echo esc_attr( $progress ); ?>%;"></span>
					</div>
					<?php endif; ?>
				</div>
				<?php endforeach; ?>
			</div>
			<?php endif; ?>
		</div>
	</div>
</div>
