<?php
/**
 * Stable shared admin asset registration for Free and Pro.
 *
 * @package SEOvaultSEO\Extension
 */

namespace SEOvaultSEO\Extension;

use SEOvaultSEO\Admin\CacheCompatibilityNotice;
use SEOvaultSEO\Core\I18n;
use SEOvaultSEO\Core\Options;

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

/**
 * Registers seovaultseo-admin-foundation / seovaultseo-admin-components.
 *
 * Free continues to enqueue the legacy `seovaultseo-admin` alias handles so
 * existing feature scripts keep working. Pro must depend on the stable
 * foundation/components handles — never hardcode Free asset URLs.
 */
class Assets {

	/**
	 * Hook registration.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register' ), 0 );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register' ), 0 );
	}

	/**
	 * Register stable + legacy alias handles (idempotent).
	 *
	 * @return void
	 */
	public static function register() {
		// Re-register when WP registries were reset (e.g. PHPUnit) even if this
		// process already registered handles earlier in the same request lifecycle.
		if (
			wp_style_is( ExtensionApi::HANDLE_ADMIN_FOUNDATION, 'registered' )
			&& wp_script_is( ExtensionApi::HANDLE_ADMIN_COMPONENTS, 'registered' )
			&& wp_style_is( ExtensionApi::HANDLE_ADMIN_LEGACY, 'registered' )
			&& wp_script_is( ExtensionApi::HANDLE_ADMIN_LEGACY, 'registered' )
		) {
			return;
		}

		$style_ver  = SEOVAULT_SEO_VERSION . '-34';
		$script_ver = SEOVAULT_SEO_VERSION . '-13';

		wp_register_style(
			ExtensionApi::HANDLE_ADMIN_FOUNDATION,
			SEOVAULT_SEO_URL . 'assets/css/admin.css',
			array(),
			$style_ver
		);

		wp_register_script(
			ExtensionApi::HANDLE_ADMIN_COMPONENTS,
			SEOVAULT_SEO_URL . 'assets/js/admin.js',
			array( 'jquery' ),
			$script_ver,
			true
		);

		// Legacy Free handles: empty src, depend on stable handles.
		wp_register_style(
			ExtensionApi::HANDLE_ADMIN_LEGACY,
			false,
			array( ExtensionApi::HANDLE_ADMIN_FOUNDATION ),
			$style_ver
		);

		wp_register_script(
			ExtensionApi::HANDLE_ADMIN_LEGACY,
			false,
			array( ExtensionApi::HANDLE_ADMIN_COMPONENTS ),
			$script_ver,
			true
		);
	}

	/**
	 * Enqueue Free admin shell assets and localize the components handle.
	 *
	 * @return void
	 */
	public static function enqueue_admin_shell() {
		self::register();

		wp_enqueue_style( ExtensionApi::HANDLE_ADMIN_LEGACY );
		wp_enqueue_script( ExtensionApi::HANDLE_ADMIN_LEGACY );

		wp_set_script_translations(
			ExtensionApi::HANDLE_ADMIN_COMPONENTS,
			'seovault-ai',
			SEOVAULT_SEO_DIR . 'languages/'
		);

		wp_localize_script(
			ExtensionApi::HANDLE_ADMIN_COMPONENTS,
			ExtensionApi::LOCALIZE_ADMIN,
			I18n::merge_admin_script_strings(
				array(
					'ajax_url'     => admin_url( 'admin-ajax.php' ),
					'nonce'        => wp_create_nonce( 'seovaultseo_admin' ),
					'site_key'     => Options::get_site_key(),
					'rest_url'     => rest_url( 'seovaultai/v1/' ),
					'version'      => SEOVAULT_SEO_VERSION,
					'is_connected' => Options::is_connection_verified() && ! empty( Options::get_connected_at() ),
					'cache_notice' => CacheCompatibilityNotice::get_localize_data(),
				)
			)
		);
	}

	/**
	 * Whether a handle is part of the documented shared asset contract.
	 *
	 * @param string $handle Script or style handle.
	 * @return bool
	 */
	public static function is_documented_handle( $handle ) {
		$handle = (string) $handle;
		return in_array(
			$handle,
			array(
				ExtensionApi::HANDLE_ADMIN_FOUNDATION,
				ExtensionApi::HANDLE_ADMIN_COMPONENTS,
				ExtensionApi::HANDLE_ADMIN_LEGACY,
			),
			true
		);
	}
}
