<?php
/**
 * Maps tab — provider selection + Google key + zoom + cluster.
 *
 * 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 MapsTab extends AbstractSettingsTab {

	public const OPTION_GROUP = 'trx_ad_maps_group';

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

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

	public function register(): void {
		register_setting(
			self::OPTION_GROUP,
			AdminSettings::OPTION_KEY,
			array(
				'type'              => 'array',
				'sanitize_callback' => array( AdminSettings::class, 'sanitize' ),
			)
		);

		$page = AdminSettings::PAGE_SLUG . '_maps';

		add_settings_section(
			'trx_ad_maps',
			__( 'Map Settings', 'trx-auto-dealer' ),
			array( $this, 'renderSectionDescription' ),
			$page
		);

		$fields = array(
			array( 'map_provider', __( 'Map Provider', 'trx-auto-dealer' ), 'renderMapProvider' ),
			array( 'google_maps_api_key', __( 'Google Maps API Key', 'trx-auto-dealer' ), 'renderGoogleMapsApiKey' ),
			array( 'map_default_zoom', __( 'Default Zoom', 'trx-auto-dealer' ), 'renderMapDefaultZoom' ),
			array( 'map_cluster', __( 'Cluster Markers', 'trx-auto-dealer' ), 'renderMapCluster' ),
		);
		foreach ( $fields as $f ) {
			add_settings_field( $f[0], $f[1], array( $this, $f[2] ), $page, 'trx_ad_maps' );
		}
	}

	public function render(): void {
		?>
		<form method="post" action="options.php">
			<?php
			settings_fields( self::OPTION_GROUP );
			// Hidden marker so sanitize() can distinguish "Maps form submitted
			// with cluster off" from "Maps form not submitted at all".
			echo '<input type="hidden" name="' . esc_attr( AdminSettings::OPTION_KEY ) . '[trx_ad_maps_tab]" value="1">';
			do_settings_sections( AdminSettings::PAGE_SLUG . '_maps' );
			submit_button();
			?>
		</form>
		<?php
	}

	public function renderSectionDescription(): void {
		echo '<p>';
		esc_html_e( 'Configure map provider and geocoding for dealer and vehicle locations.', 'trx-auto-dealer' );
		echo '</p>';
	}

	public function renderMapProvider(): void {
		$settings = AdminSettings::getSettings();
		$current  = (string) ( $settings['map_provider'] ?? 'none' );

		printf( '<select name="%s[map_provider]" id="trx_ad_map_provider">', esc_attr( AdminSettings::OPTION_KEY ) );
		$options = array(
			'none'    => __( 'Disabled', 'trx-auto-dealer' ),
			'leaflet' => __( 'OpenStreetMap (Leaflet)', 'trx-auto-dealer' ),
			'google'  => __( 'Google Maps', 'trx-auto-dealer' ),
		);
		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__( 'OpenStreetMap is free and requires no API key. Google Maps requires a Maps JavaScript API key.', 'trx-auto-dealer' )
		);
	}

	public function renderGoogleMapsApiKey(): void {
		$settings = AdminSettings::getSettings();
		printf(
			'<input type="text" name="%s[google_maps_api_key]" id="trx_ad_google_maps_api_key" value="%s" class="regular-text" autocomplete="off">'
			. '<p class="description">%s</p>',
			esc_attr( AdminSettings::OPTION_KEY ),
			esc_attr( (string) ( $settings['google_maps_api_key'] ?? '' ) ),
			esc_html__( 'Google Maps JavaScript API key. Required when "Google Maps" is selected above.', 'trx-auto-dealer' )
		);
	}

	public function renderMapDefaultZoom(): void {
		$settings = AdminSettings::getSettings();
		printf(
			'<input type="number" name="%s[map_default_zoom]" id="trx_ad_map_default_zoom" value="%d" min="1" max="20" class="small-text">'
			. '<p class="description">%s</p>',
			esc_attr( AdminSettings::OPTION_KEY ),
			(int) ( $settings['map_default_zoom'] ?? 12 ),
			esc_html__( 'Default zoom level for single-pin maps (1=world, 20=street).', 'trx-auto-dealer' )
		);
	}

	public function renderMapCluster(): void {
		$settings = AdminSettings::getSettings();
		$checked  = ! empty( $settings['map_cluster'] );
		printf(
			'<label><input type="checkbox" name="%s[map_cluster]" id="trx_ad_map_cluster" value="1"%s> %s</label>'
			. '<p class="description">%s</p>',
			esc_attr( AdminSettings::OPTION_KEY ),
			$checked ? ' checked' : '',
			esc_html__( 'Group nearby pins into clusters', 'trx-auto-dealer' ),
			esc_html__( 'Recommended when displaying many vehicles or dealers on a single map.', 'trx-auto-dealer' )
		);
	}
}
