<?php
/**
 * Purchase Confirmation Banner View
 *
 * Displays a rewards banner on order confirmation pages (WooCommerce/EDD).
 * Shows different states based on member status and participation mode.
 *
 * @package Awesomemotive\Rewardswp\Views
 * @since 1.1.0
 */

namespace Awesomemotive\Rewardswp\Views;

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

use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;

/**
 * Purchase Confirmation Banner View
 *
 * @since 1.1.0
 */
class PurchaseConfirmationBannerView implements ViewInterface {

	/**
	 * Icon helper.
	 *
	 * @since 1.1.0
	 * @var IconHelper
	 */
	private IconHelper $icon_helper;

	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 *
	 * @param IconHelper $icon_helper Icon helper service.
	 */
	public function __construct( IconHelper $icon_helper ) {
		$this->icon_helper = $icon_helper;
	}

	/**
	 * Render the purchase confirmation banner.
	 *
	 * @since 1.1.0
	 *
	 * @param array $args {
	 *     View arguments.
	 *
	 *     @type int    $variant           Banner variant (1-5).
	 *     @type int    $points_earned     Points earned from this purchase.
	 *     @type int    $current_balance   Current points balance.
	 *     @type int    $pending_points    Points pending payment confirmation (for variants 4 & 5).
	 *     @type string $currency_label    Currency label for points earned (singular or plural).
	 *     @type string $balance_label     Currency label for balance (singular or plural).
	 *     @type string $advocate_email    Member's email address.
	 *     @type string $customer_name     Customer's full name.
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		// Extract args.
		$variant         = $args['variant'] ?? 1;
		$points_earned   = $args['points_earned'] ?? 0;
		$current_balance = $args['current_balance'] ?? 0;
		$pending_points  = $args['pending_points'] ?? 0;
		$currency_label  = $args['currency_label'] ?? '';
		$balance_label   = $args['balance_label'] ?? '';
		$advocate_email  = $args['advocate_email'] ?? '';
		$customer_name   = $args['customer_name'] ?? '';

		// Determine banner modifier class.
		$modifier_class = in_array( $variant, [ 4, 5 ], true ) ? 'rwp-banner--pending' : 'rwp-banner--confirmation';

		?>
		<section class="rwp-banner <?php echo esc_attr( $modifier_class ); ?>" role="status" aria-live="polite">
			<div class="rwp-banner__icon">
				<?php $this->icon_helper->render_icon( 'gift' ); ?>
			</div>
			<div class="rwp-banner__content">
				<?php if ( 1 === $variant ) : // Variant 1: Logged-in member. ?>
					<p class="rwp-banner__heading">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: Number of points earned with currency label */
								__( 'You earned %s', 'rewardswp' ),
								number_format_i18n( $points_earned ) . ' ' . $currency_label
							)
						);
						?>
					</p>
					<p class="rwp-banner__subtext">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: Current balance with currency label */
								__( 'Balance is now %s.', 'rewardswp' ),
								number_format_i18n( $current_balance ) . ' ' . $balance_label
							)
						);
						?>
					</p>
					<div class="rwp-banner__actions">
						<a href="#rewardswp-redeem" class="rwp-banner__button">
							<?php esc_html_e( 'See rewards', 'rewardswp' ); ?>
						</a>
						<a href="#rewardswp-home" class="rwp-banner__link">
							<?php esc_html_e( 'Program details', 'rewardswp' ); ?>
						</a>
					</div>

				<?php elseif ( 2 === $variant ) : // Variant 2: Guest purchase with all customers mode. ?>
					<p class="rwp-banner__heading">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: Number of points earned with currency label */
								__( 'You earned %s', 'rewardswp' ),
								number_format_i18n( $points_earned ) . ' ' . $currency_label
							)
						);
						?>
					</p>
					<p class="rwp-banner__subtext">
						<?php esc_html_e( 'Create a free account to redeem your points for rewards.', 'rewardswp' ); ?>
					</p>
					<div class="rwp-banner__actions">
						<a href="#rewardswp-join" onclick="event.preventDefault(); window.dispatchEvent(new CustomEvent('rewardswp-join-with-email', { detail: { email: '<?php echo esc_js( $advocate_email ); ?>', name: '<?php echo esc_js( $customer_name ); ?>' } })); window.location.hash = '#rewardswp-join';" class="rwp-banner__button">
							<?php esc_html_e( 'Create account', 'rewardswp' ); ?>
						</a>
						<a href="#rewardswp-home" class="rwp-banner__link">
							<?php esc_html_e( 'What can I do with points?', 'rewardswp' ); ?>
						</a>
					</div>

				<?php elseif ( 3 === $variant ) : // Variant 3: Guest purchase with members only / guest checkout off. ?>
					<p class="rwp-banner__heading">
						<?php esc_html_e( 'Create a free account to start earning', 'rewardswp' ); ?>
					</p>
					<p class="rwp-banner__subtext">
						<?php esc_html_e( 'You did not earn points for this order because you were not logged in.', 'rewardswp' ); ?>
					</p>
					<div class="rwp-banner__actions">
						<a href="#rewardswp-join" onclick="event.preventDefault(); window.dispatchEvent(new CustomEvent('rewardswp-join-with-email', { detail: { email: '<?php echo esc_js( $advocate_email ); ?>', name: '<?php echo esc_js( $customer_name ); ?>' } })); window.location.hash = '#rewardswp-join';" class="rwp-banner__button">
							<?php esc_html_e( 'Create account', 'rewardswp' ); ?>
						</a>
						<span class="rwp-banner__inline">
							<?php esc_html_e( 'Already a member?', 'rewardswp' ); ?>
							<a href="#rewardswp-login" class="rwp-banner__link">
								<?php esc_html_e( 'Sign in', 'rewardswp' ); ?>
							</a>
						</span>
					</div>

				<?php elseif ( 4 === $variant ) : // Variant 4: Logged-in member, pending payment. ?>
					<p class="rwp-banner__heading">
						<?php esc_html_e( 'Points pending', 'rewardswp' ); ?>
					</p>
					<p class="rwp-banner__subtext">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: Number of points with currency label */
								__( "You'll earn %s once your payment is confirmed. We'll update your balance automatically.", 'rewardswp' ),
								number_format_i18n( $pending_points ) . ' ' . $currency_label
							)
						);
						?>
					</p>
					<div class="rwp-banner__actions">
						<a href="#rewardswp-redeem" class="rwp-banner__button">
							<?php esc_html_e( 'See rewards', 'rewardswp' ); ?>
						</a>
						<a href="#rewardswp-home" class="rwp-banner__link">
							<?php esc_html_e( 'Program details', 'rewardswp' ); ?>
						</a>
					</div>

				<?php elseif ( 5 === $variant ) : // Variant 5: Guest, pending payment (all-customers mode). ?>
					<p class="rwp-banner__heading">
						<?php esc_html_e( 'Points pending', 'rewardswp' ); ?>
					</p>
					<p class="rwp-banner__subtext">
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: Number of points with currency label */
								__( "You'll earn %s once your payment is confirmed. Create a free account to redeem your points for rewards.", 'rewardswp' ),
								number_format_i18n( $pending_points ) . ' ' . $currency_label
							)
						);
						?>
					</p>
					<div class="rwp-banner__actions">
						<a href="#rewardswp-join" onclick="event.preventDefault(); window.dispatchEvent(new CustomEvent('rewardswp-join-with-email', { detail: { email: '<?php echo esc_js( $advocate_email ); ?>', name: '<?php echo esc_js( $customer_name ); ?>' } })); window.location.hash = '#rewardswp-join';" class="rwp-banner__button">
							<?php esc_html_e( 'Create account', 'rewardswp' ); ?>
						</a>
						<a href="#rewardswp-home" class="rwp-banner__link">
							<?php esc_html_e( 'What can I do with points?', 'rewardswp' ); ?>
						</a>
					</div>
				<?php endif; ?>
			</div>
		</section>
		<?php
	}
}
