<?php
/**
 * Settings section for Noesia Smart Waiter.
 *
 * Bridge template: defaults and registry discovery are prepared by a helper
 * service while this file remains the presentation layer for legacy panels.
 *
 * @package Noesia_Smart_Waiter
 */

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

$noesia_swa_settings_parent_options = ( isset( $options ) && is_array( $options ) ) ? $options : null;
$noesia_swa_settings_section_service = null;

if ( function_exists( 'noesia_swa' ) ) {
	$noesia_swa_plugin = noesia_swa();

	if ( is_object( $noesia_swa_plugin ) && isset( $noesia_swa_plugin->settings_section_data ) ) {
		$noesia_swa_settings_section_service = $noesia_swa_plugin->settings_section_data;
	}
}

if ( ! $noesia_swa_settings_section_service instanceof Noesia_SWA_Settings_Section_Data && class_exists( 'Noesia_SWA_Settings_Section_Data' ) ) {
	$noesia_swa_settings_section_service = new Noesia_SWA_Settings_Section_Data();
}

$noesia_swa_settings_section_context = ( $noesia_swa_settings_section_service instanceof Noesia_SWA_Settings_Section_Data )
	? $noesia_swa_settings_section_service->get_context( $noesia_swa_settings_parent_options )
	: array(
		'options' => is_array( $noesia_swa_settings_parent_options ) ? $noesia_swa_settings_parent_options : array(),
		'values' => is_array( $noesia_swa_settings_parent_options ) ? $noesia_swa_settings_parent_options : array(),
		'settings_panels_registry' => null,
	);

( static function ( $noesia_swa_settings_section_context, $noesia_swa_settings_section_service ) {
	$options = isset( $noesia_swa_settings_section_context['options'] ) && is_array( $noesia_swa_settings_section_context['options'] ) ? $noesia_swa_settings_section_context['options'] : array();
	$values = isset( $noesia_swa_settings_section_context['values'] ) && is_array( $noesia_swa_settings_section_context['values'] ) ? $noesia_swa_settings_section_context['values'] : array();
	$settings_panels_registry = isset( $noesia_swa_settings_section_context['settings_panels_registry'] ) ? $noesia_swa_settings_section_context['settings_panels_registry'] : null;
	$normalize_yes_no_flag = static function ( $value ) use ( &$normalize_yes_no_flag ) {
		if ( is_array( $value ) ) {
			foreach ( array_reverse( $value ) as $item ) {
				if ( 'yes' === $normalize_yes_no_flag( $item ) ) {
					return 'yes';
				}
			}

			return 'no';
		}

		$safe = sanitize_key( wp_unslash( (string) $value ) );

		return in_array( $safe, array( 'yes', '1', 'true', 'on' ), true ) ? 'yes' : 'no';
	};

	$canonical_options = get_option( 'noesia_swa_options', array() );

	if ( is_array( $canonical_options ) && array_key_exists( 'delete_all_data_on_uninstall', $canonical_options ) ) {
		$values['delete_all_data_on_uninstall'] = $canonical_options['delete_all_data_on_uninstall'];
	}

	$world_live_enabled = isset( $values['dashboard_world_live_data_enabled'] ) && 'yes' === sanitize_key( (string) $values['dashboard_world_live_data_enabled'] );
	$delete_all_data_on_uninstall_enabled = isset( $values['delete_all_data_on_uninstall'] ) && 'yes' === $normalize_yes_no_flag( $values['delete_all_data_on_uninstall'] );
	$world_live_consented_at_raw = isset( $values['dashboard_world_live_data_consented_at'] ) ? sanitize_text_field( (string) $values['dashboard_world_live_data_consented_at'] ) : '';
	$world_live_consented_at_label = '';

	if ( '' !== $world_live_consented_at_raw ) {
		$world_live_consented_at_ts = strtotime( $world_live_consented_at_raw );

		if ( false !== $world_live_consented_at_ts ) {
			$world_live_consented_at_label = wp_date( 'M j, Y H:i', $world_live_consented_at_ts );
		}
	}

	$uploads_directory = wp_upload_dir();
	$uploads_base_dir = isset( $uploads_directory['basedir'] ) ? (string) $uploads_directory['basedir'] : '';
	$uploads_writable = '' !== $uploads_base_dir && wp_is_writable( $uploads_base_dir );
	$site_scheme = wp_parse_url( home_url( '/' ), PHP_URL_SCHEME );
	$https_active = is_ssl() || 'https' === strtolower( (string) $site_scheme );
	$php_version_supported = version_compare( PHP_VERSION, '7.4', '>=' );
	$memory_limit_raw = defined( 'WP_MEMORY_LIMIT' ) ? (string) WP_MEMORY_LIMIT : (string) ini_get( 'memory_limit' );
	$memory_limit_bytes = wp_convert_hr_to_bytes( $memory_limit_raw );
	$memory_limit_label = $memory_limit_bytes > 0 ? size_format( (int) $memory_limit_bytes ) : $memory_limit_raw;
	$memory_recommended = $memory_limit_bytes >= 268435456;

$render_profile_info_panel = static function ( $panel = array(), $registry = null ) use ( $values ) {
	?>
	<div class="noesia-swa-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'PROFILE INFO', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'This replaces the old clinic-style identity block. The dashboard greeting will use the The Host value when available.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-business-name"><?php esc_html_e( 'Business Name', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-business-name"
					name="noesia_swa_options[profile_business_name]"
					value="<?php echo esc_attr( $values['profile_business_name'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. Noesia Smart Waiter Lounge', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-host-name"><?php esc_html_e( 'The Host', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-host-name"
					name="noesia_swa_options[profile_host_name]"
					value="<?php echo esc_attr( $values['profile_host_name'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. Alexander', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Used in the dashboard greeting: Good morning, Good afternoon or Good evening.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-business-type"><?php esc_html_e( 'Business Type', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-business-type"
					name="noesia_swa_options[profile_business_type]"
					value="<?php echo esc_attr( $values['profile_business_type'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. Restaurant, Hotel, Café, Bar', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-phone"><?php esc_html_e( 'Phone', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-phone"
					name="noesia_swa_options[profile_phone]"
					value="<?php echo esc_attr( $values['profile_phone'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. +30 210 0000000', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-email"><?php esc_html_e( 'Email', 'noesia-smart-waiter' ); ?></label>
				<input
					type="email"
					id="noesia-swa-profile-email"
					name="noesia_swa_options[profile_email]"
					value="<?php echo esc_attr( $values['profile_email'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. hello@example.com', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-website"><?php esc_html_e( 'Website', 'noesia-smart-waiter' ); ?></label>
				<input
					type="url"
					id="noesia-swa-profile-website"
					name="noesia_swa_options[profile_website]"
					value="<?php echo esc_attr( $values['profile_website'] ); ?>"
					placeholder="https://"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-tax-id"><?php esc_html_e( 'Tax ID / VAT No.', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-tax-id"
					name="noesia_swa_options[profile_tax_id]"
					value="<?php echo esc_attr( $values['profile_tax_id'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. VAT123456789', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-profile-registration-no"><?php esc_html_e( 'Business Registration No.', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-profile-registration-no"
					name="noesia_swa_options[profile_registration_no]"
					value="<?php echo esc_attr( $values['profile_registration_no'] ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. CRN, EIN, VAT registration, or License ID', 'noesia-smart-waiter' ); ?>"
					autocomplete="off"
				/>
			</div>

			<div class="noesia-swa-form-group noesia-swa-card--span2">
				<label for="noesia-swa-profile-address"><?php esc_html_e( 'Full Address', 'noesia-smart-waiter' ); ?></label>
				<textarea
					id="noesia-swa-profile-address"
					name="noesia_swa_options[profile_address]"
					rows="3"
					placeholder="<?php esc_attr_e( 'Street, Number, City, Zip...', 'noesia-smart-waiter' ); ?>"
				><?php echo esc_textarea( $values['profile_address'] ); ?></textarea>
			</div>

	</div></div>
	<?php
};

$render_live_privacy_panel = static function ( $panel = array(), $registry = null ) use ( $world_live_enabled, $world_live_consented_at_label ) {
	?>
	<div class="noesia-swa-card noesia-swa-settings-live-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'PRIVACY-FIRST LIVE DATA', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Choose whether Feel the World can contact its live providers on the server side for weather, earthquakes, disasters, orbit telemetry and world brief updates.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-settings-consent-card">
			<div class="noesia-swa-settings-consent-copy">
				<h4><?php esc_html_e( 'Enable Feel the World Live Providers', 'noesia-smart-waiter' ); ?></h4>
				<p class="noesia-swa-muted"><?php esc_html_e( 'No third-party scripts are loaded in the browser for this feature. Requests are handled server-side and cached locally where possible.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-settings-consent-row">
				<div class="noesia-swa-settings-consent-toggle-copy">
					<strong><?php esc_html_e( 'I explicitly enable external live providers for Feel the World', 'noesia-smart-waiter' ); ?></strong>
					<small><?php esc_html_e( 'This consent is recorded in plugin settings and may be changed later by an administrator.', 'noesia-smart-waiter' ); ?></small>
				</div>
				<div class="noesia-swa-settings-consent-action">
					<span class="noesia-swa-settings-consent-action-label"><?php esc_html_e( 'Live data', 'noesia-smart-waiter' ); ?></span>
					<div class="noesia-swa-settings-consent-toggle-row">
						<input type="hidden" name="noesia_swa_options[dashboard_world_live_data_enabled]" value="no" />
						<label class="noesia-swa-settings-consent-toggle" for="noesia-swa-dashboard-world-live-data-enabled">
							<input
								type="checkbox"
								id="noesia-swa-dashboard-world-live-data-enabled"
								name="noesia_swa_options[dashboard_world_live_data_enabled]"
								value="yes"
								<?php checked( $world_live_enabled ); ?>
							/>
							<span class="noesia-swa-settings-consent-toggle-ui" aria-hidden="true"></span>
						</label>
						<span class="noesia-swa-settings-consent-status" aria-live="polite">
							<span class="noesia-swa-settings-consent-status-off"><?php esc_html_e( 'Off', 'noesia-smart-waiter' ); ?></span>
							<span class="noesia-swa-settings-consent-status-on"><?php esc_html_e( 'On', 'noesia-smart-waiter' ); ?></span>
						</span>
					</div>
				</div>
			</div>

			<div class="noesia-swa-settings-chip-list" aria-label="<?php esc_attr_e( 'Live data architecture', 'noesia-smart-waiter' ); ?>">
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Server-side only', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'No third-party browser scripts', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Cached with transients', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Admin controlled', 'noesia-smart-waiter' ); ?></span>
			</div>

			<p class="noesia-swa-muted"><?php esc_html_e( 'Providers currently used by Feel the World: Open-Meteo, Google News RSS, USGS, GDACS and wheretheiss.at. No plugin can guarantee legal compliance by itself; this setting is a privacy-conscious control for administrators.', 'noesia-smart-waiter' ); ?></p>

			<?php if ( '' !== $world_live_consented_at_label ) : ?>
				<div class="noesia-swa-settings-consent-meta">
					<span><?php esc_html_e( 'Last recorded consent', 'noesia-smart-waiter' ); ?></span>
					<strong><?php echo esc_html( $world_live_consented_at_label ); ?></strong>
				</div>
			<?php endif; ?>
		</div>
	</div>
	<?php
};

$render_morning_briefing_panel = static function ( $panel = array(), $registry = null ) use ( $values ) {
	?>
	<div class="noesia-swa-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'GENERAL UI SETTINGS', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Choose what the global active badge shows in the top bar, the dashboard, and the screen saver overlay.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<?php
$dashboard_stats_source_value = '';
if ( isset( $values['dashboard_stats_source'] ) ) {
	$dashboard_stats_source_value = sanitize_key( $values['dashboard_stats_source'] );
}

if ( '' === $dashboard_stats_source_value ) {
	$dashboard_stats_source_value = isset( $values['global_active_badge_type'] )
		? sanitize_key( $values['global_active_badge_type'] )
		: 'requests';
}

if ( ! in_array( $dashboard_stats_source_value, array( 'requests', 'reminders', 'appointments' ), true ) ) {
	$dashboard_stats_source_value = 'requests';
}
?>

<div class="noesia-swa-form-grid">
	<div class="noesia-swa-form-group">
		<label for="noesia-swa-dashboard-stats-source">
			<?php esc_html_e( 'Dashboard Stats Source', 'noesia-smart-waiter' ); ?>
		</label>

		<select id="noesia-swa-dashboard-stats-source" name="noesia_swa_options[dashboard_stats_source]">
			<option value="requests" <?php selected( $dashboard_stats_source_value, 'requests' ); ?>>
				<?php esc_html_e( 'Requests', 'noesia-smart-waiter' ); ?>
			</option>
			<option value="reminders" <?php selected( $dashboard_stats_source_value, 'reminders' ); ?>>
				<?php esc_html_e( 'Reminders', 'noesia-smart-waiter' ); ?>
			</option>
			<option value="appointments" <?php selected( $dashboard_stats_source_value, 'appointments' ); ?>>
				<?php esc_html_e( 'Appointments', 'noesia-smart-waiter' ); ?>
			</option>
		</select>

		<p class="noesia-swa-muted">
			<?php esc_html_e( 'Controls the counter text in Morning Briefing (e.g. “Today you have 3 …”).', 'noesia-smart-waiter' ); ?>
		</p>
	</div>
</div>
	</div>
	<?php
};

$render_security_access_panel = static function ( $panel = array(), $registry = null ) use ( $delete_all_data_on_uninstall_enabled ) {
	?>
	<div class="noesia-swa-card noesia-swa-settings-live-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'SECURITY AND ACCESS', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Manage high-impact security and uninstall data policies for this workspace.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-settings-consent-card noesia-swa-settings-consent-card--spaced">
			<div class="noesia-swa-settings-consent-copy">
				<h4><?php esc_html_e( 'Full Wipe on Plugin Uninstall', 'noesia-smart-waiter' ); ?></h4>
				<p class="noesia-swa-muted"><?php esc_html_e( 'By default, deleting the plugin preserves Noesia data for safe reinstall, Free-to-Pro activation, Pro-to-Free downgrade, and recovery flows.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-settings-consent-row">
				<div class="noesia-swa-settings-consent-toggle-copy">
					<strong><?php esc_html_e( 'Delete all Noesia data on uninstall', 'noesia-smart-waiter' ); ?></strong>
					<small><?php esc_html_e( 'Warning: if enabled, deleting the plugin will permanently remove Noesia settings, module data, custom tables, uploads, private files, logs, archives, and related records. This action cannot be undone.', 'noesia-smart-waiter' ); ?></small>
				</div>
				<div class="noesia-swa-settings-consent-action">
					<span class="noesia-swa-settings-consent-action-label"><?php esc_html_e( 'Uninstall policy', 'noesia-smart-waiter' ); ?></span>
					<div class="<?php echo esc_attr( $delete_all_data_on_uninstall_enabled ? 'noesia-swa-settings-consent-toggle-row noesia-swa-settings-consent-toggle-row--enabled' : 'noesia-swa-settings-consent-toggle-row' ); ?>">
						<input type="hidden" name="noesia_swa_options[delete_all_data_on_uninstall_control_present]" value="1" form="noesia-swa-settings-form" />
						<label class="noesia-swa-settings-consent-toggle" for="noesia-swa-delete-all-data-on-uninstall">
							<input
								type="checkbox"
								id="noesia-swa-delete-all-data-on-uninstall"
								name="noesia_swa_options[delete_all_data_on_uninstall]"
								value="yes"
								form="noesia-swa-settings-form"
								<?php checked( $delete_all_data_on_uninstall_enabled ); ?>
							/>
							<span class="noesia-swa-settings-consent-toggle-ui" aria-hidden="true"></span>
						</label>
						<span class="noesia-swa-settings-consent-status" aria-live="polite">
							<span class="noesia-swa-settings-consent-status-off"><?php esc_html_e( 'Preserve', 'noesia-smart-waiter' ); ?></span>
							<span class="noesia-swa-settings-consent-status-on"><?php esc_html_e( 'Full wipe', 'noesia-smart-waiter' ); ?></span>
						</span>
					</div>
				</div>
			</div>

				<p class="noesia-swa-muted"><?php esc_html_e( 'Recommended for production: leave this OFF so uninstall preserves customer data and future reinstall or Freemius tier changes can restore the workspace intact.', 'noesia-smart-waiter' ); ?></p>
		</div>
	</div>
	<?php
};

$render_business_pulse_panel = static function ( $panel = array(), $registry = null ) use ( $values ) {
	?>
	<div class="noesia-swa-card noesia-swa-settings-live-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'BUSINESS PULSE - News - Weather', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'These settings control the dashboard weather and news widgets.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-form-group">
				<label for="noesia-swa-news-language"><?php esc_html_e( 'Dashboard Language', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-news-language" name="noesia_swa_options[news_language]">
					<option value="en" <?php selected( $values['news_language'], 'en' ); ?>><?php esc_html_e( 'English', 'noesia-smart-waiter' ); ?></option>
					<option value="el" <?php selected( $values['news_language'], 'el' ); ?>><?php esc_html_e( 'Greek', 'noesia-smart-waiter' ); ?></option>
					<option value="fr" <?php selected( $values['news_language'], 'fr' ); ?>><?php esc_html_e( 'French', 'noesia-smart-waiter' ); ?></option>
					<option value="de" <?php selected( $values['news_language'], 'de' ); ?>><?php esc_html_e( 'German', 'noesia-smart-waiter' ); ?></option>
					<option value="it" <?php selected( $values['news_language'], 'it' ); ?>><?php esc_html_e( 'Italian', 'noesia-smart-waiter' ); ?></option>
					<option value="es" <?php selected( $values['news_language'], 'es' ); ?>><?php esc_html_e( 'Spanish', 'noesia-smart-waiter' ); ?></option>
				</select>
				<p class="noesia-swa-muted"><?php esc_html_e( 'General News and Business News will follow this language.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-news-country"><?php esc_html_e( 'News Country', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-news-country"
					name="noesia_swa_options[news_country]"
					value="<?php echo esc_attr( isset( $values['news_country'] ) ? $values['news_country'] : '' ); ?>"
					maxlength="2"
				/>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Optional 2-letter code for What Matters Now and News feeds, for example GR, US, FR, DE.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-weather-city"><?php esc_html_e( 'Weather City', 'noesia-smart-waiter' ); ?></label>
				<input
					type="text"
					id="noesia-swa-weather-city"
					name="noesia_swa_options[weather_city]"
					value="<?php echo esc_attr( $values['weather_city'] ); ?>"
				/>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Example: Athens, Corfu, London, Rome.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-row">
				<div class="noesia-swa-form-group noesia-swa-form-group--small">
					<label for="noesia-swa-weather-country"><?php esc_html_e( 'Country Code', 'noesia-smart-waiter' ); ?></label>
					<input
						type="text"
						id="noesia-swa-weather-country"
						name="noesia_swa_options[weather_country]"
						value="<?php echo esc_attr( $values['weather_country'] ); ?>"
						maxlength="2"
					/>
					<p class="noesia-swa-muted"><?php esc_html_e( 'Use a 2-letter code such as GR, US, UK, IT, FR.', 'noesia-smart-waiter' ); ?></p>
				</div>

				<div class="noesia-swa-form-group">
					<label for="noesia-swa-weather-units"><?php esc_html_e( 'Weather Units', 'noesia-smart-waiter' ); ?></label>
					<select id="noesia-swa-weather-units" name="noesia_swa_options[weather_units]">
						<option value="metric" <?php selected( $values['weather_units'], 'metric' ); ?>><?php esc_html_e( 'Metric (°C)', 'noesia-smart-waiter' ); ?></option>
						<option value="imperial" <?php selected( $values['weather_units'], 'imperial' ); ?>><?php esc_html_e( 'Imperial (°F)', 'noesia-smart-waiter' ); ?></option>
					</select>
				</div>
			</div>

			<div class="noesia-swa-form-group noesia-swa-card--span2">
				<label for="noesia-swa-dashboard-world-cities"><?php esc_html_e( 'Cities In Motion', 'noesia-smart-waiter' ); ?></label>
				<textarea
					id="noesia-swa-dashboard-world-cities"
					name="noesia_swa_options[dashboard_world_cities]"
					rows="4"
					placeholder="<?php esc_attr_e( 'One city per line: Athens or Tokyo, JP', 'noesia-smart-waiter' ); ?>"
				><?php echo esc_textarea( isset( $values['dashboard_world_cities'] ) ? $values['dashboard_world_cities'] : '' ); ?></textarea>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Up to 4 lines. Just type a city name. You can optionally add a country code like Tokyo, JP or New York, US. If you leave this empty, Noesia uses your main weather city.', 'noesia-smart-waiter' ); ?></p>
			</div>

		</div>
	</div>
	<?php
};

$render_screen_saver_panel = static function ( $panel = array(), $registry = null ) use ( $values ) {
	?>
	<div class="noesia-swa-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'SCREEN SAVER', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Configure the automatic idle screen saver. This is separate from the Screen Lock module.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-form-group">
				<label for="noesia-swa-screensaver-enabled"><?php esc_html_e( 'Enable Screen Saver', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-screensaver-enabled" name="noesia_swa_options[screensaver_enabled]">
					<option value="yes" <?php selected( $values['screensaver_enabled'], 'yes' ); ?>><?php esc_html_e( 'Enabled', 'noesia-smart-waiter' ); ?></option>
					<option value="no" <?php selected( $values['screensaver_enabled'], 'no' ); ?>><?php esc_html_e( 'Disabled', 'noesia-smart-waiter' ); ?></option>
				</select>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-screensaver-idle-minutes"><?php esc_html_e( 'Idle Timeout (minutes)', 'noesia-smart-waiter' ); ?></label>
				<input
					type="number"
					id="noesia-swa-screensaver-idle-minutes"
					name="noesia_swa_options[screensaver_idle_minutes]"
					value="<?php echo esc_attr( $values['screensaver_idle_minutes'] ); ?>"
					min="1"
					max="120"
					step="1"
				/>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Recommended: 3 to 10 minutes.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-screensaver-show-clock"><?php esc_html_e( 'Show Clock', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-screensaver-show-clock" name="noesia_swa_options[screensaver_show_clock]">
					<option value="yes" <?php selected( $values['screensaver_show_clock'], 'yes' ); ?>><?php esc_html_e( 'Yes', 'noesia-smart-waiter' ); ?></option>
					<option value="no" <?php selected( $values['screensaver_show_clock'], 'no' ); ?>><?php esc_html_e( 'No', 'noesia-smart-waiter' ); ?></option>
				</select>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-screensaver-show-updates"><?php esc_html_e( 'Show Updates Counter', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-screensaver-show-updates" name="noesia_swa_options[screensaver_show_updates]">
					<option value="yes" <?php selected( $values['screensaver_show_updates'], 'yes' ); ?>><?php esc_html_e( 'Yes', 'noesia-smart-waiter' ); ?></option>
					<option value="no" <?php selected( $values['screensaver_show_updates'], 'no' ); ?>><?php esc_html_e( 'No', 'noesia-smart-waiter' ); ?></option>
				</select>
			</div>

			<div class="noesia-swa-form-group">
				<label for="noesia-swa-screensaver-show-center-box"><?php esc_html_e( 'Show Center Box', 'noesia-smart-waiter' ); ?></label>
				<select id="noesia-swa-screensaver-show-center-box" name="noesia_swa_options[screensaver_show_center_box]">
					<option value="yes" <?php selected( isset( $values['screensaver_show_center_box'] ) ? $values['screensaver_show_center_box'] : 'yes', 'yes' ); ?>><?php esc_html_e( 'Yes', 'noesia-smart-waiter' ); ?></option>
					<option value="no" <?php selected( isset( $values['screensaver_show_center_box'] ) ? $values['screensaver_show_center_box'] : 'yes', 'no' ); ?>><?php esc_html_e( 'No', 'noesia-smart-waiter' ); ?></option>
				</select>
			</div>
		</div>

		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( 'Screen Saver Behavior', 'noesia-smart-waiter' ); ?></strong>
			<span><?php esc_html_e( 'The Screen Saver can display the clock and the active counter selected in General UI Settings (Requests, Reminders, or Appointments).', 'noesia-smart-waiter' ); ?></span>
		</div>

		<?php do_action( 'noesia_swa_screen_saver_settings_panel', $values, $panel, $registry ); ?>
	</div>
	<?php
};

$render_server_security_info_panel = static function ( $panel = array(), $registry = null ) {
		$apache_rule = implode(
	"\n",
	array(
		'# Noesia Smart Waiter - OCR Runtime Allow Rule',
		'Options -Indexes',
		'',
		'<IfModule mod_mime.c>',
		'    AddType application/javascript .js',
		'    AddType text/javascript .mjs',
		'    AddType application/wasm .wasm',
		'    AddType application/json .json',
		'</IfModule>',
		'',
		'<FilesMatch "\\.(js|mjs|wasm|json|data)$">',
		'    <IfModule mod_authz_core.c>',
		'        Require all granted',
		'    </IfModule>',
		'</FilesMatch>',
		'',
		'<IfModule mod_headers.c>',
		'    Header set X-Content-Type-Options "nosniff"',
		'    Header set Cache-Control "public, max-age=31536000, immutable"',
		'</IfModule>',
		'',
	)
);

$nginx_rule = implode(
	"\n",
	array(
		'# Noesia Smart Waiter - OCR Runtime Allow Rule',
		'location ^~ /wp-content/uploads/noesia-smart-waiter/vision-ocr/ {',
		'    autoindex off;',
		'    try_files $uri =404;',
		'',
		'    types {',
		'        application/javascript js mjs;',
		'        application/wasm wasm;',
		'        application/json json;',
		'    }',
		'',
		'    add_header X-Content-Type-Options "nosniff" always;',
		'    add_header Cache-Control "public, max-age=31536000, immutable" always;',
		'}',
		'',
	)
);
	?>
	<div class="noesia-swa-card noesia-swa-card--span2">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'SERVER SECURITY INFO', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Private Noesia upload folders are designed to be protected automatically on standard hosting environments that support Apache or LiteSpeed access rules.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-status-box">
				<strong><?php esc_html_e( 'Automatic Protection (Most Hosts)', 'noesia-smart-waiter' ); ?></strong>
				<p><?php esc_html_e( 'Noesia Smart Waiter is designed to automatically protect private files such as Clinical documents, Smart Pocket media, and Screen Recordings in standard hosting environments that support Apache or LiteSpeed .htaccess rules. This commonly includes many shared hosting environments from providers such as Bluehost, GoDaddy, Hostinger, and SiteGround.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-status-box">
				<strong><?php esc_html_e( 'Pure Nginx Servers (Advanced Infrastructures)', 'noesia-smart-waiter' ); ?></strong>
				<p><?php esc_html_e( 'Pure Nginx servers do not process .htaccess files. If your infrastructure runs on a custom pure Nginx stack, you or your server administrator should apply equivalent server-level protection rules for Noesia private upload directories.', 'noesia-smart-waiter' ); ?></p>
				<strong><?php esc_html_e( 'What To Do (Verification)', 'noesia-smart-waiter' ); ?></strong>
				<ol>
					<li><?php esc_html_e( 'Copy a direct file URL from one of your private modules, such as a Clinical Workspace document.', 'noesia-smart-waiter' ); ?></li>
					<li><?php esc_html_e( 'Paste it into an incognito browser window.', 'noesia-smart-waiter' ); ?></li>
					<li><?php esc_html_e( 'If you see a "403 Forbidden" message or a "Page Not Found" response, your private file protection appears to be active. If the file opens directly, contact your hosting provider or server administrator to apply equivalent Nginx directory protection.', 'noesia-smart-waiter' ); ?></li>
				</ol>
			</div>
		</div>
		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( 'Advanced OCR Runtime Compatibility (Vision)', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'In most standard environments the Vision OCR runtime works without extra action. If you see a 403 Forbidden response or a nosniff MIME error for Tesseract runtime files, your server is blocking JavaScript or WASM execution inside the OCR runtime uploads path.', 'noesia-smart-waiter' ); ?></p>
			<p><strong><?php esc_html_e( 'Target path:', 'noesia-smart-waiter' ); ?></strong> <code>/wp-content/uploads/noesia-smart-waiter/vision-ocr/</code></p>
			<p><?php esc_html_e( 'Apply rules only to the OCR runtime path above. Do not open your entire uploads directory and do not apply these rules to private Clinical or other protected data folders.', 'noesia-smart-waiter' ); ?></p>
		</div>

		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-form-group noesia-swa-card--span2">
				<label for="noesia-swa-vision-ocr-apache-rule"><?php esc_html_e( 'Apache / LiteSpeed rule', 'noesia-smart-waiter' ); ?></label>
				<textarea id="noesia-swa-vision-ocr-apache-rule" rows="14" readonly><?php echo esc_textarea( $apache_rule ); ?></textarea>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Create a .htaccess file inside the OCR runtime path and paste this rule only if your OCR runtime fails to load.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-form-group noesia-swa-card--span2">
				<label for="noesia-swa-vision-ocr-nginx-rule"><?php esc_html_e( 'Nginx rule', 'noesia-smart-waiter' ); ?></label>
				<textarea id="noesia-swa-vision-ocr-nginx-rule" rows="14" readonly><?php echo esc_textarea( $nginx_rule ); ?></textarea>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Add this location block only to the Vision OCR runtime path if your infrastructure runs on Nginx and the runtime is blocked.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>
	</div>
	<?php
};
$render_troubleshooting_panel = static function ( $panel = array(), $registry = null ) use ( $https_active, $php_version_supported, $uploads_writable, $memory_limit_label, $memory_recommended ) {
	?>
	<div class="noesia-swa-card noesia-swa-card--span2">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'DIAGNOSTIC & HEALTH CENTER', 'noesia-smart-waiter' ); ?></div>
				<h3><?php esc_html_e( 'Troubleshooting', 'noesia-smart-waiter' ); ?></h3>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Run quick environment checks, clear common cache conflicts, and review the first-response steps before opening a support ticket.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>
		<div class="noesia-swa-form-grid">
			<div class="noesia-swa-status-box">
				<strong><?php echo esc_html( $https_active ? '🔒 ' . __( 'HTTPS Status: Active', 'noesia-smart-waiter' ) : '⚠️ ' . __( 'HTTPS Status: Check Required', 'noesia-smart-waiter' ) ); ?></strong>
				<span><?php echo esc_html( $https_active ? __( 'Secure HTTPS is available. Camera, microphone, OCR, and browser permission flows can initialize correctly. Important: If Vision OCR stays on Scanning or Reading text…, or your browser shows blocked insecure content, make sure your WordPress Address and Site Address use HTTPS, clear CDN and browser cache, open a fresh tab, and then retry the OCR runtime download.', 'noesia-smart-waiter' ) : __( 'HTTPS is required for camera, microphone, OCR, and many browser permission flows. Enable HTTPS before troubleshooting modules that use device access.', 'noesia-smart-waiter' ) ); ?></span>
			</div>
			<div class="noesia-swa-status-box">
				<?php if ( $php_version_supported ) : ?>
	<strong>
		<?php
		/* translators: %s: current PHP version. */
		echo esc_html( sprintf( __( '🧩 PHP Runtime: %s', 'noesia-smart-waiter' ), PHP_VERSION ) );
		?>
	</strong>
<?php else : ?>
	<strong>
		<?php
		/* translators: %s: current PHP version. */
		echo esc_html( sprintf( __( '⚠️ PHP Runtime: %s', 'noesia-smart-waiter' ), PHP_VERSION ) );
		?>
	</strong>
<?php endif; ?>
				<span><?php echo esc_html( $php_version_supported ? __( 'Your current PHP version is within the supported range for the plugin baseline.', 'noesia-smart-waiter' ) : __( 'Noesia Smart Waiter requires PHP 7.4 or higher. Update PHP before production use.', 'noesia-smart-waiter' ) ); ?></span>
			</div>

			<div class="noesia-swa-status-box">
				<strong><?php echo esc_html( $uploads_writable ? __( '📁 Uploads Directory: Writable', 'noesia-smart-waiter' ) : __( '⚠️ Uploads Directory: Check Permissions', 'noesia-smart-waiter' ) ); ?></strong>
				<span><?php echo esc_html( $uploads_writable ? __( 'The WordPress uploads directory is writable. Protected file storage, screenshots, recordings, and generated assets can be created.', 'noesia-smart-waiter' ) : __( 'The uploads directory does not appear writable. File uploads, screenshots, OCR packages, and media generation may fail until permissions are corrected.', 'noesia-smart-waiter' ) ); ?></span>
			</div>

			<div class="noesia-swa-status-box">
				<?php if ( $memory_recommended ) : ?>
	<strong>
		<?php
		/* translators: %s: detected WordPress memory limit. */
		echo esc_html( sprintf( __( '⚙️ Memory Budget: %s', 'noesia-smart-waiter' ), $memory_limit_label ) );
		?>
	</strong>
<?php else : ?>
	<strong>
		<?php
		/* translators: %s: detected WordPress memory limit. */
		echo esc_html( sprintf( __( '⚠️ Memory Budget: %s', 'noesia-smart-waiter' ), $memory_limit_label ) );
		?>
	</strong>
<?php endif; ?>
				<span><?php echo esc_html( $memory_recommended ? __( 'The detected WordPress memory budget is healthy for most dashboard, media, and OCR tasks.', 'noesia-smart-waiter' ) : __( 'For larger media workflows and OCR tasks, 256MB or more is recommended. Low memory can cause incomplete processing or admin failures.', 'noesia-smart-waiter' ) ); ?></span>
			</div>
		</div>

		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( '🛠️ Performance & Display Conflicts', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'If the dashboard looks broken, cards do not refresh, or a module suddenly stops loading after an update, follow these steps in order before opening a support ticket.', 'noesia-smart-waiter' ); ?></p>
			<ol>
				<li><?php esc_html_e( 'Trigger a full CDN or reverse-proxy purge if you use Cloudflare, Sucuri, LiteSpeed Cache, or another edge caching layer.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Clear the browser cache or perform a hard reload. For testing, close the current dashboard tab fully and open a fresh tab or an incognito window.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Temporarily disable aggressive speed, minify, combine, defer, or admin-side cache plugins if the shell or a module stops rendering correctly.', 'noesia-smart-waiter' ); ?></li>
			</ol>
			<div class="noesia-swa-settings-chip-list" aria-label="<?php esc_attr_e( 'Recommended first-response actions', 'noesia-smart-waiter' ); ?>">
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Purge CDN cache', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Hard reload browser', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Open fresh tab / incognito', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Disable minify or admin cache', 'noesia-smart-waiter' ); ?></span>
			</div>
		</div>

		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( '⚡ No Lazy Load Needed in the Admin Shell', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'Noesia Smart Waiter uses an event-driven shell and module-aware loading strategy. In the WordPress admin area it does not need third-party lazy loading, aggressive JavaScript minification, or dashboard HTML caching. Those optimizers can interfere with dynamic module bootstrapping, permissions flows, or background requests.', 'noesia-smart-waiter' ); ?></p>
		</div>

			<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( '📷 Camera, Recording, and OCR Modules', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'Clinical camera tools, Smart Pocket media, screen recording, Live Command device features, and Vision OCR depend on HTTPS, browser permissions, and a clean non-cached session. When testing these modules, always retry in a fresh tab after cache clearing before assuming a server fault.', 'noesia-smart-waiter' ); ?></p>
			<p><?php esc_html_e( 'If Vision OCR reports a 403 Forbidden or a nosniff runtime error, review the Server Security Info tab for the targeted OCR compatibility rules.', 'noesia-smart-waiter' ); ?></p>
		</div>

		<div class="noesia-swa-settings-help-card noesia-swa-settings-help-card--builders">
			<div class="noesia-swa-settings-help-card__header">
				<h3 class="noesia-swa-settings-help-card__title">
					<?php esc_html_e( '🧩 Page Builders (WPBakery, Elementor, Divi)', 'noesia-smart-waiter' ); ?>
				</h3>
			</div>

			<div class="noesia-swa-settings-help-card__body">
				<p class="noesia-swa-settings-help-card__text">
					<?php esc_html_e( 'If the Smart Waiter menu appears broken, or popups and modals are blocked, it is usually because the page builder is wrapping the app inside restrictive containers.', 'noesia-smart-waiter' ); ?>
				</p>

				<ul class="noesia-swa-settings-help-card__list">
					<li class="noesia-swa-settings-help-card__item">
						<strong><?php esc_html_e( 'WPBakery:', 'noesia-smart-waiter' ); ?></strong>
						<?php esc_html_e( 'Always place the shortcode inside a standard Text Block element. Avoid using the dedicated Shortcode widget, as it may break absolute positioning and z-index layers.', 'noesia-smart-waiter' ); ?>
					</li>

					<li class="noesia-swa-settings-help-card__item">
						<strong><?php esc_html_e( 'Elementor / Divi:', 'noesia-smart-waiter' ); ?></strong>
						<?php esc_html_e( 'Use the standard Shortcode or Text Editor widget. Make sure the column or section holding the app does not use overflow hidden, because it can clip menus, drawers, popups, and modal layers.', 'noesia-smart-waiter' ); ?>
					</li>
				</ul>
			</div>
		</div>

		<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( '🔐 Vault App Pairing & Sync', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'If the Vault App shows a pairing or sync error, remove the installed app from your Dock, Desktop, or device, then repeat the pairing process from the beginning with a fresh pairing code.', 'noesia-smart-waiter' ); ?></p>
			<ol>
				<li><?php esc_html_e( 'Close the Vault App completely on the device.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Remove the installed Vault App from your Dock, Desktop, or device.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Open the Vault App again from the Noesia Smart Waiter backend.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Generate a fresh pairing code and complete the pairing process again.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'After pairing completes, run Sync again and confirm the latest data appears normally.', 'noesia-smart-waiter' ); ?></li>
			</ol>
			<div class="noesia-swa-settings-chip-list" aria-label="<?php esc_attr_e( 'Vault troubleshooting actions', 'noesia-smart-waiter' ); ?>">
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Remove installed Vault App', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Open a fresh Vault App session', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Generate new pairing code', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Run sync again', 'noesia-smart-waiter' ); ?></span>
			</div>
		</div>	<?php
$noesia_swa_support_diagnostics_url = wp_nonce_url(
    admin_url( 'admin-post.php?action=noesia_swa_export_support_diagnostics' ),
    'noesia_swa_export_support_diagnostics',
    'noesia_swa_support_diagnostics_nonce'
);
?>
<div class="noesia-swa-status-box">
    <strong><?php esc_html_e( '🧰 Support Diagnostics Export', 'noesia-smart-waiter' ); ?></strong>
    <p><?php esc_html_e( 'Export a technical diagnostic snapshot and send it to Noesia support for faster troubleshooting.', 'noesia-smart-waiter' ); ?></p>
    <p><?php esc_html_e( 'The export is created locally on your site and is shared with Noesia only if you choose to send it.', 'noesia-smart-waiter' ); ?></p>
    <p><?php esc_html_e( 'This file is designed to include WordPress, PHP, server, theme template, active and inactive plugins, must-use plugins, drop-ins, Noesia modules, runtime flags, loaded PHP extensions, and selected non-sensitive module settings.', 'noesia-smart-waiter' ); ?></p>
    <p><?php esc_html_e( 'It should not include API keys, passwords, patient records, customer records, uploaded documents, or message content.', 'noesia-smart-waiter' ); ?></p>

    <div class="noesia-swa-settings-chip-list" aria-label="<?php esc_attr_e( 'Support diagnostics contents', 'noesia-smart-waiter' ); ?>">
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'Theme + template', 'noesia-smart-waiter' ); ?></span>
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'All plugins', 'noesia-smart-waiter' ); ?></span>
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'MU plugins + drop-ins', 'noesia-smart-waiter' ); ?></span>
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'Server + PHP runtime', 'noesia-smart-waiter' ); ?></span>
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'Noesia modules', 'noesia-smart-waiter' ); ?></span>
        <span class="noesia-swa-settings-chip"><?php esc_html_e( 'No API keys / No patient data', 'noesia-smart-waiter' ); ?></span>
    </div>

    <p>
        <a class="button button-primary" href="<?php echo esc_url( $noesia_swa_support_diagnostics_url ); ?>">
            <?php esc_html_e( 'Export Diagnostic Snapshot', 'noesia-smart-waiter' ); ?>
        </a>
    </p>
</div>
	<div class="noesia-swa-status-box">
			<strong><?php esc_html_e( '🤖 AI Clinical Advisor Troubleshooting', 'noesia-smart-waiter' ); ?></strong>
			<p><?php esc_html_e( 'If every Ask AI or Drug Check result uses the local fallback answer instead of the remote AI service, the site is not reaching the configured AI API correctly.', 'noesia-smart-waiter' ); ?></p>
			<ol>
				<li><?php esc_html_e( 'Check the saved OpenAI API key carefully and save it again.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'If needed, replace the current API key with a fresh valid key and test again.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Confirm that the selected model is available for that API key and account.', 'noesia-smart-waiter' ); ?></li>
				<li><?php esc_html_e( 'Check billing, quota, and any provider-side restrictions for the current API key.', 'noesia-smart-waiter' ); ?></li>
			</ol>
			<p><?php esc_html_e( 'When the remote AI service is working correctly, you should see a normal success message such as: AI answer generated successfully.', 'noesia-smart-waiter' ); ?></p>
			<p><?php esc_html_e( 'If the issue continues after checking or replacing the API key, please contact Noesia support.', 'noesia-smart-waiter' ); ?></p>
			<div class="noesia-swa-settings-chip-list" aria-label="<?php esc_attr_e( 'AI troubleshooting actions', 'noesia-smart-waiter' ); ?>">
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Check saved API key', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Replace API key if needed', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Confirm model access', 'noesia-smart-waiter' ); ?></span>
				<span class="noesia-swa-settings-chip"><?php esc_html_e( 'Review billing / quota', 'noesia-smart-waiter' ); ?></span>
			</div>
		</div>

	</div>
	<?php
};

$render_header_bookmarks_panel = static function ( $panel = array(), $registry = null ) use ( $values ) {
	?>
	<div class="noesia-swa-card noesia-swa-card--span2 noesia-swa-settings-hero-card">
		<div class="noesia-swa-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'HEADER BOOKMARKS', 'noesia-smart-waiter' ); ?></div>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Up to 8 quick links can appear inside the bookmarks dropdown in the header.', 'noesia-smart-waiter' ); ?></p>
			</div>
		</div>

		<div class="noesia-swa-bookmark-editor">
			<?php for ( $i = 1; $i <= 8; $i++ ) : ?>
				<div class="noesia-swa-bookmark-row">
					<div class="noesia-swa-form-group">
						<label for="noesia-swa-bookmark-<?php echo esc_attr( (string) $i ); ?>-name">
							<?php
							printf(
								/* translators: %d: bookmark slot number */
								esc_html__( 'Bookmark %d Name', 'noesia-smart-waiter' ),
								(int) $i
							);
							?>
						</label>
						<input
							type="text"
							id="noesia-swa-bookmark-<?php echo esc_attr( (string) $i ); ?>-name"
							name="noesia_swa_options[bookmark_<?php echo esc_attr( (string) $i ); ?>_name]"
							value="<?php echo esc_attr( $values[ 'bookmark_' . $i . '_name' ] ); ?>"
						/>
					</div>

					<div class="noesia-swa-form-group">
						<label for="noesia-swa-bookmark-<?php echo esc_attr( (string) $i ); ?>-url">
							<?php
							printf(
								/* translators: %d: bookmark slot number */
								esc_html__( 'Bookmark %d URL', 'noesia-smart-waiter' ),
								(int) $i
							);
							?>
						</label>
						<input
							type="url"
							id="noesia-swa-bookmark-<?php echo esc_attr( (string) $i ); ?>-url"
							name="noesia_swa_options[bookmark_<?php echo esc_attr( (string) $i ); ?>_url]"
							value="<?php echo esc_url( $values[ 'bookmark_' . $i . '_url' ] ); ?>"
						/>
					</div>
				</div>
			<?php endfor; ?>
		</div>
	</div>
	<?php
};


$render_dock_url_shortcuts_panel = static function ( $panel = array(), $registry = null ) {
	?>
	<div class="noesia-swa-card noesia-swa-card--span2 noesia-swa-dock-shortcuts-card" data-noesia-swa-dock-shortcuts-card="1">
		<div class="noesia-swa-card-header noesia-swa-dock-shortcuts-card-header">
			<div>
				<div class="noesia-swa-kicker"><?php esc_html_e( 'DOCK URL SHORTCUTS', 'noesia-smart-waiter' ); ?></div>
				<h3><?php esc_html_e( 'Quick Dock URL Shortcuts', 'noesia-smart-waiter' ); ?></h3>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Add shortcuts after the dock separator. Choose a preset with a ready link or enter your own website URL or supported deep link.', 'noesia-smart-waiter' ); ?></p>
			</div>
			<div class="noesia-swa-inline-actions">
				<button type="button" class="button" data-noesia-swa-action="resetDockShortcuts"><?php esc_html_e( 'Reset', 'noesia-smart-waiter' ); ?></button>
				<button type="button" class="button button-primary" data-noesia-swa-action="openDockShortcutModal"><?php esc_html_e( 'Add URL Shortcut', 'noesia-smart-waiter' ); ?></button>
			</div>
		</div>
		<div class="noesia-swa-dock-shortcuts-helper-row">
			<span class="noesia-swa-dock-shortcuts-helper-badge"><?php esc_html_e( 'Quick tips', 'noesia-smart-waiter' ); ?></span>
			<span class="noesia-swa-dock-shortcuts-helper-text"><?php esc_html_e( 'Core Noesia apps stay on the left. Your shortcuts render after the separator. Use normal website links such as https://mail.google.com for browser shortcuts. Use supported deep links such as viber://chatex only when your app or operating system already handles that target.', 'noesia-smart-waiter' ); ?></span>
		</div>
		<div class="noesia-swa-dock-shortcuts-info-box">
			<strong><?php esc_html_e( 'How to use the Target field', 'noesia-smart-waiter' ); ?></strong>
			<p class="noesia-swa-muted"><?php esc_html_e( 'Use a full website URL or a supported deep link. Do not use local app file paths. Ready examples:', 'noesia-smart-waiter' ); ?></p>
			<div class="noesia-swa-dock-shortcuts-example-grid">
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Chrome / Browser', 'noesia-smart-waiter' ); ?></strong><code>https://www.google.com/</code></span>
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Gmail', 'noesia-smart-waiter' ); ?></strong><code>https://mail.google.com/</code></span>
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Viber', 'noesia-smart-waiter' ); ?></strong><code>viber://chatex</code></span>
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Messenger', 'noesia-smart-waiter' ); ?></strong><code>https://www.messenger.com/</code></span>
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Canva', 'noesia-smart-waiter' ); ?></strong><code>https://www.canva.com/</code></span>
				<span class="noesia-swa-dock-shortcuts-example-pill"><strong><?php esc_html_e( 'Photoshop Web', 'noesia-smart-waiter' ); ?></strong><code>https://www.adobe.com/products/photoshop.html</code></span>
			</div>
		</div>
		<p class="noesia-swa-dock-shortcuts-helper-text">
			<?php esc_html_e( 'Examples: Chrome should use a website URL such as https://www.google.com. Viber can use a deep link such as viber://chatex.', 'noesia-smart-waiter' ); ?>
		</p>

		<div class="noesia-swa-dock-shortcuts-empty" data-noesia-swa-dock-shortcuts-empty="1" hidden>
			<p class="noesia-swa-muted"><?php esc_html_e( 'No custom URL shortcuts yet. Add your first shortcut to extend the dock.', 'noesia-smart-waiter' ); ?></p>
		</div>
		<div class="noesia-swa-dock-shortcuts-list" data-noesia-swa-dock-shortcuts-list="1"></div>
	</div>
	<?php
};

$legacy_panels = array(
	array(
		'id'       => 'legacy-profile-info',
		'title'    => __( 'Profile Info', 'noesia-smart-waiter' ),
		'group'    => 'general',
		'priority' => 10,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_profile_info_panel,
	),
	array(
		'id'       => 'legacy-live-privacy',
		'title'    => __( 'Privacy-First Live Data', 'noesia-smart-waiter' ),
		'group'    => 'general',
		'priority' => 15,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_live_privacy_panel,
	),
	array(
		'id'       => 'legacy-morning-briefing',
		'title'    => __( 'Morning Briefing', 'noesia-smart-waiter' ),
		'group'    => 'general',
		'priority' => 20,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_morning_briefing_panel,
	),
	array(
		'id'       => 'legacy-security-access',
		'title'    => __( 'Security and Access', 'noesia-smart-waiter' ),
		'group'    => 'general',
		'priority' => 25,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_security_access_panel,
	),
	array(
		'id'       => 'legacy-business-pulse',
		'title'    => __( 'BUSINESS PULSE - News - Weather', 'noesia-smart-waiter' ),
		'group'    => 'general',
		'priority' => 30,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_business_pulse_panel,
	),
	array(
		'id'       => 'legacy-screen-saver',
		'title'    => __( 'Screen Saver', 'noesia-smart-waiter' ),
		'group'    => 'automation',
		'priority' => 40,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'callback' => $render_screen_saver_panel,
	),
	array(
		'id'       => 'legacy-header-bookmarks',
		'title'    => __( 'Header Bookmarks', 'noesia-smart-waiter' ),
		'group'    => 'ui',
		'priority' => 50,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'class'    => 'noesia-swa-card--span2',
		'callback' => $render_header_bookmarks_panel,
	),
	array(
		'id'       => 'legacy-dock-url-shortcuts',
		'title'    => __( 'Dock URL Shortcuts', 'noesia-smart-waiter' ),
		'group'    => 'ui',
		'priority' => 55,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'class'    => 'noesia-swa-card--span2',
		'callback' => $render_dock_url_shortcuts_panel,
	),
	array(
		'id'       => 'legacy-troubleshooting',
		'title'    => __( 'Troubleshooting', 'noesia-smart-waiter' ),
		'group'    => 'advanced',
		'priority' => 1000,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'class'    => 'noesia-swa-card--span2',
		'callback' => $render_troubleshooting_panel,
	),
	array(
		'id'       => 'legacy-server-security-info',
		'title'    => __( 'Server Security Info', 'noesia-smart-waiter' ),
		'group'    => 'advanced',
		'priority' => 999,
		'tier'     => 'free',
		'owner'    => 'legacy',
		'class'    => 'noesia-swa-card--span2',
		'callback' => $render_server_security_info_panel,
	),
);

$rendered_via_registry = false;

if ( $settings_panels_registry instanceof Noesia_SWA_Settings_Panels ) {
	$settings_panels_registry->register_panels( $legacy_panels );
	$rendered_via_registry = true;
}
?>

<?php
$settings_tab_panels = array();

if ( $rendered_via_registry ) {
	$group_order = ( $noesia_swa_settings_section_service instanceof Noesia_SWA_Settings_Section_Data )
		? $noesia_swa_settings_section_service->get_group_order( $settings_panels_registry )
		: $settings_panels_registry->get_groups();

	foreach ( $group_order as $group ) {
		foreach ( $settings_panels_registry->get_panels( $group ) as $panel ) {
			if ( ! empty( $panel['id'] ) ) {
				$settings_tab_panels[ $panel['id'] ] = $panel;
			}
		}
	}

	foreach ( $settings_panels_registry->get_panels() as $panel ) {
		if ( ! empty( $panel['id'] ) && ! isset( $settings_tab_panels[ $panel['id'] ] ) ) {
			$settings_tab_panels[ $panel['id'] ] = $panel;
		}
	}
} else {
	foreach ( $legacy_panels as $panel ) {
		if ( ! empty( $panel['id'] ) ) {
			$settings_tab_panels[ $panel['id'] ] = $panel;
		}
	}
}

$settings_tab_panels = array_values(
	array_filter(
		$settings_tab_panels,
		static function ( $panel ) {
			return is_array( $panel ) && ! empty( $panel['id'] ) && ! empty( $panel['callback'] ) && is_callable( $panel['callback'] );
		}
	)
);

$settings_default_panel_id = ! empty( $settings_tab_panels[0]['id'] ) ? (string) $settings_tab_panels[0]['id'] : '';
?>

<div class="noesia-swa-section-grid noesia-swa-settings-tabbed-layout">
	<div class="noesia-swa-card noesia-swa-card--span2">
		<div class="noesia-swa-card-header">
			<div>
				<h3><?php esc_html_e( 'Settings', 'noesia-smart-waiter' ); ?></h3>
				<p class="noesia-swa-muted"><?php esc_html_e( 'Configure profile info, morning briefing, dashboard language, weather source and header bookmarks.', 'noesia-smart-waiter' ); ?></p>
			</div>

			<div class="noesia-swa-inline-actions">
				<?php submit_button( __( 'Save Changes', 'noesia-smart-waiter' ), 'primary', 'submit', false, array( 'form' => 'noesia-swa-settings-form' ) ); ?>
			</div>
		</div>
	</div>

	<div class="noesia-swa-settings-workspace noesia-swa-card--span2">
		<aside class="noesia-swa-settings-sidebar" aria-label="<?php esc_attr_e( 'Settings sections', 'noesia-smart-waiter' ); ?>">
			<div
				class="noesia-swa-settings-sidebar-inner"
				data-noesia-swa-settings-tablist="1"
				data-noesia-swa-settings-default-tab="<?php echo esc_attr( $settings_default_panel_id ); ?>"
				role="tablist"
				aria-orientation="vertical"
			>
				<?php foreach ( $settings_tab_panels as $panel ) : ?>
					<?php
					$panel_id = isset( $panel['id'] ) ? sanitize_key( (string) $panel['id'] ) : '';
					$panel_title = isset( $panel['title'] ) && '' !== (string) $panel['title'] ? (string) $panel['title'] : $panel_id;
					$panel_dom_id = 'noesia-swa-settings-panel-' . $panel_id;
					?>
					<button
						type="button"
						class="noesia-swa-settings-tab"
						data-noesia-swa-settings-tab="<?php echo esc_attr( $panel_id ); ?>"
						role="tab"
						aria-selected="false"
						aria-controls="<?php echo esc_attr( $panel_dom_id ); ?>"
						id="<?php echo esc_attr( 'noesia-swa-settings-tab-' . $panel_id ); ?>"
					>
						<span class="noesia-swa-settings-tab-label"><?php echo esc_html( $panel_title ); ?></span>
					</button>
				<?php endforeach; ?>
			</div>
		</aside>

		<div class="noesia-swa-settings-panels">
			<?php foreach ( $settings_tab_panels as $panel ) : ?>
				<?php
				$panel_id = isset( $panel['id'] ) ? sanitize_key( (string) $panel['id'] ) : '';
				$panel_dom_id = 'noesia-swa-settings-panel-' . $panel_id;
				ob_start();
				call_user_func( $panel['callback'], $panel, $settings_panels_registry );
				$panel_markup = ob_get_clean();
				?>
				<section
					class="noesia-swa-settings-panel"
					data-noesia-swa-settings-panel="<?php echo esc_attr( $panel_id ); ?>"
					id="<?php echo esc_attr( $panel_dom_id ); ?>"
					role="tabpanel"
					aria-labelledby="<?php echo esc_attr( 'noesia-swa-settings-tab-' . $panel_id ); ?>"
					hidden
				>
					<?php echo $panel_markup; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				</section>
			<?php endforeach; ?>
		</div>
	</div>
</div>

<?php
} )( $noesia_swa_settings_section_context, $noesia_swa_settings_section_service );