<?php
/**
 * Elementor editor asset loader.
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Integrations\Elementor;

use SEOvaultSEO\Core\PostMeta;
use SEOvaultSEO\Utils\Formatter;
use SEOvaultSEO\Utils\Guidelines;

/**
 * Enqueues and configures the SEOvault Elementor panel.
 */
class EditorAssets {

	/**
	 * Enqueue assets inside Elementor's editor shell.
	 *
	 * @return void
	 */
	public static function enqueue() {
		$post_id = Bootstrap::get_request_post_id();
		$post    = $post_id ? get_post( $post_id ) : null;

		if ( ! $post || ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		$is_v4        = self::is_v4_atomic_active();
		$dependencies = array( 'jquery' );
		$script_data  = self::get_script_data( $post );

		if ( $is_v4 ) {
			wp_enqueue_script(
				'seovaultseo-elementor-v4',
				SEOVAULT_SEO_URL . 'assets/js/elementor-seo-v4.js',
				array( 'jquery' ),
				SEOVAULT_SEO_VERSION . '-1',
				true
			);
			$dependencies[] = 'seovaultseo-elementor-v4';
			wp_localize_script( 'seovaultseo-elementor-v4', 'seovaultseoElementor', $script_data );
			wp_set_script_translations( 'seovaultseo-elementor-v4', 'seovault-ai', SEOVAULT_SEO_DIR . 'languages/' );
		}

		wp_enqueue_style(
			'seovaultseo-elementor',
			SEOVAULT_SEO_URL . 'assets/css/elementor-seo.css',
			array(),
			SEOVAULT_SEO_VERSION . '-1'
		);

		wp_enqueue_script(
			'seovaultseo-elementor',
			SEOVAULT_SEO_URL . 'assets/js/elementor-seo.js',
			$dependencies,
			SEOVAULT_SEO_VERSION . '-1',
			true
		);

		wp_set_script_translations( 'seovaultseo-elementor', 'seovault-ai', SEOVAULT_SEO_DIR . 'languages/' );
		wp_localize_script(
			'seovaultseo-elementor',
			'seovaultseoElementor',
			$script_data
		);
	}

	/**
	 * Build localized editor state.
	 *
	 * @param \WP_Post $post Current post.
	 * @return array
	 */
	public static function get_script_data( $post ) {
		$post_id = (int) $post->ID;

		return array(
			'ajaxUrl'           => admin_url( 'admin-ajax.php' ),
			'saveNonce'         => wp_create_nonce( 'seovaultseo_elementor_save' ),
			'assessmentNonce'   => wp_create_nonce( 'seovaultseo_metabox_nonce' ),
			'postId'            => $post_id,
			'postTitle'         => $post->post_title,
			'permalink'         => get_permalink( $post_id ),
			'isV4Atomic'        => self::is_v4_atomic_active(),
			'autoTitle'         => Formatter::get_effective_title( $post_id ),
			'autoDescription'   => Formatter::get_effective_description( $post_id ),
			'titleLimits'       => Guidelines::get_title_limits(),
			'descriptionLimits' => Guidelines::get_description_limits(),
			'fields'            => array(
				'seoTitle'       => PostMeta::get_seo_title( $post_id ),
				'seoDescription' => PostMeta::get_seo_description( $post_id ),
				'focusKeyword'   => PostMeta::get_focus_keyword( $post_id ),
				'canonicalUrl'   => PostMeta::get_canonical_url( $post_id ),
				'noindex'        => PostMeta::is_noindex( $post_id ),
				'nofollow'       => PostMeta::is_nofollow( $post_id ),
				'ogTitle'        => PostMeta::get_og_title( $post_id ),
				'ogDescription'  => PostMeta::get_og_description( $post_id ),
				'ogImage'        => PostMeta::get_og_image( $post_id ),
			),
			'strings'           => array(
				'panelTitle'       => __( 'SEOvault AI', 'seovault-ai' ),
				'openPanel'        => __( 'Open SEOvault AI', 'seovault-ai' ),
				'closePanel'       => __( 'Back to Elementor', 'seovault-ai' ),
				'searchAppearance' => __( 'Search appearance', 'seovault-ai' ),
				'seoTitle'         => __( 'SEO title', 'seovault-ai' ),
				'seoDescription'   => __( 'SEO description', 'seovault-ai' ),
				'focusKeyword'     => __( 'Focus keyword', 'seovault-ai' ),
				'canonicalUrl'     => __( 'Canonical URL', 'seovault-ai' ),
				'robots'           => __( 'Robots', 'seovault-ai' ),
				'noindex'          => __( 'Noindex this page', 'seovault-ai' ),
				'nofollow'         => __( 'Nofollow links on this page', 'seovault-ai' ),
				'social'           => __( 'Social sharing', 'seovault-ai' ),
				'ogTitle'          => __( 'Social title', 'seovault-ai' ),
				'ogDescription'    => __( 'Social description', 'seovault-ai' ),
				'ogImage'          => __( 'Social image URL', 'seovault-ai' ),
				'assessment'       => __( 'Live SEO checks', 'seovault-ai' ),
				'contentWords'     => __( 'Content words', 'seovault-ai' ),
				'save'             => __( 'Save SEO settings', 'seovault-ai' ),
				'saving'           => __( 'Saving…', 'seovault-ai' ),
				'saved'            => __( 'SEO settings saved.', 'seovault-ai' ),
				'saveError'        => __( 'SEO settings could not be saved.', 'seovault-ai' ),
				'loading'          => __( 'Checking live content…', 'seovault-ai' ),
				'assessmentError'  => __( 'Live SEO checks could not be updated.', 'seovault-ai' ),
				'needsWork'        => __( 'Needs Work', 'seovault-ai' ),
				'okay'             => __( 'Okay', 'seovault-ai' ),
				'good'             => __( 'Good', 'seovault-ai' ),
				'unknown'          => __( 'Unknown', 'seovault-ai' ),
				'v4Active'         => __( 'Atomic Editor content sync active', 'seovault-ai' ),
			),
		);
	}

	/**
	 * Detect Elementor's authoritative Atomic Editor opt-in.
	 *
	 * @return bool
	 */
	public static function is_v4_atomic_active() {
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return false;
		}

		$elementor = \Elementor\Plugin::$instance ?? null;
		if ( ! $elementor || ! isset( $elementor->experiments ) ) {
			return false;
		}

		if ( ! method_exists( $elementor->experiments, 'is_feature_active' ) ) {
			return false;
		}

		return (bool) $elementor->experiments->is_feature_active( 'e_opt_in_v4' );
	}
}
