<?php
/**
 * General settings 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\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\General\CurrencyOption;
use Awesomemotive\Rewardswp\Settings\Options\General\ProgramParticipantsOption;
use function Awesomemotive\Rewardswp\Helpers\get_currencies;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_view_component;
use function Awesomemotive\Rewardswp\Helpers\is_guest_checkout_enabled;
use function Awesomemotive\Rewardswp\Helpers\kses;
use Awesomemotive\Rewardswp\Helpers\IconHelper;

class GeneralSettingsContentView extends AbstractView implements ViewInterface {
	private IconHelper $icon_helper;

	public function __construct( IconHelper $icon_helper ) {
		$this->icon_helper = $icon_helper;
	}

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		$currencies        = get_currencies();
		$selected_currency = get_option_value( CurrencyOption::class );

		?>

		<div class="divide-y divide-gray-200">

			<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[general][currency]" class="text-sm font-semibold"><?php esc_html_e( 'Currency', 'rewardswp' ); ?></label>
					</div>
					<div class="md:w-2/3">
					<?php

					echo wp_kses(
						get_view_component(
							'select',
							[
								'section'  => 'general',
								'name'     => 'currency',
								'label'    => esc_html__( 'Currency', 'rewardswp' ),
								'options'  => array_reduce(
									array_keys( $currencies ),
									function ( $carry, $currency_code ) use ( $currencies ) {
										$carry[ $currency_code ] = sprintf(
											'%s (%s) — %s',
											$currencies[ $currency_code ]['name'],
											$currencies[ $currency_code ]['symbol'],
											$currency_code
										);
										return $carry;
									},
									[]
								),
								'selected' => $selected_currency,
							]
						),
						kses()
					);

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

		<?php
		// Check if guest checkout is enabled in WooCommerce or EDD.
		$guest_checkout_enabled = is_guest_checkout_enabled();
		$selected_value         = get_option_value( ProgramParticipantsOption::class );

		// Ensure we have a valid value (fallback to default if not set or invalid).
		if ( empty( $selected_value ) || ! in_array( $selected_value, [ 'all_customers', 'members_only' ], true ) ) {
			$selected_value = 'all_customers'; // Default value.
		}
		?>

			<div class="py-5">
				<div class="gap-8 md:flex md:items-start">
					<div class="md:w-1/3 flex-none">
						<label class="text-sm font-semibold"><?php esc_html_e( 'Program Participants', 'rewardswp' ); ?></label>
									</div>
			<div class="md:w-2/3">
			<fieldset class="mt-2 xs:mt-0" x-data="{ selectedParticipant: '<?php echo esc_attr( $selected_value ); ?>' }">
				<?php
				$radio_options = [
					[
						'value'       => 'all_customers',
						'label'       => esc_html__( 'All customers — members & guests (recommended)', 'rewardswp' ),
						'description' => esc_html__( 'Guests must create an account before they can redeem points for rewards.', 'rewardswp' ),
					],
					[
						'value'       => 'members_only',
						'label'       => esc_html__( 'Members only — customers with a WordPress account', 'rewardswp' ),
						'description' => esc_html__( 'Customers must create an account before they can participate.', 'rewardswp' ),
					],
				];

				// Callback to render warning message for 'all_customers' option.
				$option_callback = function ( $option, $args ) use ( $selected_value, $guest_checkout_enabled ) {
					// Show warning only for 'all_customers' option when guest checkout is disabled.
					if ( 'all_customers' === $option['value'] && ! $guest_checkout_enabled ) :
						$active_integration = get_option_value( \Awesomemotive\Rewardswp\Settings\Options\Integrations\IntegrationSourceOption::class );
						?>
						<div class="ml-7 mt-2 rounded-md bg-yellow-50 p-3" x-show="selectedParticipant === 'all_customers'">
							<div class="flex">
								<div class="shrink-0">
									<?php echo $this->icon_helper->get_icon( 'information-circle', 'h-5 w-5 text-yellow-400' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
								</div>
					<div class="ml-3">
						<p class="text-sm text-yellow-700">
							<?php
							if ( 'woocommerce' === $active_integration ) {
								printf(
									wp_kses(
										/* translators: %s: Link to WooCommerce settings */
										__( 'Guest checkout is currently disabled in WooCommerce. <a href="%s" class="underline">Enable guest checkout</a> to allow guests to participate in the rewards program.', 'rewardswp' ),
										[
											'a' => [
												'href'  => [],
												'class' => [],
											],
										]
									),
									esc_url( admin_url( 'admin.php?page=wc-settings&tab=account' ) )
								);
							} elseif ( 'edd' === $active_integration ) {
								printf(
									wp_kses(
										/* translators: %s: Link to EDD settings */
										__( 'Guest checkout is currently disabled in Easy Digital Downloads. <a href="%s" class="underline">Enable guest checkout</a> to allow guests to participate in the rewards program.', 'rewardswp' ),
										[
											'a' => [
												'href'  => [],
												'class' => [],
											],
										]
									),
									esc_url( admin_url( 'edit.php?post_type=download&page=edd-settings&tab=general&section=main' ) )
								);
							} else {
								esc_html_e( 'Guest checkout is currently disabled. Please enable it in WooCommerce or Easy Digital Downloads to allow guests to participate in the rewards program.', 'rewardswp' );
							}
							?>
						</p>
					</div>
							</div>
						</div>
								<?php
							endif;
				};

				echo wp_kses(
					get_view_component(
						'radio',
						[
							'name'            => 'rewardswp_settings[general][program_participants]',
							'options'         => $radio_options,
							'checked'         => $selected_value,
							'x-model'         => 'selectedParticipant',
							'option_callback' => $option_callback,
						]
					),
					kses()
				);
		?>
				</fieldset>
					</div>
				</div>
			</div>
		</div>

		<?php
	}
}
