<?php
/**
 * Radio component view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Components
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Components;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use function Awesomemotive\Rewardswp\Helpers\build_input_id;
use function Awesomemotive\Rewardswp\Helpers\parse_input_name;
use function Awesomemotive\Rewardswp\Helpers\replace_placeholders;
use function Awesomemotive\Rewardswp\Helpers\tag_attr;

class RadioComponentView extends AbstractView implements ViewInterface {
	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'name'           => '',
				':name'          => '',
				'x-model'        => '',
				'options'        => [],
				'@change'        => '',
				'x-on:change'    => '',
				'checked'        => '',
				':checked'       => '',
				'x-init:checked' => '',
			]
		);

		// Backward compatibility: allow shorthand arg keys while emitting only full Alpine directives.
		if ( '' === $args['x-on:change'] && '' !== $args['@change'] ) {
			$args['x-on:change'] = $args['@change'];
		}

		$name_input_name = empty( $args[':name'] )
			? 'name'
			: ':name';
		$name_input_val  = empty( $args[':name'] )
			? $args['name']
			: $args[':name'];

		if ( empty( $name_input_val ) ) {
			esc_html_e( 'Failed to render the radio component, name argument is required.', 'rewardswp' );
			return;
		}

		$checked_attr   = '';
		$checked_value  = '';
		$static_checked = '';

		if ( ! empty( $args['x-bind:checked'] ) ) {
			$checked_attr  = 'x-bind:checked';
			$checked_value = $args['x-bind:checked'];
		} elseif ( ! empty( $args[':checked'] ) ) {
			$checked_attr  = ':checked';
			$checked_value = $args[':checked'];
		} elseif ( isset( $args['checked'] ) && $args['checked'] !== '' ) {
			// Static checked value (for non-Alpine forms).
			$static_checked = $args['checked'];
		}

		?>

		<?php foreach ( $args['options'] as $option ) : ?>

			<?php
			$option_value         = isset( $option['value'] ) ? (string) $option['value'] : '';
			$sanitized_option_val = esc_attr( $option_value );
			$replacements         = [
				'option_value' => $sanitized_option_val,
			];
			$input_id             = build_input_id( 'radio', sanitize_title( $option_value ), uniqid( 'option_' ) );

			// Pre-calculate the checked attribute for static forms.
			$is_checked = ( '' !== $static_checked && $static_checked === $option_value ) ? ' checked="checked"' : '';
			?>

			<div class="mb-2">
				<div class="flex items-center">
					<input
						<?php tag_attr( 'x-model', $args['x-model'] ); ?>
						<?php tag_attr( 'x-on:change', $args['x-on:change'] ); ?>
						<?php

						echo wp_kses_data(
							replace_placeholders(
								tag_attr( $name_input_name, $name_input_val, false, false ),
								$replacements
							)
						);

						echo empty( $checked_attr )
							? ''
							: wp_kses_data(
								replace_placeholders(
									tag_attr( $checked_attr, $checked_value, true, false ),
									$replacements
								)
							);

						?>
						id="<?php echo esc_attr( $input_id ); ?>"
						type="radio"
						value="<?php echo esc_attr( $option_value ); ?>"<?php echo $is_checked; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						class="relative size-4 appearance-none rounded-full border border-gray-300 bg-white before:absolute before:inset-1 before:rounded-full before:bg-white not-checked:before:hidden checked:border-blue-600 checked:bg-blue-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 disabled:border-gray-300 disabled:bg-gray-100 disabled:before:bg-gray-400 forced-colors:appearance-auto forced-colors:before:hidden"
					>
					<label for="<?php echo esc_attr( $input_id ); ?>" class="ml-3 block text-sm/6 text-gray-900">
						<?php echo esc_html( $option['label'] ); ?>
					</label>
				</div>

				<?php if ( ! empty( $option['description'] ) ) : ?>
					<p class="ml-7 mt-1 text-sm text-gray-500">
						<?php echo esc_html( $option['description'] ); ?>
					</p>
				<?php endif; ?>

				<?php
				// Allow custom content after each option (e.g., warnings, additional info).
				if ( isset( $args['option_callback'] ) && is_callable( $args['option_callback'] ) ) {
					call_user_func( $args['option_callback'], $option, $args );
				}
				?>
			</div>

		<?php endforeach; ?>

		<?php
	}
}
