<?php

declare(strict_types=1);

namespace Australcode\Rewards\Frontend;

use Australcode\Rewards\Domain\Settings\SettingsRepository;
use Australcode\Rewards\Hooks\RedemptionCartHook;
use Australcode\Rewards\Service\RedemptionService;

defined( 'ABSPATH' ) || exit;

/**
 * Renders the "Use your points" redeem form for the current customer's cart.
 *
 * Shared by the My Account Rewards tab and the [acrwd_redeem] shortcode/block so
 * the redemption surface is identical wherever it is placed. The form posts to
 * the same wp_loaded handler (CartPointsNotice::handle_apply) and the discount is
 * applied as a cart fee on woocommerce_cart_calculate_fees, which both the
 * classic and Blocks cart execute — so it works on every store.
 */
final class RedeemFormRenderer {

	public function __construct(
		private readonly RedemptionService $redemption,
		private readonly RedemptionCartHook $cart_hook,
		private readonly SettingsRepository $settings,
	) {
	}

	/**
	 * Echoes the redeem-form card for $user_id, or nothing when redemption is
	 * disabled, the cart is empty, or no points are redeemable.
	 */
	public function render( int $user_id ): void {
		if ( ! $this->settings->get()->redemption_enabled ) {
			return;
		}

		$cart = function_exists( 'WC' ) ? WC()->cart : null;
		if ( null === $cart || $cart->is_empty() ) {
			return;
		}

		$max = $this->redemption->max_redeemable_points( $user_id, (float) $cart->get_subtotal() );
		if ( $max <= 0 ) {
			return;
		}

		$amount  = $this->redemption->points_to_amount( $max );
		$applied = $this->cart_hook->session_points();

		echo '<div class="acrwd-redeem-card">';
		echo '<h3>' . esc_html__( 'Use your points', 'australcode-points-rewards' ) . '</h3>';
		if ( $applied > 0 ) {
			echo '<p>' . wp_kses_post(
				sprintf(
					/* translators: 1: applied points, 2: cart URL. */
					__( '<strong>%1$s</strong> points applied to your current cart — <a href="%2$s">view cart</a>.', 'australcode-points-rewards' ),
					esc_html( number_format_i18n( $applied ) ),
					esc_url( wc_get_cart_url() )
				)
			) . '</p>';
		} else {
			/* translators: keep the {points} and {max_discount} placeholders. */
			$default  = __( 'Redeem up to <strong>{points}</strong> points on your current cart — worth {max_discount} off.', 'australcode-points-rewards' );
			$template = '' !== $this->settings->get()->msg_redeem ? $this->settings->get()->msg_redeem : $default;
			$message  = MessageTemplate::render(
				$template,
				array(
					'{points}'       => esc_html( number_format_i18n( $max ) ),
					'{max_discount}' => wc_price( $amount ),
				)
			);
			echo '<p>' . wp_kses_post( $message ) . '</p>';
		}
		echo '<form method="post" class="acrwd-redeem-form">';
		wp_nonce_field( 'acrwd_apply_redemption' );
		echo '<input type="hidden" name="acrwd_action" value="apply_redemption" />';
		echo '<input type="number" name="acrwd_points" min="0" max="' . esc_attr( (string) $max ) . '" step="1" value="' . esc_attr( (string) ( $applied > 0 ? $applied : $max ) ) . '" aria-label="' . esc_attr__( 'Points to redeem', 'australcode-points-rewards' ) . '" /> ';
		echo '<button type="submit" class="button">' . ( $applied > 0 ? esc_html__( 'Update', 'australcode-points-rewards' ) : esc_html__( 'Apply points', 'australcode-points-rewards' ) ) . '</button>';
		if ( $applied > 0 ) {
			echo ' <button type="submit" name="acrwd_points" value="0" class="button">' . esc_html__( 'Remove', 'australcode-points-rewards' ) . '</button>';
		}
		echo '</form></div>';
	}
}
