<?php
/**
 * Referrals program status content view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Parts
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Parts;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\EnabledOption;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_view_component;
use function Awesomemotive\Rewardswp\Helpers\kses;
use function Awesomemotive\Rewardswp\Helpers\svg_kses;

class ReferralsProgramStatusContentView extends AbstractView implements ViewInterface {

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

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

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 * @since 1.2.0 Added warning badge for out-of-stock advocate reward.
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		?>

		<div class="py-5">
			<div class="gap-8 md:flex md:items-center">
				<div class="md:w-1/3 flex-none">
					<label for="rewardswp_settings[referrals][enabled]" class="text-sm font-semibold">
						<?php esc_html_e( 'Program Status', 'rewardswp' ); ?>
						<?php
						echo wp_kses(
							get_view_component(
								'badge',
								[
									'text'               => esc_html__( 'Active', 'rewardswp' ),
									'style'              => 'active',
									'additional_classes' => 'ml-2',
									'x-show'             => 'enabled',
								]
							),
							kses()
						);
						?>
						<?php
						echo wp_kses(
							get_view_component(
								'badge',
								[
									'text'               => esc_html__( 'Inactive', 'rewardswp' ),
									'style'              => 'inactive',
									'additional_classes' => 'ml-2',
									'x-show'             => '!enabled',
								]
							),
							kses()
						);
						?>
					</label>
				</div>
				<div class="md:w-2/3">

					<?php

					echo wp_kses(
						get_view_component(
							'toggle',
							[
								'section'         => 'referrals',
								'name'            => 'enabled',
								'label'           => esc_html__( 'Program Status', 'rewardswp' ),
								'is_checked'      => get_option_value( EnabledOption::class ),
								'on_click'        => 'enabled = !enabled',
								'x-bind:disabled' => "\$store.referrals.rewards.advocate?.type === 'free_product' && \$store.referrals.rewards.advocate?.products_included?.[0]?.in_stock === false",
							]
						),
						kses()
					);

					?>

					<!-- Warning when advocate Free Product reward is out of stock -->
					<div x-show="$store.referrals.rewards.advocate?.type === 'free_product' && $store.referrals.rewards.advocate?.products_included?.[0]?.in_stock === false"
						 x-cloak
						 class="mt-3 flex items-start gap-2 bg-amber-50 text-amber-700 px-3 py-2 rounded-md border border-amber-200">
						<?php echo wp_kses( $this->icon_helper->get_icon( 'alert-triangle', 'w-4 h-4 flex-shrink-0 mt-1.5' ), svg_kses() ); ?>
						<span class="text-sm">
							<?php esc_html_e( 'Program disabled: Advocate reward product is out of stock.', 'rewardswp' ); ?>
							<br>
							<?php esc_html_e( 'Select a different product or restock to re-enable.', 'rewardswp' ); ?>
						</span>
					</div>

				</div>
			</div>
		</div>

		<?php
	}
}
