<?php
/**
 * Gutenberg Integration Class
 *
 * Manages integration with WordPress Gutenberg editor
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Gutenberg Integration class
 */
class SmartyPress_AI_Gutenberg_Integration {

	/**
	 * AI Providers
	 *
	 * @var array
	 */
	private $providers;

	/**
	 * Constructor
	 *
	 * @param array $providers AI providers instances.
	 */
	public function __construct( $providers ) {
		$this->providers = $providers;
	}

	/**
	 * Enqueue editor assets
	 */
	public function enqueue_editor_assets() {
		$settings        = get_option( 'smartypress_ai_settings', array() );
		$enabled_types   = $settings['enabled_post_types'] ?? array( 'post', 'page' );
		$current_screen  = get_current_screen();

		// Check if current screen exists and post type is enabled
		if ( ! $current_screen || ! isset( $current_screen->post_type ) ) {
			return;
		}

		if ( ! in_array( $current_screen->post_type, $enabled_types, true ) ) {
			return;
		}

		// Enqueue compiled React app
		$asset_file = SPAI_CG_PLUGIN_DIR . '/assets/dist/gutenberg-plugin.asset.php';
		
		if ( file_exists( $asset_file ) ) {
			$asset = require $asset_file;
		} else {
			$asset = array(
				'dependencies' => array(
					'wp-plugins',
					'wp-edit-post',
					'wp-element',
					'wp-components',
					'wp-data',
					'wp-compose',
					'wp-hooks',
					'wp-i18n',
				),
				'version'      => SPAI_CG_PLUGIN_VERSION,
			);
		}

		wp_enqueue_script(
			'smartypress-ai-gutenberg',
			SPAI_CG_PLUGIN_URL . '/assets/dist/gutenberg-plugin.js',
			$asset['dependencies'],
			$asset['version'],
			true
		);

		// Set script translations for Gutenberg plugin
		wp_set_script_translations(
			'smartypress-ai-gutenberg',
			'smartypress-ai-engine',
			SMARTYPRESS_AI_PLUGIN_DIR . 'languages'
		);

		wp_enqueue_style(
			'smartypress-ai-gutenberg',
			SPAI_CG_PLUGIN_URL . '/assets/dist/gutenberg-plugin.css',
			array( 'wp-components' ),
			SPAI_CG_PLUGIN_VERSION
		);

		// Localize script with data
		wp_localize_script(
			'smartypress-ai-gutenberg',
			'smartypressAI',
			array(
				'restUrl'        => rest_url( 'smartypress/v1/' ),
				'nonce'          => wp_create_nonce( 'wp_rest' ),
				'pluginUrl'      => SMARTYPRESS_AI_PLUGIN_URL, // Use main plugin URL for assets
				'adminUrl'       => admin_url(),
				'settings'       => $settings,
				'providers'      => array_keys( $this->providers ),
				'providerModels' => $this->get_provider_models(),
				'localeDefaultLanguage' => smartypress_ai_get_language_by_locale(),
			)
		);

		// Enqueue wand plugin
		$wand_asset_file = SPAI_CG_PLUGIN_DIR . '/assets/dist/wand-plugin.asset.php';
		
		if ( file_exists( $wand_asset_file ) ) {
			$wand_asset = require $wand_asset_file;
		} else {
			$wand_asset = array(
				'dependencies' => array_merge(
					$asset['dependencies'],
					array( 'wp-rich-text', 'wp-block-editor' )
				),
				'version'      => SPAI_CG_PLUGIN_VERSION,
			);
		}

		wp_enqueue_script(
			'smartypress-ai-wand',
			SPAI_CG_PLUGIN_URL . '/assets/dist/wand-plugin.js',
			$wand_asset['dependencies'],
			$wand_asset['version'],
			true
		);

		// Set script translations for Wand plugin
		wp_set_script_translations(
			'smartypress-ai-wand',
			'smartypress-ai-engine',
			SMARTYPRESS_AI_PLUGIN_DIR . 'languages'
		);

		// Localize wand script
		wp_localize_script(
			'smartypress-ai-wand',
			'smartypressWand',
			array(
				'features' => SmartyPress_AI_Wand_Features::get_features(),
				'restUrl'  => rest_url( 'smartypress/v1/' ),
				'nonce'    => wp_create_nonce( 'wp_rest' ),
				'pluginUrl' => SMARTYPRESS_AI_PLUGIN_URL, // Use main plugin URL for assets
				'adminUrl'  => admin_url(),
				'providers' => array_keys( $this->providers ),
				'settings'  => $settings,
			)
		);

		// Also add providerModels to wand script
		wp_localize_script(
			'smartypress-ai-wand',
			'smartypressAI',
			array(
				'restUrl'        => rest_url( 'smartypress/v1/' ),
				'nonce'          => wp_create_nonce( 'wp_rest' ),
				'pluginUrl'      => SMARTYPRESS_AI_PLUGIN_URL,
				'adminUrl'       => admin_url(),
				'settings'       => $settings,
				'providers'      => array_keys( $this->providers ),
				'providerModels' => $this->get_provider_models(),
				'localeDefaultLanguage' => smartypress_ai_get_language_by_locale(),
			)
		);
	}

	/**
	 * Get available models for each provider
	 *
	 * @return array Provider models.
	 */
	private function get_provider_models() {
		$provider_models = array();
		
		// Use static Providers class to get available providers
		// This ensures we get the latest initialized providers
		$available = SmartyPress_AI_Providers::get_available_providers();
		
		foreach ( $available as $provider_name => $provider ) {
			if ( method_exists( $provider, 'get_available_models' ) ) {
				$provider_models[ $provider_name ] = $provider->get_available_models();
			}
		}
		
		// Fallback to instance providers if static class has none
		if ( empty( $provider_models ) ) {
			foreach ( $this->providers as $provider_name => $provider ) {
				if ( method_exists( $provider, 'get_available_models' ) ) {
					$provider_models[ $provider_name ] = $provider->get_available_models();
				}
			}
		}
		
		return $provider_models;
	}
}

