<?php
/**
 * Product points notice view.
 *
 * @package Awesomemotive\Rewardswp\Views
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use function Awesomemotive\Rewardswp\Helpers\get_points_currency_singular;
use function Awesomemotive\Rewardswp\Helpers\get_points_currency_plural;
use function Awesomemotive\Rewardswp\Helpers\kses;

class ProductPointsNoticeView extends AbstractView implements ViewInterface {
	/**
	 * Icon helper instance.
	 *
	 * @since 1.1.0
	 * @var IconHelper
	 */
	protected IconHelper $icon_helper;

	/**
	 * Constructor with DI.
	 *
	 * @since 1.1.0
	 */
	public function __construct( IconHelper $icon_helper ) {
		$this->icon_helper = $icon_helper;
	}
	/**
	 * Render the product points notice template.
	 *
	 * @since 1.1.0
	 *
	 * @param array $args {
	 *     Arguments for rendering the notice.
	 *     @type int    $points      The number of points to be earned.
	 *     @type int    $variant     The display variant (1, 2, or 3). Default 1.
	 *     @type string $points_label Optional. The label for points (singular or plural).
	 *                               If not provided, will be auto-generated from settings.
	 *     @type string $icon_svg    Optional. The SVG icon HTML. Defaults to gift-box icon.
	 *     @type string $message     Optional. Custom message text. Auto-generated if not provided.
	 *     @type string $description Optional. Additional description text. Auto-generated if not provided.
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$defaults = [
			'points'       => 0,
			'variant'      => 1,
			'points_label' => 'points',
			'icon_svg'     => '',
			'message'      => '',
		];

		$args = wp_parse_args( $args, $defaults );

		// If no points, don't render anything.
		if ( empty( $args['points'] ) || $args['points'] <= 0 ) {
			return;
		}

		$variant = absint( $args['variant'] );

		// Generate default points_label if not provided.
		if ( empty( $args['points_label'] ) || 'points' === $args['points_label'] ) {
			$singular             = get_points_currency_singular();
			$plural               = get_points_currency_plural();
			$args['points_label'] = 1 === $args['points'] ? $singular : $plural;
		}

		// Format points with number formatting.
		$formatted_points = number_format_i18n( $args['points'] );

		// Generate default message based on variant if not provided.
		if ( empty( $args['message'] ) ) {
			if ( 3 === $variant ) {
				// Variant 3: Logged-out visitor, members only.
				$args['message'] = sprintf(
					/* translators: 1: points amount, 2: points label */
					esc_html__( 'Sign in to earn %1$s %2$s when you buy this item.', 'rewardswp' ),
					$formatted_points,
					$args['points_label']
				);
			} else {
				// Variant 1 & 2: Same message for logged-in and guest with participation.
				$args['message'] = sprintf(
					/* translators: 1: points amount, 2: points label */
					esc_html__( 'Earn %1$s %2$s when you buy this item.', 'rewardswp' ),
					$formatted_points,
					$args['points_label']
				);
			}
		}

		// Default icon if not provided.
		if ( empty( $args['icon_svg'] ) ) {
			$args['icon_svg'] = $this->icon_helper->get_icon( 'gift', '', 'regular' );
		}

		// For variant 3, make "Sign in" a clickable link.
		if ( 3 === $variant ) {
			$message_html = sprintf(
				'<a href="#rewardswp-login" class="rwp-banner__link">%1$s</a> %2$s',
				esc_html__( 'Sign in', 'rewardswp' ),
				sprintf(
					/* translators: 1: points amount, 2: points label */
					esc_html__( 'to earn %1$s %2$s when you buy this item.', 'rewardswp' ),
					$formatted_points,
					$args['points_label']
				)
			);
		} else {
			$message_html = esc_html( $args['message'] );
		}

		$html = sprintf(
			'<div class="rwp-banner rwp-banner--pdp">
				<div class="rwp-banner__icon">
					%1$s
				</div>
				<div class="rwp-banner__content">
					<p class="rwp-banner__text">%2$s</p>
				</div>
			</div>',
			$args['icon_svg'],
			$message_html
		);

		echo wp_kses( $html, kses() );
	}
}
