<?php
/**
 * Compare / Favorites tab — toggle each feature, pick the page that hosts the
 * shortcode, and tune visitor-facing strings.
 *
 * Compare + Favorites both ship enabled by default (Activator creates the pages
 * during activation), but operators sometimes need to disable them per-site —
 * single-listing dealers, custom auction flows, or sites where Favorites is
 * surfaced inside a member area instead.
 *
 * @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 CompareFavoritesTab extends AbstractSettingsTab {

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

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

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

		// Page-id options are standalone scalars, not nested in the main settings
		// array — easier for the activator + DealerDashboard to read.
		register_setting(
			'trx_ad_settings_group',
			'trx_ad_compare_page_id',
			array(
				'type'              => 'integer',
				'sanitize_callback' => 'absint',
				'default'           => 0,
			)
		);
		register_setting(
			'trx_ad_settings_group',
			'trx_ad_favorites_page_id',
			array(
				'type'              => 'integer',
				'sanitize_callback' => 'absint',
				'default'           => 0,
			)
		);

		add_settings_section(
			'trx_ad_compare_favorites',
			__( 'Compare & Favorites', 'trx-auto-dealer' ),
			array( $this, 'renderSectionDescription' ),
			$page
		);

		add_settings_field(
			'enable_compare',
			__( 'Enable Compare', 'trx-auto-dealer' ),
			array( $this, 'renderEnableCompare' ),
			$page,
			'trx_ad_compare_favorites'
		);

		add_settings_field(
			'compare_page',
			__( 'Compare page', 'trx-auto-dealer' ),
			array( $this, 'renderComparePage' ),
			$page,
			'trx_ad_compare_favorites'
		);

		add_settings_field(
			'enable_favorites',
			__( 'Enable Favorites', 'trx-auto-dealer' ),
			array( $this, 'renderEnableFavorites' ),
			$page,
			'trx_ad_compare_favorites'
		);

		add_settings_field(
			'favorites_page',
			__( 'Favorites page', 'trx-auto-dealer' ),
			array( $this, 'renderFavoritesPage' ),
			$page,
			'trx_ad_compare_favorites'
		);
	}

	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_compare_favorites_tab]" value="1">
			<?php
			do_settings_sections( AdminSettings::PAGE_SLUG . '_compare_favorites' );
			submit_button();
			?>
		</form>
		<?php
	}

	public function renderSectionDescription(): void {
		printf(
			'<p>%s</p>',
			esc_html__( 'Compare lets visitors stack up to 4 vehicles side by side. Favorites lets visitors bookmark vehicles to revisit later (anonymously in free; per-user with Pro). Both pages and their shortcodes were created during activation.', 'trx-auto-dealer' )
		);
	}

	public function renderEnableCompare(): void {
		$settings = AdminSettings::getSettings();
		$enabled  = ! empty( $settings['enable_compare'] );
		printf(
			'<label><input type="checkbox" name="%1$s[enable_compare]" 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 Compare button on vehicle cards and the floating compare bar.', 'trx-auto-dealer' ),
			esc_html__( 'When off, the [trx_ad_compare_listings] shortcode still works but no UI surfaces it.', 'trx-auto-dealer' )
		);
	}

	public function renderEnableFavorites(): void {
		$settings = AdminSettings::getSettings();
		$enabled  = ! empty( $settings['enable_favorites'] );
		printf(
			'<label><input type="checkbox" name="%1$s[enable_favorites]" 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 heart toggle on vehicle cards.', 'trx-auto-dealer' ),
			esc_html__( 'When off, the [trx_ad_favorites] shortcode still works but cards no longer expose the save action.', 'trx-auto-dealer' )
		);
	}

	public function renderComparePage(): void {
		$page_id = (int) get_option( 'trx_ad_compare_page_id', 0 );
		$this->renderPagePicker( 'trx_ad_compare_page_id', $page_id, __( 'Page that hosts [trx_ad_compare_listings].', 'trx-auto-dealer' ) );
	}

	public function renderFavoritesPage(): void {
		$page_id = (int) get_option( 'trx_ad_favorites_page_id', 0 );
		$this->renderPagePicker( 'trx_ad_favorites_page_id', $page_id, __( 'Page that hosts [trx_ad_favorites].', 'trx-auto-dealer' ) );
	}

	private function renderPagePicker( string $name, int $current, string $description ): void {
		$pages = get_pages( array( 'post_status' => 'publish' ) );
		printf( '<select name="%s">', esc_attr( $name ) );
		printf( '<option value="0">%s</option>', esc_html__( '— None —', 'trx-auto-dealer' ) );
		if ( is_array( $pages ) ) {
			foreach ( $pages as $page ) {
				printf(
					'<option value="%d"%s>%s</option>',
					(int) $page->ID,
					selected( $current, (int) $page->ID, false ),
					esc_html( $page->post_title )
				);
			}
		}
		echo '</select>';
		if ( $current > 0 ) {
			printf(
				' <a href="%s" target="_blank" rel="noopener">%s</a>',
				esc_url( (string) get_permalink( $current ) ),
				esc_html__( 'View page', 'trx-auto-dealer' )
			);
		}
		printf( '<p class="description">%s</p>', esc_html( $description ) );

		// Stand-alone option used by the helper code, not part of the settings array.
		// Write it now via a small hidden field hook on save.
		// Actual save uses register_setting on these individual option keys below.
	}
}
