<?php
/**
 * Appearance tab — color + radius overrides for plugin CSS custom properties.
 *
 * Extracted from AdminSettings per QUALITY-IMPROVEMENT-PLAN.md §T2-2.
 *
 * @package ThemeREX\AutoDealer\Admin\Settings\Tabs
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Admin\Settings\Tabs;

defined( 'ABSPATH' ) || exit;

use ThemeREX\AutoDealer\Admin\AdminSettings;
use ThemeREX\AutoDealer\Admin\Settings\AbstractSettingsTab;

final class AppearanceTab extends AbstractSettingsTab {

	public function getKey(): string {
		return 'appearance';
	}

	public function getLabel(): string {
		return __( 'Appearance', 'trx-auto-dealer' );
	}

	public function register(): void {
		$page = AdminSettings::PAGE_SLUG . '_appearance';

		add_settings_section(
			'trx_ad_appearance',
			__( 'Appearance', 'trx-auto-dealer' ),
			array( $this, 'renderSectionDescription' ),
			$page
		);

		$fields = array(
			array(
				'key'         => 'color_primary',
				'label'       => __( 'Primary Color', 'trx-auto-dealer' ),
				'fallback'    => '#1a56db',
				'description' => __( 'Main brand color (buttons, links, badges).', 'trx-auto-dealer' ),
			),
			array(
				'key'      => 'color_primary_hover',
				'label'    => __( 'Primary Hover Color', 'trx-auto-dealer' ),
				'fallback' => '#1e429f',
			),
			array(
				'key'         => 'color_success',
				'label'       => __( 'Success / Available Color', 'trx-auto-dealer' ),
				'fallback'    => '#0e9f6e',
				'description' => __( 'Used for "New" and available-status indicators.', 'trx-auto-dealer' ),
			),
			array(
				'key'      => 'color_warning',
				'label'    => __( 'Warning / Coming Soon Color', 'trx-auto-dealer' ),
				'fallback' => '#c27803',
			),
			array(
				'key'      => 'color_danger',
				'label'    => __( 'Danger / Sold Color', 'trx-auto-dealer' ),
				'fallback' => '#e02424',
			),
		);
		foreach ( $fields as $field ) {
			add_settings_field(
				$field['key'],
				$field['label'],
				array( $this, 'renderColorField' ),
				$page,
				'trx_ad_appearance',
				array(
					'key'         => $field['key'],
					'fallback'    => $field['fallback'],
					'description' => $field['description'] ?? '',
				)
			);
		}

		add_settings_field(
			'radius',
			__( 'Card Corner Radius', 'trx-auto-dealer' ),
			array( $this, 'renderRadiusField' ),
			$page,
			'trx_ad_appearance'
		);

		add_settings_field(
			'use_theme_fonts',
			__( 'Use theme fonts', 'trx-auto-dealer' ),
			array( $this, 'renderUseThemeFonts' ),
			$page,
			'trx_ad_appearance'
		);

		add_settings_section(
			'trx_ad_appearance_display',
			__( 'Display Options', 'trx-auto-dealer' ),
			array( $this, 'renderDisplaySectionDescription' ),
			$page
		);

		add_settings_field(
			'show_price',
			__( 'Show price', 'trx-auto-dealer' ),
			array( $this, 'renderShowPrice' ),
			$page,
			'trx_ad_appearance_display'
		);

		add_settings_field(
			'show_featured_badge',
			__( 'Show featured badge', 'trx-auto-dealer' ),
			array( $this, 'renderShowFeaturedBadge' ),
			$page,
			'trx_ad_appearance_display'
		);

		add_settings_field(
			'single_layout',
			__( 'Active single-page layout', 'trx-auto-dealer' ),
			array( $this, 'renderSingleLayout' ),
			$page,
			'trx_ad_appearance_display'
		);
	}

	public function render(): void {
		?>
		<form method="post" action="options.php">
			<?php settings_fields( 'trx_ad_settings_group' ); ?>
			<input type="hidden" name="<?php echo esc_attr( AdminSettings::OPTION_KEY ); ?>[trx_ad_appearance_tab]" value="1">
			<?php
			do_settings_sections( AdminSettings::PAGE_SLUG . '_appearance' );
			submit_button();
			?>
		</form>
		<?php
	}

	public function renderSectionDescription(): void {
		printf(
			'<p>%s</p>',
			esc_html__( 'Override the plugin\'s default colors and corner radius. Leave a color blank to use the stylesheet default. Values write to CSS custom properties so they cascade to all blocks, widgets, and templates.', 'trx-auto-dealer' )
		);
	}

	/**
	 * @param array<string,string> $args Settings API callback args (key, fallback, description?).
	 */
	public function renderColorField( array $args ): void {
		$settings = AdminSettings::getSettings();
		$key      = (string) ( $args['key'] ?? '' );
		$fallback = (string) ( $args['fallback'] ?? '' );
		$desc     = (string) ( $args['description'] ?? '' );
		$value    = isset( $settings[ $key ] ) ? (string) $settings[ $key ] : '';

		printf(
			'<input type="text" name="%1$s[%2$s]" id="trx_ad_%2$s" value="%3$s" class="trx-ad-color-field" data-default-color="%4$s" placeholder="%4$s">',
			esc_attr( AdminSettings::OPTION_KEY ),
			esc_attr( $key ),
			esc_attr( $value ),
			esc_attr( $fallback )
		);
		if ( '' !== $desc ) {
			printf( '<p class="description">%s</p>', esc_html( $desc ) );
		}
	}

	public function renderDisplaySectionDescription(): void {
		printf(
			'<p>%s</p>',
			esc_html__( 'Toggle the on-card price + featured badge and pick which template renders the single-listing page. The single-page layout also appears in the Single Listing tab — both controls write to the same setting.', 'trx-auto-dealer' )
		);
	}

	public function renderShowPrice(): void {
		$settings = AdminSettings::getSettings();
		$enabled  = ! empty( $settings['show_price'] );
		printf(
			'<label><input type="checkbox" name="%1$s[show_price]" value="1"%2$s> %3$s</label>'
			. '<p class="description">%4$s</p>',
			esc_attr( AdminSettings::OPTION_KEY ),
			checked( $enabled, true, false ),
			esc_html__( 'Show vehicle price on archive cards and the single-listing page.', 'trx-auto-dealer' ),
			esc_html__( 'Turn off to suppress prices site-wide (call-for-price dealers).', 'trx-auto-dealer' )
		);
	}

	public function renderShowFeaturedBadge(): void {
		$settings = AdminSettings::getSettings();
		$enabled  = ! empty( $settings['show_featured_badge'] );
		printf(
			'<label><input type="checkbox" name="%1$s[show_featured_badge]" value="1"%2$s> %3$s</label>'
			. '<p class="description">%4$s</p>',
			esc_attr( AdminSettings::OPTION_KEY ),
			checked( $enabled, true, false ),
			esc_html__( 'Show the "Featured" ribbon on listings flagged as featured.', 'trx-auto-dealer' ),
			esc_html__( 'When off, featured listings still sort first but no badge appears.', 'trx-auto-dealer' )
		);
	}

	public function renderSingleLayout(): void {
		$settings = AdminSettings::getSettings();
		$current  = (string) ( $settings['single_layout'] ?? 'default' );
		$options  = array(
			'default'   => __( 'Default (PHP template)', 'trx-auto-dealer' ),
			'gutenberg' => __( 'Gutenberg template', 'trx-auto-dealer' ),
			'elementor' => __( 'Elementor template', 'trx-auto-dealer' ),
		);
		printf( '<select name="%s[single_layout]" id="trx_ad_appearance_single_layout">', esc_attr( AdminSettings::OPTION_KEY ) );
		foreach ( $options as $value => $label ) {
			printf(
				'<option value="%s"%s>%s</option>',
				esc_attr( $value ),
				selected( $current, $value, false ),
				esc_html( $label )
			);
		}
		echo '</select>';
		printf(
			'<p class="description">%s</p>',
			esc_html__( 'Which template renders single vehicle pages. For Gutenberg / Elementor you also pick the template ID in the Single Listing tab.', 'trx-auto-dealer' )
		);
	}

	public function renderRadiusField(): void {
		$settings = AdminSettings::getSettings();
		$value    = (int) ( $settings['radius'] ?? 8 );
		printf(
			'<input type="range" name="%1$s[radius]" id="trx_ad_radius" value="%2$s" min="0" max="32" step="1" class="trx-ad-radius-slider" oninput="document.getElementById(\'trx_ad_radius_out\').textContent=this.value+\'px\';">',
			esc_attr( AdminSettings::OPTION_KEY ),
			esc_attr( (string) $value )
		);
		printf(
			'<output id="trx_ad_radius_out" style="margin-left:8px;">%spx</output>',
			esc_html( (string) $value )
		);
		printf(
			'<p class="description">%s</p>',
			esc_html__( 'Default 8px. Range 0–32px; 0 = square corners. Applies to cards, buttons, badges.', 'trx-auto-dealer' )
		);
	}

	public function renderUseThemeFonts(): void {
		$settings = AdminSettings::getSettings();
		printf(
			'<label><input type="checkbox" name="%1$s[use_theme_fonts]" value="1" %2$s> %3$s</label>',
			esc_attr( AdminSettings::OPTION_KEY ),
			checked( ! empty( $settings['use_theme_fonts'] ), true, false ),
			esc_html__( 'Inherit the active theme\'s typography instead of the plugin\'s built-in font stack.', 'trx-auto-dealer' )
		);
		printf(
			'<p class="description">%s</p>',
			esc_html__( 'Applies to inventory archive and single-listing pages (the plugin\'s own font stack is a neutral system stack).', 'trx-auto-dealer' )
		);
	}
}
