<?php
/**
 * Admin settings page for Local SEO.
 *
 * @package SEOvaultSEO\LocalSEO
 */

namespace SEOvaultSEO\LocalSEO;

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

class AdminPage {

	public static function init() {
		add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
	}

	public static function register_settings() {
		register_setting(
			'seovault_local_settings_group',
			'seovaultseo_local_settings',
			array(
				'sanitize_callback' => array( __CLASS__, 'sanitize_settings' ),
				'autoload'          => false,
			)
		);
	}

	public static function sanitize_settings( $input ) {
		if ( ! is_array( $input ) ) {
			return array();
		}

		$clean = array();

		$clean['enabled'] = isset( $input['enabled'] ) ? 'on' : '';

		$text_fields = array(
			'business_name',
			'business_address',
			'business_address_2',
			'business_city',
			'business_state',
			'business_zipcode',
			'business_phone',
			'business_phone_2nd',
			'business_fax',
			'business_price_range',
			'business_currencies_accepted',
			'business_payment_accepted',
			'business_area_served',
			'business_vat',
			'business_tax',
			'business_contact_phone',
			'coordinates_lat',
			'coordinates_long',
		);

		foreach ( $text_fields as $field ) {
			$clean[ $field ] = isset( $input[ $field ] ) ? sanitize_text_field( $input[ $field ] ) : '';
		}

		$clean['business_country'] = isset( $input['business_country'] ) && Countries::is_valid( $input['business_country'] )
			? sanitize_text_field( $input['business_country'] )
			: '';

		$clean['business_email']         = isset( $input['business_email'] ) ? sanitize_email( $input['business_email'] ) : '';
		$clean['business_contact_email'] = isset( $input['business_contact_email'] ) ? sanitize_email( $input['business_contact_email'] ) : '';
		$clean['business_url']           = isset( $input['business_url'] ) && filter_var( $input['business_url'], FILTER_VALIDATE_URL ) ? esc_url_raw( $input['business_url'] ) : '';
		$clean['maps_api_key']           = isset( $input['maps_api_key'] ) ? sanitize_text_field( $input['maps_api_key'] ) : '';

		$clean['business_type'] = isset( $input['business_type'] ) && BusinessTypes::is_valid( $input['business_type'] )
			? sanitize_text_field( $input['business_type'] )
			: '';

		$clean['unit_system'] = isset( $input['unit_system'] ) && in_array( $input['unit_system'], array( 'METRIC', 'IMPERIAL' ), true )
			? $input['unit_system']
			: 'METRIC';

		$clean['sl_num_results'] = isset( $input['sl_num_results'] ) ? absint( $input['sl_num_results'] ) : 10;

		$bool_fields = array(
			'open_247',
			'hide_opening_hours',
			'multiple_opening_hours',
			'format_24h',
		);
		foreach ( $bool_fields as $field ) {
			$clean[ $field ] = isset( $input[ $field ] ) ? 'on' : '';
		}

		$days = OpeningHours::get_days();
		foreach ( $days as $day ) {
			$clean[ 'opening_hours_' . $day . '_from' ]        = isset( $input[ 'opening_hours_' . $day . '_from' ] ) ? LocationMeta::sanitize_time( $input[ 'opening_hours_' . $day . '_from' ] ) : '';
			$clean[ 'opening_hours_' . $day . '_to' ]          = isset( $input[ 'opening_hours_' . $day . '_to' ] ) ? LocationMeta::sanitize_time( $input[ 'opening_hours_' . $day . '_to' ] ) : '';
			$clean[ 'opening_hours_' . $day . '_second_from' ] = isset( $input[ 'opening_hours_' . $day . '_second_from' ] ) ? LocationMeta::sanitize_time( $input[ 'opening_hours_' . $day . '_second_from' ] ) : '';
			$clean[ 'opening_hours_' . $day . '_second_to' ]   = isset( $input[ 'opening_hours_' . $day . '_second_to' ] ) ? LocationMeta::sanitize_time( $input[ 'opening_hours_' . $day . '_second_to' ] ) : '';
			$clean[ 'opening_hours_' . $day . '_24h' ]         = isset( $input[ 'opening_hours_' . $day . '_24h' ] ) ? 'on' : '';
		}

		return $clean;
	}

	public static function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$settings = LocalSEOInit::get_settings();
		$pt       = PostType::get_post_type();
		?>
		<div class="wrap">
			<h1><?php echo esc_html__( 'Local SEO Settings', 'seovault-ai' ); ?></h1>

			<div class="nav-tab-wrapper">
				<a href="#seovault-local-business" class="nav-tab nav-tab-active"><?php esc_html_e( 'Business Info', 'seovault-ai' ); ?></a>
				<a href="#seovault-local-hours" class="nav-tab"><?php esc_html_e( 'Opening Hours', 'seovault-ai' ); ?></a>
				<a href="#seovault-local-maps" class="nav-tab"><?php esc_html_e( 'Maps & Store Locator', 'seovault-ai' ); ?></a>
				<a href="#seovault-local-import" class="nav-tab"><?php esc_html_e( 'Import / Export', 'seovault-ai' ); ?></a>
			</div>

			<form method="post" action="options.php">
				<?php settings_fields( 'seovault_local_settings_group' ); ?>

				<div id="seovault-local-business" class="seovault-local-tab">
					<table class="form-table">
						<tr>
							<th scope="row"><label for="local_enabled"><?php esc_html_e( 'Enable Local SEO', 'seovault-ai' ); ?></label></th>
							<td>
								<?php self::render_toggle( 'seovaultseo_local_settings[enabled]', __( 'Enable Local SEO features including schema, geo tags, and shortcodes.', 'seovault-ai' ), $settings['enabled'] ?? '', 'local_enabled' ); ?>
								<p class="description"><?php esc_html_e( 'Enable Local SEO features including schema, geo tags, and shortcodes.', 'seovault-ai' ); ?></p>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="business_type"><?php esc_html_e( 'Business Type', 'seovault-ai' ); ?></label></th>
							<td>
								<select name="seovaultseo_local_settings[business_type]" id="business_type" class="seovaultseo-select">
									<option value=""><?php esc_html_e( 'Select a type', 'seovault-ai' ); ?></option>
									<?php
									$types        = BusinessTypes::flatten();
									$current_type = $settings['business_type'] ?? '';
									foreach ( $types as $key => $label ) {
										echo '<option value="' . esc_attr( $key ) . '" ' . selected( $current_type, $key, false ) . '>' . esc_html( $label ) . '</option>';
									}
									?>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="business_name"><?php esc_html_e( 'Business Name', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_name]" id="business_name" class="regular-text" value="<?php echo esc_attr( $settings['business_name'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_address"><?php esc_html_e( 'Street Address', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_address]" id="business_address" class="regular-text" value="<?php echo esc_attr( $settings['business_address'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_address_2"><?php esc_html_e( 'Address Line 2', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_address_2]" id="business_address_2" class="regular-text" value="<?php echo esc_attr( $settings['business_address_2'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_city"><?php esc_html_e( 'City', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_city]" id="business_city" class="regular-text" value="<?php echo esc_attr( $settings['business_city'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_state"><?php esc_html_e( 'State / Province', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_state]" id="business_state" class="regular-text" value="<?php echo esc_attr( $settings['business_state'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_zipcode"><?php esc_html_e( 'Postal Code', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_zipcode]" id="business_zipcode" class="regular-text" value="<?php echo esc_attr( $settings['business_zipcode'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_country"><?php esc_html_e( 'Country', 'seovault-ai' ); ?></label></th>
							<td>
								<select name="seovaultseo_local_settings[business_country]" id="business_country" class="seovaultseo-select">
									<option value=""><?php esc_html_e( 'Choose a country', 'seovault-ai' ); ?></option>
									<?php
									$countries       = Countries::get_all();
									$current_country = $settings['business_country'] ?? '';
									foreach ( $countries as $code => $name ) {
										if ( '' === $code ) {
											continue;
										}
										echo '<option value="' . esc_attr( $code ) . '" ' . selected( $current_country, $code, false ) . '>' . esc_html( $name ) . '</option>';
									}
									?>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="business_phone"><?php esc_html_e( 'Phone', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_phone]" id="business_phone" class="regular-text" value="<?php echo esc_attr( $settings['business_phone'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_phone_2nd"><?php esc_html_e( 'Secondary Phone', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_phone_2nd]" id="business_phone_2nd" class="regular-text" value="<?php echo esc_attr( $settings['business_phone_2nd'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_fax"><?php esc_html_e( 'Fax', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_fax]" id="business_fax" class="regular-text" value="<?php echo esc_attr( $settings['business_fax'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_email"><?php esc_html_e( 'Email', 'seovault-ai' ); ?></label></th>
							<td><input type="email" name="seovaultseo_local_settings[business_email]" id="business_email" class="regular-text" value="<?php echo esc_attr( $settings['business_email'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_url"><?php esc_html_e( 'Website URL', 'seovault-ai' ); ?></label></th>
							<td><input type="url" name="seovaultseo_local_settings[business_url]" id="business_url" class="regular-text" value="<?php echo esc_attr( $settings['business_url'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="coordinates_lat"><?php esc_html_e( 'Latitude', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[coordinates_lat]" id="coordinates_lat" class="regular-text" value="<?php echo esc_attr( $settings['coordinates_lat'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="coordinates_long"><?php esc_html_e( 'Longitude', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[coordinates_long]" id="coordinates_long" class="regular-text" value="<?php echo esc_attr( $settings['coordinates_long'] ?? '' ); ?>"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_price_range"><?php esc_html_e( 'Price Range', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_price_range]" id="business_price_range" class="regular-text" value="<?php echo esc_attr( $settings['business_price_range'] ?? '' ); ?>" placeholder="$$"></td>
						</tr>
						<tr>
							<th scope="row"><label for="business_area_served"><?php esc_html_e( 'Area Served', 'seovault-ai' ); ?></label></th>
							<td><input type="text" name="seovaultseo_local_settings[business_area_served]" id="business_area_served" class="regular-text" value="<?php echo esc_attr( $settings['business_area_served'] ?? '' ); ?>"></td>
						</tr>
					</table>
				</div>

				<div id="seovault-local-hours" class="seovault-local-tab" style="display:none;">
					<table class="form-table">
						<tr>
							<th scope="row"><?php esc_html_e( '24/7 Open', 'seovault-ai' ); ?></th>
							<td>
								<label class="seovaultseo-check">
									<input type="checkbox" name="seovaultseo_local_settings[open_247]" value="on" <?php checked( $settings['open_247'] ?? '', 'on' ); ?>>
									<span></span>
								</label>
								<span class="description"><?php esc_html_e( 'Check if your business is open 24 hours, 7 days a week.', 'seovault-ai' ); ?></span>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Hide Opening Hours', 'seovault-ai' ); ?></th>
							<td>
								<label class="seovaultseo-check">
									<input type="checkbox" name="seovaultseo_local_settings[hide_opening_hours]" value="on" <?php checked( $settings['hide_opening_hours'] ?? '', 'on' ); ?>>
									<span></span>
								</label>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php esc_html_e( 'Multiple Time Slots', 'seovault-ai' ); ?></th>
							<td>
								<label class="seovaultseo-check">
									<input type="checkbox" name="seovaultseo_local_settings[multiple_opening_hours]" value="on" <?php checked( $settings['multiple_opening_hours'] ?? '', 'on' ); ?>>
									<span></span>
								</label>
								<span class="description"><?php esc_html_e( 'Enable if your business has split hours (e.g. morning and afternoon).', 'seovault-ai' ); ?></span>
							</td>
						</tr>
					</table>

					<h3><?php esc_html_e( 'Opening Hours', 'seovault-ai' ); ?></h3>
					<table class="widefat striped">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Day', 'seovault-ai' ); ?></th>
								<th><?php esc_html_e( 'First Open', 'seovault-ai' ); ?></th>
								<th><?php esc_html_e( 'First Close', 'seovault-ai' ); ?></th>
								<th><?php esc_html_e( 'Second Open', 'seovault-ai' ); ?></th>
								<th><?php esc_html_e( 'Second Close', 'seovault-ai' ); ?></th>
								<th><?php esc_html_e( '24h', 'seovault-ai' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$days = OpeningHours::get_days();
							foreach ( $days as $day ) :
								$from  = $settings[ 'opening_hours_' . $day . '_from' ] ?? '';
								$to    = $settings[ 'opening_hours_' . $day . '_to' ] ?? '';
								$sfrom = $settings[ 'opening_hours_' . $day . '_second_from' ] ?? '';
								$sto   = $settings[ 'opening_hours_' . $day . '_second_to' ] ?? '';
								$h24   = $settings[ 'opening_hours_' . $day . '_24h' ] ?? '';
								?>
								<tr>
									<td><strong><?php echo esc_html( OpeningHours::get_day_full( $day ) ); ?></strong></td>
									<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_from]" value="<?php echo esc_attr( $from ); ?>"></td>
									<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_to]" value="<?php echo esc_attr( $to ); ?>"></td>
									<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_second_from]" value="<?php echo esc_attr( $sfrom ); ?>"></td>
									<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_second_to]" value="<?php echo esc_attr( $sto ); ?>"></td>
									<td><label class="seovaultseo-check"><input type="checkbox" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_24h]" value="on" <?php checked( $h24, 'on' ); ?>><span></span></label></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>

				<div id="seovault-local-maps" class="seovault-local-tab" style="display:none;">
					<table class="form-table">
						<tr>
							<th scope="row"><label for="maps_api_key"><?php esc_html_e( 'Google Maps API Key', 'seovault-ai' ); ?></label></th>
							<td>
								<input type="text" name="seovaultseo_local_settings[maps_api_key]" id="maps_api_key" class="regular-text" value="<?php echo esc_attr( $settings['maps_api_key'] ?? '' ); ?>">
								<p class="description"><?php esc_html_e( 'Required for map shortcodes and store locator.', 'seovault-ai' ); ?></p>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="unit_system"><?php esc_html_e( 'Unit System', 'seovault-ai' ); ?></label></th>
							<td>
								<select name="seovaultseo_local_settings[unit_system]" id="unit_system" class="seovaultseo-select seovaultseo-select-small">
									<option value="METRIC" <?php selected( $settings['unit_system'] ?? 'METRIC', 'METRIC' ); ?>><?php esc_html_e( 'Metric (km)', 'seovault-ai' ); ?></option>
									<option value="IMPERIAL" <?php selected( $settings['unit_system'] ?? '', 'IMPERIAL' ); ?>><?php esc_html_e( 'Imperial (miles)', 'seovault-ai' ); ?></option>
								</select>
							</td>
						</tr>
						<tr>
							<th scope="row"><label for="sl_num_results"><?php esc_html_e( 'Store Locator: Max Results', 'seovault-ai' ); ?></label></th>
							<td>
								<input type="number" name="seovaultseo_local_settings[sl_num_results]" id="sl_num_results" min="1" max="100" value="<?php echo esc_attr( $settings['sl_num_results'] ?? 10 ); ?>">
							</td>
						</tr>
					</table>
					<p class="description">
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: 1: opening anchor tag, 2: KML URL, 3: closing anchor tag. */
								__( 'KML sitemap available at: %1$s%2$s%3$s', 'seovault-ai' ),
								'<a href="' . esc_url( KmlSitemap::get_kml_url() ) . '">',
								esc_html( KmlSitemap::get_kml_url() ),
								'</a>'
							)
						);
						?>
					</p>
				</div>

				<div id="seovault-local-import" class="seovault-local-tab" style="display:none;">
					<?php ImportExport::render_import_export_section(); ?>
				</div>

				<?php submit_button( __( 'Save Local SEO Settings', 'seovault-ai' ) ); ?>
			</form>

		</div>
		<?php
	}

	public static function render_tab() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$settings = LocalSEOInit::get_settings();
		$pt       = PostType::get_post_type();
		?>
		<h2><?php esc_html_e( 'Local SEO', 'seovault-ai' ); ?></h2>

		<div class="nav-tab-wrapper">
			<a href="#seovault-local-business" class="nav-tab nav-tab-active"><?php esc_html_e( 'Business Info', 'seovault-ai' ); ?></a>
			<a href="#seovault-local-hours" class="nav-tab"><?php esc_html_e( 'Opening Hours', 'seovault-ai' ); ?></a>
			<a href="#seovault-local-maps" class="nav-tab"><?php esc_html_e( 'Maps & Store Locator', 'seovault-ai' ); ?></a>
			<a href="#seovault-local-import" class="nav-tab"><?php esc_html_e( 'Import / Export', 'seovault-ai' ); ?></a>
		</div>

		<div id="seovault-local-business" class="seovault-local-tab">
				<table class="form-table">
					<tr>
						<th scope="row"><label for="local_enabled"><?php esc_html_e( 'Enable Local SEO', 'seovault-ai' ); ?></label></th>
						<td>
							<?php self::render_toggle( 'seovaultseo_local_settings[enabled]', __( 'Enable Local SEO features including schema, geo tags, and shortcodes.', 'seovault-ai' ), $settings['enabled'] ?? '', 'local_enabled' ); ?>
							<p class="description"><?php esc_html_e( 'Enable Local SEO features including schema, geo tags, and shortcodes.', 'seovault-ai' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="business_type"><?php esc_html_e( 'Business Type', 'seovault-ai' ); ?></label></th>
						<td>
							<select name="seovaultseo_local_settings[business_type]" id="business_type" class="seovaultseo-select">
								<option value=""><?php esc_html_e( 'Select a type', 'seovault-ai' ); ?></option>
								<?php
								$types        = BusinessTypes::flatten();
								$current_type = $settings['business_type'] ?? '';
								foreach ( $types as $key => $label ) {
									echo '<option value="' . esc_attr( $key ) . '" ' . selected( $current_type, $key, false ) . '>' . esc_html( $label ) . '</option>';
								}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="business_name"><?php esc_html_e( 'Business Name', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_name]" id="business_name" class="regular-text" value="<?php echo esc_attr( $settings['business_name'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_address"><?php esc_html_e( 'Street Address', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_address]" id="business_address" class="regular-text" value="<?php echo esc_attr( $settings['business_address'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_address_2"><?php esc_html_e( 'Address Line 2', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_address_2]" id="business_address_2" class="regular-text" value="<?php echo esc_attr( $settings['business_address_2'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_city"><?php esc_html_e( 'City', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_city]" id="business_city" class="regular-text" value="<?php echo esc_attr( $settings['business_city'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_state"><?php esc_html_e( 'State / Province', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_state]" id="business_state" class="regular-text" value="<?php echo esc_attr( $settings['business_state'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_zipcode"><?php esc_html_e( 'Postal Code', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_zipcode]" id="business_zipcode" class="regular-text" value="<?php echo esc_attr( $settings['business_zipcode'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_country"><?php esc_html_e( 'Country', 'seovault-ai' ); ?></label></th>
						<td>
							<select name="seovaultseo_local_settings[business_country]" id="business_country" class="seovaultseo-select">
								<option value=""><?php esc_html_e( 'Choose a country', 'seovault-ai' ); ?></option>
								<?php
								$countries       = Countries::get_all();
								$current_country = $settings['business_country'] ?? '';
								foreach ( $countries as $code => $name ) {
									if ( '' === $code ) {
										continue;
									}
									echo '<option value="' . esc_attr( $code ) . '" ' . selected( $current_country, $code, false ) . '>' . esc_html( $name ) . '</option>';
								}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="business_phone"><?php esc_html_e( 'Phone', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_phone]" id="business_phone" class="regular-text" value="<?php echo esc_attr( $settings['business_phone'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_phone_2nd"><?php esc_html_e( 'Secondary Phone', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_phone_2nd]" id="business_phone_2nd" class="regular-text" value="<?php echo esc_attr( $settings['business_phone_2nd'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_fax"><?php esc_html_e( 'Fax', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_fax]" id="business_fax" class="regular-text" value="<?php echo esc_attr( $settings['business_fax'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_email"><?php esc_html_e( 'Email', 'seovault-ai' ); ?></label></th>
						<td><input type="email" name="seovaultseo_local_settings[business_email]" id="business_email" class="regular-text" value="<?php echo esc_attr( $settings['business_email'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_url"><?php esc_html_e( 'Website URL', 'seovault-ai' ); ?></label></th>
						<td><input type="url" name="seovaultseo_local_settings[business_url]" id="business_url" class="regular-text" value="<?php echo esc_attr( $settings['business_url'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="coordinates_lat"><?php esc_html_e( 'Latitude', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[coordinates_lat]" id="coordinates_lat" class="regular-text" value="<?php echo esc_attr( $settings['coordinates_lat'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="coordinates_long"><?php esc_html_e( 'Longitude', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[coordinates_long]" id="coordinates_long" class="regular-text" value="<?php echo esc_attr( $settings['coordinates_long'] ?? '' ); ?>"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_price_range"><?php esc_html_e( 'Price Range', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_price_range]" id="business_price_range" class="regular-text" value="<?php echo esc_attr( $settings['business_price_range'] ?? '' ); ?>" placeholder="$$"></td>
					</tr>
					<tr>
						<th scope="row"><label for="business_area_served"><?php esc_html_e( 'Area Served', 'seovault-ai' ); ?></label></th>
						<td><input type="text" name="seovaultseo_local_settings[business_area_served]" id="business_area_served" class="regular-text" value="<?php echo esc_attr( $settings['business_area_served'] ?? '' ); ?>"></td>
					</tr>
				</table>
			</div>

			<div id="seovault-local-hours" class="seovault-local-tab" style="display:none;">
				<table class="form-table">
					<tr>
						<th scope="row"><?php esc_html_e( '24/7 Open', 'seovault-ai' ); ?></th>
						<td>
							<label class="seovaultseo-check">
								<input type="checkbox" name="seovaultseo_local_settings[open_247]" value="on" <?php checked( $settings['open_247'] ?? '', 'on' ); ?>>
								<span></span>
							</label>
							<span class="description"><?php esc_html_e( 'Check if your business is open 24 hours, 7 days a week.', 'seovault-ai' ); ?></span>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Hide Opening Hours', 'seovault-ai' ); ?></th>
						<td>
							<label class="seovaultseo-check">
								<input type="checkbox" name="seovaultseo_local_settings[hide_opening_hours]" value="on" <?php checked( $settings['hide_opening_hours'] ?? '', 'on' ); ?>>
								<span></span>
							</label>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Multiple Time Slots', 'seovault-ai' ); ?></th>
						<td>
							<label class="seovaultseo-check">
								<input type="checkbox" name="seovaultseo_local_settings[multiple_opening_hours]" value="on" <?php checked( $settings['multiple_opening_hours'] ?? '', 'on' ); ?>>
								<span></span>
							</label>
							<span class="description"><?php esc_html_e( 'Enable if your business has split hours (e.g. morning and afternoon).', 'seovault-ai' ); ?></span>
						</td>
					</tr>
				</table>

				<h3><?php esc_html_e( 'Opening Hours', 'seovault-ai' ); ?></h3>
				<table class="widefat striped">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Day', 'seovault-ai' ); ?></th>
							<th><?php esc_html_e( 'First Open', 'seovault-ai' ); ?></th>
							<th><?php esc_html_e( 'First Close', 'seovault-ai' ); ?></th>
							<th><?php esc_html_e( 'Second Open', 'seovault-ai' ); ?></th>
							<th><?php esc_html_e( 'Second Close', 'seovault-ai' ); ?></th>
							<th><?php esc_html_e( '24h', 'seovault-ai' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
						$days = OpeningHours::get_days();
						foreach ( $days as $day ) :
							$from  = $settings[ 'opening_hours_' . $day . '_from' ] ?? '';
							$to    = $settings[ 'opening_hours_' . $day . '_to' ] ?? '';
							$sfrom = $settings[ 'opening_hours_' . $day . '_second_from' ] ?? '';
							$sto   = $settings[ 'opening_hours_' . $day . '_second_to' ] ?? '';
							$h24   = $settings[ 'opening_hours_' . $day . '_24h' ] ?? '';
							?>
							<tr>
								<td><strong><?php echo esc_html( OpeningHours::get_day_full( $day ) ); ?></strong></td>
								<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_from]" value="<?php echo esc_attr( $from ); ?>"></td>
								<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_to]" value="<?php echo esc_attr( $to ); ?>"></td>
								<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_second_from]" value="<?php echo esc_attr( $sfrom ); ?>"></td>
								<td><input type="time" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_second_to]" value="<?php echo esc_attr( $sto ); ?>"></td>
								<td><label class="seovaultseo-check"><input type="checkbox" name="seovaultseo_local_settings[opening_hours_<?php echo esc_attr( $day ); ?>_24h]" value="on" <?php checked( $h24, 'on' ); ?>><span></span></label></td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>

			<div id="seovault-local-maps" class="seovault-local-tab" style="display:none;">
				<table class="form-table">
					<tr>
						<th scope="row"><label for="maps_api_key"><?php esc_html_e( 'Google Maps API Key', 'seovault-ai' ); ?></label></th>
						<td>
							<input type="text" name="seovaultseo_local_settings[maps_api_key]" id="maps_api_key" class="regular-text" value="<?php echo esc_attr( $settings['maps_api_key'] ?? '' ); ?>">
							<p class="description"><?php esc_html_e( 'Required for map shortcodes and store locator.', 'seovault-ai' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="unit_system"><?php esc_html_e( 'Unit System', 'seovault-ai' ); ?></label></th>
						<td>
							<select name="seovaultseo_local_settings[unit_system]" id="unit_system" class="seovaultseo-select seovaultseo-select-small">
								<option value="METRIC" <?php selected( $settings['unit_system'] ?? 'METRIC', 'METRIC' ); ?>><?php esc_html_e( 'Metric (km)', 'seovault-ai' ); ?></option>
								<option value="IMPERIAL" <?php selected( $settings['unit_system'] ?? '', 'IMPERIAL' ); ?>><?php esc_html_e( 'Imperial (miles)', 'seovault-ai' ); ?></option>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="sl_num_results"><?php esc_html_e( 'Store Locator: Max Results', 'seovault-ai' ); ?></label></th>
						<td>
							<input type="number" name="seovaultseo_local_settings[sl_num_results]" id="sl_num_results" min="1" max="100" value="<?php echo esc_attr( $settings['sl_num_results'] ?? 10 ); ?>">
						</td>
					</tr>
				</table>
				<p class="description">
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: 1: KML sitemap URL, 2: KML sitemap URL used as link text. */
							__( 'KML sitemap available at: <a href="%1$s">%2$s</a>', 'seovault-ai' ),
							esc_url( KmlSitemap::get_kml_url() ),
							esc_html( KmlSitemap::get_kml_url() )
						)
					);
					?>
				</p>
			</div>

			<div id="seovault-local-import" class="seovault-local-tab" style="display:none;">
				<?php ImportExport::render_import_export_section(); ?>
			</div>

		<?php
	}

	/**
	 * Render an animated toggle switch.
	 *
	 * @param string $name    The input name attribute.
	 * @param string $label   The visible label text.
	 * @param bool   $checked Whether the toggle is on.
	 * @param string $id      Optional. The input id attribute (defaults to $name).
	 */
	private static function render_toggle( $name, $label, $checked, $id = '' ) {
		if ( empty( $id ) ) {
			$id = $name;
		}
		?>
		<div class="seovaultseo-toggle-row">
			<label class="seovaultseo-switch">
				<input type="checkbox" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $id ); ?>" value="on" <?php checked( $checked, 'on' ); ?> />
				<span class="seovaultseo-slider"></span>
			</label>
			<label for="<?php echo esc_attr( $id ); ?>" class="seovaultseo-toggle-label"><?php echo esc_html( $label ); ?></label>
		</div>
		<?php
	}
}
