<?php
/**
 * Admin functionality for FoodOrd Tweaks (orchestrator only).
 *
 * @package FoodOrd
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class Foodord_Tweaks
 *
 * Orchestrates the Tweaks settings page by delegating to Back to Top page handler.
 */
class Foodord_Tweaks {
	/**
	 * Retrieve current Tweaks page slug.
	 */
	private function get_page_slug(): string {
		return function_exists( 'foodord_get_settings_page_slug' )
			? foodord_get_settings_page_slug( 'tweaks' )
			: 'foodord-tweaks';
	}

	/**
	 * Possible slugs for the Tweaks page (theme or plugin context).
	 *
	 * @return string[]
	 */
	private function get_page_slugs(): array {
		return function_exists( 'foodord_get_settings_page_slugs' )
			? foodord_get_settings_page_slugs( 'tweaks' )
			: array( 'foodord-theme-settings-tweaks', 'foodord-tweaks' );
	}

	/**
	 * Build the License page URL.
	 */
	private function get_license_page_url(): string {
		return function_exists( 'foodord_get_admin_page_url' )
			? foodord_get_admin_page_url( 'license' )
			: admin_url( 'admin.php?page=foodord-license' );
	}
	/**
	 * Check if license is active.
	 */
	private function is_license_active(): bool {
		return true;
	}

	/**
	 * Check whether the FoodOrd theme is active.
	 */
	private function is_foodord_theme_active(): bool {
		if ( defined( 'FOODORD_THEME_ACTIVE' ) ) {
			return (bool) FOODORD_THEME_ACTIVE;
		}

		return function_exists( 'foodord_is_theme_active' ) && foodord_is_theme_active();
	}

	/**
	 * Bootstrap admin hooks.
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'register_tweaks_submenu' ), 12 );
		// Ensure form submissions are handled early in the admin request lifecycle.
		add_action( 'admin_init', array( $this, 'handle_tweaks_submission' ) );
		// Enqueue preview script only on Tweaks page.
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_tweaks_assets' ) );
	}

	/**
	 * Register Tweaks submenu under Footer in Theme Settings menu.
	 */
	public function register_tweaks_submenu() {
		if ( current_user_can( 'manage_options' ) && $this->is_license_active() ) {
			$parent_slug = function_exists( 'foodord_get_admin_parent_slug' )
				? foodord_get_admin_parent_slug()
				: 'foodord-settings';

			add_submenu_page(
				$parent_slug,
				__( 'Tweaks', 'foodord' ),
				__( 'Tweaks', 'foodord' ),
				'manage_options',
				$this->get_page_slug(),
				array( $this, 'render_tweaks_page' )
			);
		}
	}

	/**
	 * Render Tweaks page with both sections.
	 */
	public function render_tweaks_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'foodord' ) );
		}

		if ( ! $this->is_license_active() ) {
			wp_safe_redirect( $this->get_license_page_url() );
			exit;
		}

		// Lazy-load handlers.
		if ( ! class_exists( 'Foodord_Back_To_Top' ) ) {
			require_once __DIR__ . '/class-foodord-back-to-top.php';
		}
		if ( ! class_exists( 'Foodord_Product_Card_Images' ) ) {
			require_once __DIR__ . '/class-foodord-product-card-images.php';
		}
		if ( ! class_exists( 'Foodord_Scroll_Navigation' ) ) {
			require_once __DIR__ . '/class-foodord-scroll-navigation.php';
		}
		if ( ! class_exists( 'Foodord_Topping_Label_Toggle' ) ) {
			require_once __DIR__ . '/class-foodord-topping-label-toggle.php';
		}
		if ( ! class_exists( 'Foodord_Allergy_Info' ) ) {
			require_once __DIR__ . '/class-foodord-allergy-info.php';
		}

		$back_to_top    = new Foodord_Back_To_Top();
		$product_images = new Foodord_Product_Card_Images();
		$scroll_nav     = new Foodord_Scroll_Navigation();
		$topping_labels = new Foodord_Topping_Label_Toggle();
		$allergy_info   = new Foodord_Allergy_Info();
		$is_theme_active = $this->is_foodord_theme_active();

		// Capture Back to Top render and modify it.
		ob_start();
		$back_to_top->render( $is_theme_active );
		$html = ob_get_clean();

		// Find the submit button position (handle both double and single quote variants).
		$submit_pos = strpos( $html, '<p class="submit">' );
		if ( false === $submit_pos ) {
			$submit_pos = strpos( $html, "<p class='submit'>" );
		}

		if ( false !== $submit_pos ) {
			// Render Product Card Images section and Scroll Navigation, inject both before submit.
			ob_start();
			$product_images->render_section();
			$product_section = ob_get_clean();
			ob_start();
			$topping_labels->render_section();
			$topping_labels_section = ob_get_clean();
			ob_start();
			$allergy_info->render_section();
			$allergy_section = ob_get_clean();
			ob_start();
			$scroll_nav->render_section();
			$scroll_section = ob_get_clean();

			// Render Checkout Country Toggle section.
			$country_toggle_section = $this->render_checkout_country_toggle_section();

			// Render Modal Upsell section.
			$upsell_section = $this->render_modal_upsell_section();

			// Render Store Modes section.
			$store_modes_section = $this->render_store_modes_section();

			// Render RTL section.
			$rtl_section = $this->render_rtl_section();

			$theme_mode_section = '';
			if ( $is_theme_active ) {
				$theme_mode_section = $this->render_theme_mode_section();
			}

			$html = substr( $html, 0, $submit_pos ) . $product_section . $topping_labels_section . $allergy_section . $scroll_section . $country_toggle_section . $upsell_section . $store_modes_section . $theme_mode_section . $rtl_section . substr( $html, $submit_pos );
		}

		// Output composed HTML (Tweaks + Product Images section).
		// Safe output: content comes from trusted admin form handlers with nonce verification.
		$allowed_html = array(
			'nav'      => array(
				'class'      => array(),
				'aria-label' => array(),
			),
			'ul'       => array(
				'class' => array(),
			),
			'li'       => array(
				'class' => array(),
			),
			'div'      => array(
				'class'             => array(),
				'role'              => array(),
				'id'                => array(),
				'aria-label'        => array(),
				'aria-hidden'       => array(),
				'aria-expanded'     => array(),
				'aria-controls'     => array(),
				'aria-describedby'  => array(),
				'aria-labelledby'   => array(),
				'aria-live'         => array(),
				'aria-pressed'      => array(),
				'aria-disabled'     => array(),
				'data-editor'       => array(),
				'data-settings'     => array(),
				'data-toolbar'      => array(),
				'data-wp-editor-id' => array(),
				'data-name'         => array(),
				'data-placeholder'  => array(),
			),
			'h1'       => array( 'class' => array() ),
			'h2'       => array( 'class' => array() ),
			'p'        => array(
				'class'       => array(),
				'aria-hidden' => array(),
			),
			'form'     => array(
				'method'     => array(),
				'action'     => array(),
				'class'      => array(),
				'novalidate' => array(),
				'id'         => array(),
			),
			'input'    => array(
				'type'        => array(),
				'id'          => array(),
				'name'        => array(),
				'value'       => array(),
				'class'       => array(),
				'placeholder' => array(),
				'aria-label'  => array(),
				'checked'     => array(),
			),
			'label'    => array(
				'for'   => array(),
				'class' => array(),
			),
			'button'   => array(
				'type'              => array(),
				'name'              => array(),
				'class'             => array(),
				'id'                => array(),
				'data-target'       => array(),
				'data-editor'       => array(),
				'data-wp-editor-id' => array(),
				'title'             => array(),
				'aria-label'        => array(),
				'aria-expanded'     => array(),
				'aria-controls'     => array(),
				'aria-pressed'      => array(),
				'aria-haspopup'     => array(),
				'tabindex'          => array(),
			),
			'span'     => array(
				'class'       => array(),
				'aria-hidden' => array(),
				'aria-live'   => array(),
			),
			'a'        => array(
				'href'          => array(),
				'class'         => array(),
				'id'            => array(),
				'target'        => array(),
				'rel'           => array(),
				'role'          => array(),
				'aria-label'    => array(),
				'aria-expanded' => array(),
				'aria-controls' => array(),
				'aria-current'  => array(),
			),
			'textarea' => array(
				'id'            => array(),
				'name'          => array(),
				'class'         => array(),
				'rows'          => array(),
				'cols'          => array(),
				'aria-label'    => array(),
				'aria-required' => array(),
				'autocomplete'  => array(),
				'spellcheck'    => array(),
			),
		);
		echo wp_kses( $html, $allowed_html );
	}

	/**
	 * Handle form submission on admin_init.
	 */
	public function handle_tweaks_submission() {
		if ( ! is_admin() ) {
			return;
		}
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		if ( ! $this->is_license_active() ) {
			return;
		}

		// Handle the unified form submission.
		if ( ! isset( $_POST['foodord_tweaks_submit'] ) ) {
			return;
		}

		check_admin_referer( 'foodord_tweaks_save', 'foodord_tweaks_nonce' );
		$nonce_value = isset( $_POST['foodord_tweaks_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['foodord_tweaks_nonce'] ) ) : '';
		if ( '' === $nonce_value || ! wp_verify_nonce( $nonce_value, 'foodord_tweaks_save' ) ) {
			return;
		}

			// Include handlers.
			if ( ! class_exists( 'Foodord_Back_To_Top' ) ) {
				require_once __DIR__ . '/class-foodord-back-to-top.php';
			}
			if ( ! class_exists( 'Foodord_Product_Card_Images' ) ) {
				require_once __DIR__ . '/class-foodord-product-card-images.php';
			}
			if ( ! class_exists( 'Foodord_Scroll_Navigation' ) ) {
				require_once __DIR__ . '/class-foodord-scroll-navigation.php';
			}
			if ( ! class_exists( 'Foodord_Topping_Label_Toggle' ) ) {
				require_once __DIR__ . '/class-foodord-topping-label-toggle.php';
			}
			if ( ! class_exists( 'Foodord_Allergy_Info' ) ) {
				require_once __DIR__ . '/class-foodord-allergy-info.php';
			}

			// Delegate to all handlers.
			if ( $this->is_foodord_theme_active() ) {
				$back_to_top = new Foodord_Back_To_Top();
				$back_to_top->handle_form_submission();
			}

			$product_images = new Foodord_Product_Card_Images();
			$product_images->handle_form_submission();

			$scroll_nav = new Foodord_Scroll_Navigation();
			$scroll_nav->handle_form_submission();

			$topping_labels = new Foodord_Topping_Label_Toggle();
			$topping_labels->handle_form_submission();

			$allergy_info = new Foodord_Allergy_Info();
			$allergy_info->handle_form_submission();

			// Save: Checkout Country/Region toggle visibility setting.
			$show_toggle = isset( $_POST['foodord_checkout_country_toggle_show'] ) ? 'yes' : 'no';
			update_option( 'foodord_checkout_country_toggle_show', $show_toggle );

			// Save: RTL mode setting.
			$enable_rtl = isset( $_POST['foodord_enable_rtl'] ) ? 1 : 0;
			update_option( 'foodord_enable_rtl', $enable_rtl );

			// Save: Modal upsell setting.
			$enable_modal_upsells = isset( $_POST['foodord_enable_modal_upsells'] ) ? 'yes' : 'no';
			update_option( 'foodord_enable_modal_upsells', $enable_modal_upsells );

			if ( class_exists( 'Foodord_Business_Hours' ) ) {
				$delivery_enabled = true;
				if ( isset( $_POST['foodord_store_mode_delivery_enabled'] ) ) {
					$delivery_enabled = '1' === sanitize_text_field(
						wp_unslash( $_POST['foodord_store_mode_delivery_enabled'] )
					);
				}

				$pickup_enabled = true;
				if ( isset( $_POST['foodord_store_mode_pickup_enabled'] ) ) {
					$pickup_enabled = '1' === sanitize_text_field(
						wp_unslash( $_POST['foodord_store_mode_pickup_enabled'] )
					);
				}

				$pause_reason = __( 'Temporarily paused via FoodOrd Web Admin', 'foodord' );
				Foodord_Business_Hours::set_mode_lock( 'delivery', ! $delivery_enabled, $pause_reason );
				Foodord_Business_Hours::set_mode_lock( 'pickup', ! $pickup_enabled, $pause_reason );
			}

			// Save: Theme mode setting only when FoodOrd Theme is active.
			if ( $this->is_foodord_theme_active() ) {
				$enable_theme_mode = isset( $_POST['foodord_enable_theme_mode'] ) ? 'yes' : 'no';
				update_option( 'foodord_enable_theme_mode', $enable_theme_mode );
			}

			// Set single success message for all tweaks settings.
			set_transient( 'foodord_tweaks_success', true, 45 );
		}

	/**
	 * Render section for enabling the Checkout Country/Region toggle.
	 *
	 * @return string Safe HTML.
	 */
	private function render_checkout_country_toggle_section(): string {
		$enabled = get_option( 'foodord_checkout_country_toggle_show', 'no' );
		$checked = ( 'yes' === $enabled ) ? 'checked' : '';

		$title = esc_html__( 'Checkout: Country/Region Field', 'foodord' );
		$desc  = esc_html__( 'Show or hide the Country/Region field on the WooCommerce Checkout page.', 'foodord' );
		$label = esc_html__( 'Show Country/Region field on Checkout', 'foodord' );
		$name  = 'foodord_checkout_country_toggle_show';
		$id    = 'foodord_checkout_country_toggle_show';

		$section  = '';
		$section .= '<div class="wrc-checkout-country-container">';
		$section .= '  <div class="wrc-checkout-country-header">';
		$section .= '    <h2 class="wrc-section-title">' . $title . '</h2>';
		$section .= '    <p class="wrc-card-sub">' . $desc . '</p>';
		$section .= '  </div>';
		$section .= '  <div class="wrc-checkout-country-content">';
		$section .= '    <div class="wrc-form" role="group" aria-label="' . esc_attr__( 'Checkout: Country/Region Toggle', 'foodord' ) . '">';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="' . esc_attr( $id ) . '">' . $label . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="' . esc_attr( $id ) . '">';
		$section .= '              <input type="checkbox" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="yes" class="regular-text" ' . $checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Show Country/Region field on Checkout', 'foodord' ) . '</span>';
		$section .= '          </div>';
		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '    </div>';
		$section .= '  </div>';
		$section .= '</div>';

		return $section;
	}

	/**
	 * Render Modal Upsell section.
	 *
	 * @return string Safe HTML.
	 */
	private function render_modal_upsell_section(): string {
		$enabled = get_option( 'foodord_enable_modal_upsells', 'no' );
		$checked = ( 'yes' === $enabled ) ? 'checked' : '';

		$title = esc_html__( 'Upsell Recommendations', 'foodord' );
		$desc  = esc_html__( 'Show or hide the upsell products section in the product modal and cart.', 'foodord' );
		$label = esc_html__( 'Enable upsell', 'foodord' );
		$name  = 'foodord_enable_modal_upsells';
		$id    = 'foodord_enable_modal_upsells';

		$section  = '';
		$section .= '<div class="wrc-upsell-container">';
		$section .= '  <div class="wrc-upsell-header">';
		$section .= '    <h2 class="wrc-section-title">' . $title . '</h2>';
		$section .= '    <p class="wrc-card-sub">' . $desc . '</p>';
		$section .= '  </div>';
		$section .= '  <div class="wrc-upsell-content">';
		$section .= '    <div class="wrc-form" role="group" aria-label="' . esc_attr__( 'Upsell Recommendation Settings', 'foodord' ) . '">';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="' . esc_attr( $id ) . '">' . $label . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="' . esc_attr( $id ) . '">';
		$section .= '              <input type="checkbox" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="yes" class="regular-text" ' . $checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Enable upsell', 'foodord' ) . '</span>';
		$section .= '          </div>';
		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '    </div>';
		$section .= '  </div>';
		$section .= '</div>';

		return $section;
	}

	/**
	 * Render Store Modes section.
	 *
	 * @return string Safe HTML.
	 */
	private function render_store_modes_section(): string {
		$delivery_enabled = true;
		$pickup_enabled   = true;
		$delivery_status  = esc_html__( 'Unavailable', 'foodord' );
		$pickup_status    = esc_html__( 'Unavailable', 'foodord' );

		if ( class_exists( 'Foodord_Business_Hours' ) ) {
			$delivery_lock    = Foodord_Business_Hours::get_mode_lock( 'delivery' );
			$pickup_lock      = Foodord_Business_Hours::get_mode_lock( 'pickup' );
			$delivery_enabled = empty( $delivery_lock['locked'] );
			$pickup_enabled   = empty( $pickup_lock['locked'] );

			$payload = Foodord_Business_Hours::get_status_payload();
			$modes   = ( isset( $payload['modes'] ) && is_array( $payload['modes'] ) ) ? $payload['modes'] : array();

			if ( isset( $modes['delivery'] ) && is_array( $modes['delivery'] ) ) {
				$delivery_open = ! empty( $modes['delivery']['is_open'] );
				$delivery_status = $delivery_open ? esc_html__( 'Open', 'foodord' ) : esc_html__( 'Closed', 'foodord' );
			}

			if ( isset( $modes['pickup'] ) && is_array( $modes['pickup'] ) ) {
				$pickup_open = ! empty( $modes['pickup']['is_open'] );
				$pickup_status = $pickup_open ? esc_html__( 'Open', 'foodord' ) : esc_html__( 'Closed', 'foodord' );
			}
		}

		$delivery_checked = $delivery_enabled ? 'checked' : '';
		$pickup_checked   = $pickup_enabled ? 'checked' : '';

		$title = esc_html__( 'Store Modes', 'foodord' );
		$desc  = esc_html__( 'Pause delivery or pickup even if time is within delivery hours.', 'foodord' );

		$section  = '';
		$section .= '<div class="wrc-store-mode-container">';
		$section .= '  <div class="wrc-store-mode-header">';
		$section .= '    <h2 class="wrc-section-title">' . $title . '</h2>';
		$section .= '    <p class="wrc-card-sub">' . $desc . '</p>';
		$section .= '  </div>';
		$section .= '  <div class="wrc-store-mode-content">';
		$section .= '    <div class="wrc-form" role="group" aria-label="' . esc_attr__( 'Store Modes', 'foodord' ) . '">';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="foodord_store_mode_delivery_enabled">' . esc_html__( 'Delivery', 'foodord' ) . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <input type="hidden" name="foodord_store_mode_delivery_enabled" value="0" class="wrc-input-hidden" />';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="foodord_store_mode_delivery_enabled">';
		$section .= '              <input type="checkbox" id="foodord_store_mode_delivery_enabled" name="foodord_store_mode_delivery_enabled" value="1" class="regular-text" ' . $delivery_checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Enable delivery orders', 'foodord' ) . '</span>';
		$section .= '          </div>';
		$section .= '          <p class="wrc-help">' . esc_html__( 'Current delivery status:', 'foodord' ) . ' ' . esc_html( $delivery_status ) . '</p>';
		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="foodord_store_mode_pickup_enabled">' . esc_html__( 'Pickup', 'foodord' ) . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <input type="hidden" name="foodord_store_mode_pickup_enabled" value="0" class="wrc-input-hidden" />';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="foodord_store_mode_pickup_enabled">';
		$section .= '              <input type="checkbox" id="foodord_store_mode_pickup_enabled" name="foodord_store_mode_pickup_enabled" value="1" class="regular-text" ' . $pickup_checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Enable pickup orders', 'foodord' ) . '</span>';
		$section .= '          </div>';
		$section .= '          <p class="wrc-help">' . esc_html__( 'Current pickup status:', 'foodord' ) . ' ' . esc_html( $pickup_status ) . '</p>';
		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '    </div>';
		$section .= '  </div>';
		$section .= '</div>';

		return $section;
	}

	/**
	 * Render RTL Mode section.
	 *
	 * @return string Safe HTML.
	 */
	private function render_rtl_section(): string {
		$enabled = get_option( 'foodord_enable_rtl', false );
		$checked = $enabled ? 'checked' : '';

		$title = esc_html__( 'RTL Mode', 'foodord' );
		$desc  = esc_html__( 'Enable Right-to-Left (RTL) language support for both theme and Foodord.', 'foodord' );
		$label = esc_html__( 'Enable RTL Mode', 'foodord' );
		$name  = 'foodord_enable_rtl';
		$id    = 'foodord_enable_rtl';

		$section  = '';
		$section .= '<div class="wrc-rtl-container">';
		$section .= '  <div class="wrc-rtl-header">';
		$section .= '    <h2 class="wrc-section-title">' . $title . '</h2>';
		$section .= '    <p class="wrc-card-sub">' . $desc . '</p>';
		$section .= '  </div>';
		$section .= '  <div class="wrc-rtl-content">';
		$section .= '    <div class="wrc-form" role="group" aria-label="' . esc_attr__( 'RTL Mode Settings', 'foodord' ) . '">';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="' . esc_attr( $id ) . '">' . $label . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="' . esc_attr( $id ) . '">';
		$section .= '              <input type="checkbox" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="1" class="regular-text" ' . $checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Enable RTL Mode', 'foodord' ) . '</span>';
		$section .= '          </div>';

		// Status indicator.
		if ( $enabled ) {
			$section .= '          <div class="wrc-rtl-status wrc-status-active">';
			$section .= '            <span class="dashicons dashicons-yes-alt"></span>';
			$section .= '            <span>' . esc_html__( 'RTL mode is currently active', 'foodord' ) . '</span>';
			$section .= '          </div>';
		} else {
			$section .= '          <div class="wrc-rtl-status wrc-status-inactive">';
			$section .= '            <span class="dashicons dashicons-dismiss"></span>';
			$section .= '            <span>' . esc_html__( 'RTL mode is currently inactive', 'foodord' ) . '</span>';
			$section .= '          </div>';
		}

		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '    </div>';
		$section .= '  </div>';
		$section .= '</div>';

		return $section;
	}

	/**
	 * Render Theme Mode section.
	 *
	 * @return string Safe HTML.
	 */
	private function render_theme_mode_section(): string {
		$enabled = get_option( 'foodord_enable_theme_mode', 'no' );
		$checked = ( 'yes' === $enabled ) ? 'checked' : '';

		$title = esc_html__( 'Theme Mode', 'foodord' );
		$desc  = esc_html__( 'Enable or disable the FoodOrd Theme light, dark, and system mode controls.', 'foodord' );
		$label = esc_html__( 'Enable Theme Mode', 'foodord' );
		$name  = 'foodord_enable_theme_mode';
		$id    = 'foodord_enable_theme_mode';

		$section  = '';
		$section .= '<div class="wrc-theme-mode-container">';
		$section .= '  <div class="wrc-theme-mode-header">';
		$section .= '    <h2 class="wrc-section-title">' . $title . '</h2>';
		$section .= '    <p class="wrc-card-sub">' . $desc . '</p>';
		$section .= '  </div>';
		$section .= '  <div class="wrc-theme-mode-content">';
		$section .= '    <div class="wrc-form" role="group" aria-label="' . esc_attr__( 'Theme Mode Settings', 'foodord' ) . '">';
		$section .= '      <div class="wrc-field wrc-field--toggle">';
		$section .= '        <label class="wrc-label" for="' . esc_attr( $id ) . '">' . $label . '</label>';
		$section .= '        <div class="wrc-control">';
		$section .= '          <div class="foodord-toggle-row">';
		$section .= '            <label class="foodord-toggle-switch" for="' . esc_attr( $id ) . '">';
		$section .= '              <input type="checkbox" id="' . esc_attr( $id ) . '" name="' . esc_attr( $name ) . '" value="yes" class="regular-text" ' . $checked . ' />';
		$section .= '              <span class="foodord-toggle-slider"></span>';
		$section .= '            </label>';
		$section .= '            <span class="foodord-toggle-label">' . esc_html__( 'Allow visitors to switch theme mode', 'foodord' ) . '</span>';
		$section .= '          </div>';

		if ( 'yes' === $enabled ) {
			$section .= '          <div class="wrc-rtl-status wrc-status-active">';
			$section .= '            <span class="dashicons dashicons-yes-alt"></span>';
			$section .= '            <span>' . esc_html__( 'Theme mode is currently active', 'foodord' ) . '</span>';
			$section .= '          </div>';
		} else {
			$section .= '          <div class="wrc-rtl-status wrc-status-inactive">';
			$section .= '            <span class="dashicons dashicons-dismiss"></span>';
			$section .= '            <span>' . esc_html__( 'Theme mode is currently inactive', 'foodord' ) . '</span>';
			$section .= '          </div>';
		}

		$section .= '        </div>';
		$section .= '      </div>';
		$section .= '    </div>';
		$section .= '  </div>';
		$section .= '</div>';

		return $section;
	}

	/**
	 * Enqueue page-specific assets for Tweaks.
	 */
	/**
	 * Enqueue page-specific assets for Tweaks.
	 *
	 * @param string $hook Current admin page hook suffix.
	 */
	public function enqueue_tweaks_assets( $hook ) {
		unset( $hook );

		// Safer check via GET param since this is a submenu.
		$page_raw = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
		$page     = is_string( $page_raw ) ? sanitize_text_field( $page_raw ) : '';
		$slugs    = $this->get_page_slugs();
		$match    = in_array( $page, $slugs, true );
		if ( ! $match || ! $this->is_license_active() ) {
			return;
		}

		// Enqueue WordPress Color Picker.
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );

		wp_enqueue_style(
			'foodord-admin-nav',
			plugin_dir_url( __FILE__ ) . 'css/foodord-admin-nav.css',
			array(),
			FOODORD_VERSION
		);

		wp_enqueue_script(
			'foodord-admin-nav',
			plugin_dir_url( __FILE__ ) . 'js/foodord-admin-nav.js',
			array(),
			FOODORD_VERSION,
			true
		);

		// Enqueue Back to Top preview script.
		wp_enqueue_script(
			'foodord-back-to-top-preview',
			plugin_dir_url( __FILE__ ) . 'js/foodord-back-to-top-preview.js',
			array( 'jquery', 'wp-color-picker' ),
			FOODORD_VERSION,
			true
		);

		// Enqueue Product Card Images script.
		wp_enqueue_script(
			'foodord-product-card-images',
			plugin_dir_url( __FILE__ ) . 'js/foodord-product-card-images.js',
			array(),
			FOODORD_VERSION,
			true
		);
	}
}
