<?php
/**
 * RadiusBooking API Route Registrar
 *
 * This class is responsible for registering API routes dynamically with the WordPress REST API.
 * It supports CRUD operations and allows for middleware to be applied globally or per route.
 *
 * @package RadiusTheme\RadiusBooking\Core\Api\Routes
 */

namespace RadiusTheme\RadiusBooking\Assets;

use RadiusTheme\RadiusBooking\Common\Keys;
use RadiusTheme\RadiusBooking\Controllers\ServiceController;
use RadiusTheme\RadiusBooking\Helpers\SettingsHelper;
use RadiusTheme\RadiusBooking\Helpers\WPHelpers;
use RadiusTheme\RadiusBooking\Models\EmployeeService;
use RadiusTheme\RadiusBooking\Models\Service;
use RadiusTheme\RadiusBooking\Models\UserProfile;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Load assets class
 *
 * Responsible for managing all of the assets (CSS, JS, Images, Locales).
 */
class LoadAssets {

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'wp_print_scripts', array( $this, 'localize_printed_scripts' ), 5 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_site_assets' ), 999 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_service_list_assets' ), 999 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_my_booking_assets' ), 999 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_chatbot_assets' ), 999 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_meeting_booking_assets' ), 999 );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_event_booking_assets' ), 999 );

		// Load assets inside Elementor editor preview iframe.
		add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_service_list_assets_elementor' ) );
		add_action( 'elementor/preview/enqueue_scripts', array( $this, 'enqueue_site_assets_elementor' ) );

		// Load booking form assets in the Gutenberg block editor.
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_site_assets_elementor' ) );
	}

	/**
	 * Enqueue admin styles and scripts.
	 *
	 * @return void
	 * @since 0.3.0 Loads the JS and CSS only on the Dynamic Discount admin page.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_admin_assets() {
		if ( ! is_admin() || ! isset( $_GET['page'] ) || sanitize_text_field( wp_unslash( $_GET['page'] ) ) !== 'radius-booking' ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return;
		}

		wp_enqueue_media();
		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;
		$assets     = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/main.jsx';

		$assets->enqueue(
			$entry,
			array(
				'handle'              => 'radius-booking-app',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array( 'wp-components' ),
			)
		);

		// Set translations for main handle
		wp_set_script_translations(
			'radius-booking-app',
			'radius-booking',
			RADIUS_BOOKING_DIR . '/languages'
		);

		// Localize available LOCO translation files
		$this->localize_js_translations( RADIUS_BOOKING_DIR );
	}

	/**
	 * Detect and localize LOCO Translate generated JSON files
	 *
	 * @param string $plugin_dir Plugin directory path.
	 *
	 * @return void
	 */
	private function localize_js_translations( $plugin_dir ) {
		$languages_dir = $plugin_dir . '/languages';

		// Get current locale
		$current_locale = determine_locale();

		// Scan for LOCO generated JSON files
		$available_translations = $this->scan_json_files( $languages_dir );

		if ( empty( $available_translations ) ) {
			return;
		}

		$translation_config = array(
			'currentLocale' => $current_locale,
			'domain'        => 'radius-booking',
			'languagesUrl'  => content_url( 'plugins/radius-booking/languages' ),
			'translations'  => $available_translations,
		);
		wp_localize_script(
			'radius-booking-app',
			'radiusBookingI18n',
			$translation_config
		);
	}

	/**
	 * Scan languages directory for LOCO generated JSON files
	 *
	 * @param string $languages_dir Path to languages directory.
	 *
	 * @return array Array of translations grouped by locale.
	 */
	private function scan_json_files( $languages_dir ) {
		$translations = array();

		// Find all JSON files that LOCO generates
		// Pattern: radius-booking-{locale}-{hash}.json
		$json_files = glob( $languages_dir . '/radius-booking-*.json' );

		if ( empty( $json_files ) ) {
			return $translations;
		}

		foreach ( $json_files as $json_file ) {
			$filename = basename( $json_file );
			$matches  = explode( '-', $filename );
			if ( isset( $matches[1] ) && $matches[2] ) {
				$locale = $matches[2];
				$hash   = $matches[3];

				// Initialize array for this locale if not exists
				if ( ! isset( $translations[ $locale ] ) ) {
					$translations[ $locale ] = array();
				}

				// Add file info to the locale's array
				$translations[ $locale ][] = array(
					'hash'     => $hash,
					'filename' => $filename,
				);
			}
		}

		return $translations;
	}

	/**
	 * Localize scripts for the Radius Booking app.
	 *
	 * This function localizes the script with necessary parameters such as AJAX URL, nonce, and plugin URL.
	 *
	 * @since 1.0.0
	 */
	public function localize_printed_scripts() {
		if ( ! is_admin() || ! isset( $_GET['page'] ) || sanitize_text_field( wp_unslash( $_GET['page'] ) ) !== 'radius-booking' ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return;
		}
		wp_localize_script(
			'radius-booking-app',
			'radius_booking_param',
			array(
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
				'nonce'             => wp_create_nonce( 'wp_rest' ),
				'rest_root'         => esc_url_raw( rest_url() ),
				'plugin_url'        => untrailingslashit( content_url( 'plugins/radius-booking' ) ),
				'admin_email'       => get_option( 'admin_email' ),
				'current_user_info' => WPHelpers::get_current_user_data(),
				'currency_symbol'   => array_map( 'html_entity_decode', get_rtrb_currency_symbols() ),
				'current_location'  => get_rtrb_current_location(),
				'country'           => WPHelpers::get_formated_country_name(),
				'is_pro'            => rtrb_pro_active(),
				'active_modules'    => array(
					'service' => true,
					'meeting' => rtrb_is_module_active( 'meeting' ),
					'event'   => rtrb_is_module_active( 'event' ),
				),
				'root'              => site_url(),
				'timeZone'          => get_option( 'timezone_string' ),
				'phone_settings'    => SettingsHelper::get_setting( 'phone' ),
				'setup_completed'   => (bool) get_option( Keys::RADIUS_BOOKING_SETUP_COMPLETED, false ),
				'wp_pages'          => WPHelpers::get_wp_pages_for_select(),
				'tax_settings'      => SettingsHelper::get_setting( 'tax' ),
				'custom_fields'     => apply_filters( 'rtrb_custom_fields_data', array() ),
				'upgrade_url'       => 'https://radiusbooking.co/#pricing',
				'is_cl_active'      => function_exists( 'rtcl' ),
				'payout_enabled'    => function_exists( 'rtcl' ) && ! empty( SettingsHelper::get_setting( 'payout' )['enabled'] ),
			)
		);
	}

	/**
	 * Enqueue site styles and scripts.
	 * NEW - Enqueue site-specific assets
	 *
	 * @since 1.0.0
	 */
	public function enqueue_site_assets() {
		// Only load on frontend
		if ( is_admin() ) {
			return;
		}

		global $post;
		$should_load = false;

		// Check if shortcode is present
		if ( $post && has_shortcode( $post->post_content, 'radius_booking_form' ) ) {
			$should_load = true;
		}

		// Check if Elementor renders the appointment form widget.
		if ( ! $should_load && $post && class_exists( '\Elementor\Plugin' ) ) {
			$elementor = \Elementor\Plugin::$instance;
			if ( $elementor->db->is_built_with_elementor( $post->ID ) ) {
				$data = $elementor->documents->get( $post->ID );
				if ( $data ) {
					$content = $data->get_elements_data();
					if ( $this->elementor_has_widget( $content, 'rtrb_appointment_form' ) ) {
						$should_load = true;
					}
				}
			}
		}

		// Check if Gutenberg block is present.
		if ( ! $should_load && $post && has_block( 'radius-booking/appointment-form', $post ) ) {
			$should_load = true;
		}

		// Check if this is a single Classified Listing with booking enabled.
		if ( ! $should_load && is_singular( 'rtcl_listing' ) ) {
			$listing_id = get_the_ID();
			if ( $listing_id && get_post_meta( $listing_id, '_rtrb_booking_enabled', true ) ) {
				$should_load = true;
			}
		}

		if ( ! $should_load ) {
			return;
		}

		// Path to site assets
		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		// Initialize Vite with the build manifest
		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/site/main.jsx';
		// Use the manifest entry key for the site entry
		$vite->enqueue(
			$entry, // key from your Vite build (site entry)
			array(
				'handle'              => 'radius-booking-site',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array(), // Add frontend CSS dependencies if needed
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		// Localize site-specific data
		$this->localize_site_scripts();
	}

	/**
	 * Enqueue meeting booking page assets.
	 *
	 * @since 1.2.0
	 *
	 * @return void
	 */
	public function enqueue_meeting_booking_assets() {
		if ( is_admin() ) {
			return;
		}

		global $post;
		$should_load = false;

		if ( $post && has_shortcode( $post->post_content, 'rtrb_meeting_booking' ) ) {
			$should_load = true;
		}

		if ( ! $should_load ) {
			return;
		}

		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$vite->enqueue(
			'src/meetingBooking/main.jsx',
			array(
				'handle'              => 'radius-booking-meeting',
				'script-dependencies' => array( 'wp-element', 'wp-i18n', 'wp-api-fetch' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		wp_localize_script(
			'radius-booking-meeting',
			'radius_booking_meeting_param',
			array(
				'rest_root'          => esc_url_raw( rest_url() ),
				'nonce'              => wp_create_nonce( 'wp_rest' ),
				'date_time_settings' => SettingsHelper::get_setting( 'dateTime' ),
				'language'           => get_locale(),
				'currency_symbol'    => function_exists( 'get_rtrb_currency_symbol' ) ? get_rtrb_currency_symbol() : '$',
				'currency'           => function_exists( 'get_rtrb_currency' ) ? get_rtrb_currency() : 'USD',
			)
		);
	}

	/**
	 * Enqueue event booking page assets.
	 *
	 * @since 1.2.0
	 *
	 * @return void
	 */
	public function enqueue_event_booking_assets() {
		if ( is_admin() ) {
			return;
		}

		global $post;
		$should_load = false;

		if ( $post && ( has_shortcode( $post->post_content, 'rtrb_event_list' ) || has_shortcode( $post->post_content, 'rtrb_event_registration' ) ) ) {
			$should_load = true;
		}

		if ( ! $should_load ) {
			return;
		}

		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$vite->enqueue(
			'src/eventBooking/main.jsx',
			array(
				'handle'              => 'radius-booking-event',
				'script-dependencies' => array( 'wp-element', 'wp-i18n', 'wp-api-fetch' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		$pages = \RadiusTheme\RadiusBooking\Helpers\SettingsHelper::get_setting( 'pages' );
		wp_localize_script(
			'radius-booking-event',
			'radius_booking_event_param',
			array(
				'rest_root'              => esc_url_raw( rest_url() ),
				'nonce'                  => wp_create_nonce( 'wp_rest' ),
				'event_registration_url' => ! empty( $pages['eventRegistrationPage'] ) ? get_permalink( $pages['eventRegistrationPage'] ) : '',
				'currency_symbol'        => get_rtrb_currency_symbol(),
				'currency'               => get_rtrb_currency(),
				'is_pro'                 => function_exists( 'rtrb_pro_active' ) && rtrb_pro_active(),
			)
		);
	}


	/**
	 * Localize scripts for the site/frontend booking interface.
	 *
	 * NEW - Site-specific localization
	 *
	 * @since 1.0.0
	 */
	public function localize_site_scripts() {
		$services      = ( new ServiceController() )->fetchAllServices();
		$currency_code = get_rtrb_currency();
		wp_localize_script(
			'radius-booking-site',
			'radius_booking_site_param',
			array(
				'ajax_url'                   => admin_url( 'admin-ajax.php' ),
				'rest_url'                   => rest_url( 'wp/v2/' ),
				'rest_root'                  => esc_url_raw( rest_url() ),
				'nonce'                      => wp_create_nonce( 'wp_rest' ),
				'plugin_url'                 => untrailingslashit( content_url( 'plugins/radius-booking' ) ),
				'current_user'               => get_current_user_id(),
				'is_user_logged_in'          => is_user_logged_in(),
				'current_user_email'         => is_user_logged_in() ? wp_get_current_user()->user_email : '',
				'available_payment_gateways' => get_rtrb_available_payment_gateways_for_currency( $currency_code['symbol'] ),
				'currency'                   => get_rtrb_currency(),
				'currency_symbol'            => get_rtrb_currency_symbol(),
				'services'                   => $services,
				'phone_settings'             => SettingsHelper::get_setting( 'phone' ),
				'business_settings'          => SettingsHelper::get_setting( 'business' ),
				'date_time_settings'         => SettingsHelper::get_setting( 'dateTime' ),
				'tax_settings'               => rtrb_pro_active() ? SettingsHelper::get_setting( 'tax' ) : SettingsHelper::tax(),
				'language'                   => get_locale(),
				'is_pro'                     => rtrb_pro_active(),
				'google_redirect_url'        => admin_url( 'admin.php?page=radius-booking#/settings' ),
				'form_customization'         => SettingsHelper::get_setting( 'formCustomization' ),
				'custom_fields'              => apply_filters( 'rtrb_custom_fields_data', array() ),
				'chatbot'                    => $this->get_site_chatbot_config(),
				'preSelectedServiceId'       => $this->get_preselected_service_id(),
				'preSelectedEmployeeId'      => $this->get_preselected_employee_id(),
			)
		);
	}

	/**
	 * Get pre-selected service ID for Classified Listing pages.
	 *
	 * @return int|null
	 */
	/**
	 * Get staff permissions with Pro gating applied on the PHP side.
	 *
	 * @return array Permissions array with boolean values.
	 */
	private static function getEffectiveStaffPermissions() {
		$raw        = SettingsHelper::get_setting( 'staffPermissions' );
		$is_pro     = rtrb_pro_active();
		$is_sub     = self::isCurrentUserSubStaff();

		// Sub-staff cannot manage services or staff.
		return array(
			'canEditService'      => ! empty( $raw['canEditService'] ) && ! $is_sub,
			'canCreateService'    => ! empty( $raw['canCreateService'] ) && $is_pro && ! $is_sub,
			'canDeleteService'    => ! empty( $raw['canDeleteService'] ) && $is_pro && ! $is_sub,
			'canManageStaff'      => ! empty( $raw['canManageStaff'] ) && $is_pro && ! $is_sub,
			'canEditWorkSchedule' => ! empty( $raw['canEditWorkSchedule'] ) && $is_pro,
		);
	}

	/**
	 * Check if the current user is a sub-staff (created by another employee).
	 *
	 * @return bool
	 */
	private static function isCurrentUserSubStaff() {
		$wp_user_id = get_current_user_id();
		if ( ! $wp_user_id ) {
			return false;
		}

		$profile = ( new \RadiusTheme\RadiusBooking\Repositories\UserProfileRepository() )->findProfileByWPId( $wp_user_id );
		if ( ! $profile || empty( $profile->user_id ) ) {
			return false;
		}

		$user = \RadiusTheme\RadiusBooking\Models\User::find( $profile->user_id );

		return $user && ! empty( $user->created_by );
	}

	private function get_preselected_service_id() {
		if ( ! is_singular( 'rtcl_listing' ) ) {
			return null;
		}
		$listing_id = get_the_ID();
		if ( ! $listing_id ) {
			return null;
		}

		// Check if staff mode is active.
		if ( $this->is_cl_staff_mode() ) {
			$default_service_id = (int) get_option( 'rtrb_cl_default_service_id', 0 );
			return $default_service_id ?: null;
		}

		$service = Service::where( 'listing_id', $listing_id )->first();
		return $service ? $service->id : null;
	}

	/**
	 * Get pre-selected employee ID for Classified Listing pages.
	 *
	 * @return int|null
	 */
	private function get_preselected_employee_id() {
		if ( ! is_singular( 'rtcl_listing' ) ) {
			return null;
		}

		// Check if staff mode is active.
		if ( $this->is_cl_staff_mode() ) {
			$listing_id = get_the_ID();
			if ( ! $listing_id ) {
				return null;
			}
			$profile = UserProfile::where( 'listing_id', $listing_id )->first();
			return $profile ? $profile->user_id : null;
		}

		$service_id = $this->get_preselected_service_id();
		if ( ! $service_id ) {
			return null;
		}
		$emp_service = EmployeeService::where( 'service_id', $service_id )->first();
		return $emp_service ? $emp_service->user_id : null;
	}

	/**
	 * Check if the Classified Listing integration is in staff mapping mode.
	 *
	 * @return bool
	 */
	private function is_cl_staff_mode() {
		$settings    = SettingsHelper::get_setting( 'integrations' );
		$cl_settings = $settings['classifiedListing'] ?? array();
		return ( $cl_settings['listingMappingMode'] ?? 'service' ) === 'staff';
	}

	/**
	 * Get chatbot configuration for the booking form.
	 * Returns public config only (no API key).
	 *
	 * @return array
	 */
	private function get_site_chatbot_config() {
		$chatbot_settings = SettingsHelper::get_setting( 'chatbot' );

		// Only show AI toggle if chatbot is enabled in settings.
		if ( empty( $chatbot_settings['enabled'] ) ) {
			return array( 'enabled' => false );
		}

		return array(
			'enabled'        => true,
			'showFormToggle' => ! empty( $chatbot_settings['showFormToggle'] ),
			'botName'        => $chatbot_settings['botName'] ?? 'Booking Assistant',
			'welcomeMessage' => $chatbot_settings['welcomeMessage'] ?? 'Hi! I\'m your booking assistant. How can I help you today?',
			'primaryColor'   => $chatbot_settings['primaryColor'] ?? '#0040ff',
			'responseStyle'  => $chatbot_settings['responseStyle'] ?? 'modern',
		);
	}

	/**
	 * Enqueue service list shortcode assets.
	 *
	 * @return void
	 */
	public function enqueue_service_list_assets() {
		if ( is_admin() ) {
			return;
		}

		global $post;
		$should_load = false;

		// Check shortcode in post content.
		if ( $post && has_shortcode( $post->post_content, 'rtrb_service_list' ) ) {
			$should_load = true;
		}

		// Check if Elementor renders the widget (preview/frontend).
		if ( ! $should_load && $post && class_exists( '\Elementor\Plugin' ) ) {
			$elementor = \Elementor\Plugin::$instance;
			if ( $elementor->db->is_built_with_elementor( $post->ID ) ) {
				$data = $elementor->documents->get( $post->ID );
				if ( $data ) {
					$content = $data->get_elements_data();
					if ( $this->elementor_has_widget( $content, 'rtrb_service_list' ) ) {
						$should_load = true;
					}
				}
			}
		}

		// Check if Gutenberg block is present.
		if ( ! $should_load && $post && has_block( 'radius-booking/service-list', $post ) ) {
			$should_load = true;
		}

		if ( ! $should_load ) {
			return;
		}

		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/serviceList/main.jsx';

		$vite->enqueue(
			$entry,
			array(
				'handle'              => 'radius-booking-service-list',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		$this->localize_service_list_scripts();
	}

	/**
	 * Localize scripts for the service list shortcode.
	 *
	 * @return void
	 */
	public function localize_service_list_scripts() {
		$services      = ( new ServiceController() )->fetchAllServices();
		$currency_code = get_rtrb_currency();
		wp_localize_script(
			'radius-booking-service-list',
			'radius_booking_service_list_param',
			array(
				'ajax_url'                   => admin_url( 'admin-ajax.php' ),
				'rest_url'                   => rest_url( 'wp/v2/' ),
				'rest_root'                  => esc_url_raw( rest_url() ),
				'nonce'                      => wp_create_nonce( 'wp_rest' ),
				'plugin_url'                 => untrailingslashit( content_url( 'plugins/radius-booking' ) ),
				'current_user'               => get_current_user_id(),
				'is_user_logged_in'          => is_user_logged_in(),
				'available_payment_gateways' => get_rtrb_available_payment_gateways_for_currency( $currency_code['symbol'] ),
				'currency'                   => get_rtrb_currency(),
				'currency_symbol'            => get_rtrb_currency_symbol(),
				'services'                   => $services,
				'phone_settings'             => SettingsHelper::get_setting( 'phone' ),
				'business_settings'          => SettingsHelper::get_setting( 'business' ),
				'date_time_settings'         => SettingsHelper::get_setting( 'dateTime' ),
				'tax_settings'               => rtrb_pro_active() ? SettingsHelper::get_setting( 'tax' ) : SettingsHelper::tax(),
				'language'                   => get_locale(),
				'is_pro'                     => rtrb_pro_active(),
				'form_customization'         => SettingsHelper::get_setting( 'formCustomization' ),
				'service_list_settings'      => SettingsHelper::get_setting( 'serviceList' ),
				'custom_fields'              => apply_filters( 'rtrb_custom_fields_data', array() ),
				'chatbot'                    => $this->get_site_chatbot_config(),
			)
		);
	}

	/**
	 * Enqueue site styles and scripts.
	 * NEW - Enqueue site-specific assets
	 *
	 * @since 1.0.0
	 */
	public function enqueue_my_booking_assets() {
		// Only load on frontend
		if ( is_admin() ) {
			return;
		}

		global $post;
		$should_load = false;

		// Check if shortcode is present
		if ( $post && has_shortcode( $post->post_content, 'rtrb_my_booking' ) ) {
			$should_load = true;
		}

		// Check if on the Classified Listing My Account "Bookings" endpoint.
		if ( ! $should_load
			&& function_exists( 'rtcl' )
			&& class_exists( 'RadiusTheme\RadiusBookingPro\Integrations\ClassifiedListing\ListingDashboardHooks' )
			&& \RadiusTheme\RadiusBookingPro\Integrations\ClassifiedListing\ListingDashboardHooks::isBookingsEndpoint()
		) {
			$should_load = true;
		}

		if ( ! $should_load ) {
			return;
		}
		wp_enqueue_media();
		wp_enqueue_script( 'radius_booking_auth', RADIUS_BOOKING_ASSETS . '/js/auth.js', array( 'jquery' ), RADIUS_BOOKING_VERSION, true );

		// Path to site assets
		$assets_dir = untrailingslashit( WP_CONTENT_DIR . '/plugins/radius-booking' ) . '/build';
		$assets_url = untrailingslashit( content_url( 'plugins/radius-booking/build' ) );

		// Initialize Vite with the build manifest
		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/myBooking/main.jsx';
		// Use the manifest entry key for the site entry
		$vite->enqueue(
			$entry, // key from your Vite build (site entry)
			array(
				'handle'              => 'radius-booking-my-booking',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array(), // Add frontend CSS dependencies if needed
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		// Localize site-specific data
		$this->localize_my_booking_scripts();
	}


	/**
	 * Localize scripts for the site/frontend booking interface.
	 *
	 * NEW - Site-specific localization
	 *
	 * @since 1.0.0
	 */

	public function localize_my_booking_scripts() {
		$currency_code = get_rtrb_currency();
		wp_localize_script(
			'radius-booking-my-booking',
			'radius_booking_my_booking_param',
			array(
				'ajax_url'                   => admin_url( 'admin-ajax.php' ),
				'rest_url'                   => rest_url( 'wp/v2/' ),
				'rest_root'                  => esc_url_raw( rest_url() ),
				'nonce'                      => wp_create_nonce( 'wp_rest' ),
				'plugin_url'                 => untrailingslashit( content_url( 'plugins/radius-booking' ) ),
				'current_user'               => get_current_user_id(),
				'is_user_logged_in'          => is_user_logged_in(),
				'available_payment_gateways' => get_rtrb_available_payment_gateways_for_currency( $currency_code['symbol'] ),
				'currency'                   => get_rtrb_currency(),
				'currency_symbol'            => get_rtrb_currency_symbol(),
				'logout'                     => wp_logout_url( rtrb_get_current_url() ),
				'date_time_settings'         => SettingsHelper::get_setting( 'dateTime' ),
				'language'                   => get_locale(),
				'customer_schedule'          => SettingsHelper::get_setting( 'customer' ),
				'phone_settings'             => SettingsHelper::get_setting( 'phone' ),
				'business_settings'          => SettingsHelper::get_setting( 'business' ),
				'tax_settings'               => rtrb_pro_active() ? SettingsHelper::get_setting( 'tax' ) : SettingsHelper::tax(),
				'admin_email'                => get_option( 'admin_email' ),
				'site_url'                   => site_url(),
				'staff_permissions'          => self::getEffectiveStaffPermissions(),
				'is_pro'                     => rtrb_pro_active(),
				'is_sub_staff'               => self::isCurrentUserSubStaff(),
				'payout_enabled'             => function_exists( 'rtcl' ) && ! empty( SettingsHelper::get_setting( 'payout' )['enabled'] ),
				'payout_commission'          => ( function_exists( 'rtcl' ) && ! empty( SettingsHelper::get_setting( 'payout' )['enabled'] ) )
					? array(
						'type' => SettingsHelper::get_setting( 'payout' )['commissionType'] ?? 'percentage',
						'rate' => (float) ( SettingsHelper::get_setting( 'payout' )['commissionRate'] ?? 0 ),
					)
					: null,
				'context'                    => ( function_exists( 'rtcl' )
					&& class_exists( 'RadiusTheme\RadiusBookingPro\Integrations\ClassifiedListing\ListingDashboardHooks' )
					&& \RadiusTheme\RadiusBookingPro\Integrations\ClassifiedListing\ListingDashboardHooks::isBookingsEndpoint()
				) ? 'classified_listing' : '',
			)
		);

		wp_localize_script( 'radius_booking_auth', 'radius_booking_auth_param', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
	}

	/**
	 * Enqueue chatbot widget assets on the frontend.
	 *
	 * Loads the chatbot widget based on display mode settings:
	 * - all_pages: Load on every frontend page
	 * - booking_pages: Load only on pages with booking shortcodes
	 * - specific_pages: Load only on selected pages
	 *
	 * @return void
	 */
	public function enqueue_chatbot_assets() {
		if ( is_admin() ) {
			return;
		}

		// Chatbot is a Pro feature — backend lives in radius-booking-pro.
		if ( ! function_exists( 'rtrb_pro_active' ) || ! rtrb_pro_active() ) {
			return;
		}

		$chatbot_settings = SettingsHelper::get_setting( 'chatbot' );

		// Skip if chatbot is disabled or no API key for the selected provider.
		if ( empty( $chatbot_settings['enabled'] ) ) {
			return;
		}

		$provider = $chatbot_settings['provider'] ?? 'openai';
		$has_key  = 'gemini' === $provider
			? ! empty( $chatbot_settings['geminiApiKey'] )
			: ! empty( $chatbot_settings['apiKey'] );

		if ( ! $has_key ) {
			return;
		}

		$display_mode = $chatbot_settings['displayMode'] ?? 'all_pages';
		$should_load  = false;

		switch ( $display_mode ) {
			case 'all_pages':
				$should_load = true;
				break;

			case 'booking_pages':
				global $post;
				if ( $post && (
						has_shortcode( $post->post_content, 'radius_booking_form' ) ||
						has_shortcode( $post->post_content, 'rtrb_my_booking' ) ||
						has_shortcode( $post->post_content, 'rtrb_service_list' )
					) ) {
					$should_load = true;
				}
				break;

			case 'specific_pages':
				$specific_pages = $chatbot_settings['specificPages'] ?? array();
				if ( ! empty( $specific_pages ) && is_page( array_map( 'intval', $specific_pages ) ) ) {
					$should_load = true;
				}
				break;
		}

		if ( ! $should_load ) {
			return;
		}

		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/chatbot/main.jsx';

		$vite->enqueue(
			$entry,
			array(
				'handle'              => 'radius-booking-chatbot',
				'script-dependencies' => array( 'wp-element' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		// Localize chatbot config — never include API key.
		wp_localize_script(
			'radius-booking-chatbot',
			'radius_booking_chatbot_param',
			array(
				'nonce'           => wp_create_nonce( 'wp_rest' ),
				'rest_root'       => esc_url_raw( rest_url() ),
				'botName'         => $chatbot_settings['botName'] ?? 'Booking Assistant',
				'welcomeMessage'  => $chatbot_settings['welcomeMessage'] ?? '',
				'primaryColor'    => $chatbot_settings['primaryColor'] ?? '#0040ff',
				'position'        => $chatbot_settings['position'] ?? 'bottom-right',
				'responseStyle'   => $chatbot_settings['responseStyle'] ?? 'modern',
				'bubbleIcon'      => $chatbot_settings['bubbleIcon'] ?? 'chat',
				'bubbleImage'     => $chatbot_settings['bubbleImage'] ?? '',
				'poweredBy'       => $chatbot_settings['poweredBy'] ?? 'Powered by Radius Booking',
				'currency'        => get_rtrb_currency(),
				'currency_symbol' => get_rtrb_currency_symbol(),
			)
		);
	}

	/**
	 * Enqueue appointment form assets inside the Elementor editor preview iframe.
	 *
	 * @return void
	 */
	public function enqueue_site_assets_elementor() {
		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/site/main.jsx';

		$vite->enqueue(
			$entry,
			array(
				'handle'              => 'radius-booking-site',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		$this->localize_site_scripts();
	}

	/**
	 * Enqueue service list assets inside the Elementor editor preview iframe.
	 * Always loads — no shortcode/widget detection needed since the editor may add it dynamically.
	 *
	 * @return void
	 */
	public function enqueue_service_list_assets_elementor() {
		$assets_dir = RADIUS_BOOKING_DIR . '/build';
		$assets_url = RADIUS_BOOKING_BUILD;

		$vite = new Vite(
			$assets_dir,
			$assets_url,
			array(
				'preload-js-chunks'  => true,
				'preload-css-chunks' => true,
			)
		);

		$entry = 'src/serviceList/main.jsx';

		$vite->enqueue(
			$entry,
			array(
				'handle'              => 'radius-booking-service-list',
				'script-dependencies' => array( 'wp-element', 'wp-i18n' ),
				'style-dependencies'  => array(),
				'version'             => RADIUS_BOOKING_VERSION,
			)
		);

		$this->localize_service_list_scripts();
	}

	/**
	 * Recursively check if Elementor page data contains a specific widget.
	 *
	 * @param array $elements Elementor elements data.
	 * @param string $widget_name Widget name to find.
	 *
	 * @return bool
	 */
	private function elementor_has_widget( $elements, $widget_name ) {
		if ( ! is_array( $elements ) ) {
			return false;
		}

		foreach ( $elements as $element ) {
			if ( isset( $element['widgetType'] ) && $element['widgetType'] === $widget_name ) {
				return true;
			}
			if ( ! empty( $element['elements'] ) && $this->elementor_has_widget( $element['elements'], $widget_name ) ) {
				return true;
			}
		}

		return false;
	}
}
