<?php

namespace AtlasAiDev\Awfah\Admin;

use AtlasAiDev\Awfah\Plugin;

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

/**
 * Registers and enqueues block editor experiment scripts (Gutenberg integrations).
 */
class Editor_Experiments {

	/**
	 * Cached default prompts (lazy-loaded to avoid early translation calls).
	 *
	 * @var array<string, string>|null
	 */
	private ?array $default_prompts = null;

	public function __construct() {
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_excerpt_generation' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_image_generation' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_summarization' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_title_generation' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_alt_text_generation' ] );
		add_action( 'init', [ $this, 'register_meta' ] );
	}

	/**
	 * Get default prompts. Lazy-loaded so __() is only called at runtime
	 * (after init), not during plugin bootstrap.
	 *
	 * @return array<string, string>
	 */
	private function get_default_prompts(): array {
		if ( null === $this->default_prompts ) {
			$this->default_prompts = [
				'generate-excerpt' => __(
					'You are an expert copywriter. Generate a concise, SEO-friendly excerpt for the following content. '
					. 'The excerpt should be 1-2 sentences, 150-160 characters, and capture the essence of the content. '
					. 'Return only the excerpt text with no additional formatting or explanation.',
					'ai-workflow-automation-ai-agent-hub'
				),
				'image-generation' => __(
					'Create a visually compelling featured image that directly represents the article topic and title. '
					. 'The image must clearly relate to the specific subject matter described in the article. '
					. 'Use professional quality, balanced composition, and make it suitable for a blog header.',
					'ai-workflow-automation-ai-agent-hub'
				),
				'summarization' => __(
					'You are an expert content summarizer. Generate a concise, factual, and neutral summary. '
					. 'Use complete sentences. No markdown, bullets, or numbering — plain text only. '
					. 'Provide a high-level overview, not a list of details.',
					'ai-workflow-automation-ai-agent-hub'
				),
				'suggest-titles' => __(
					'You are an expert copywriter. Generate compelling, SEO-friendly blog post titles. '
					. 'Each title should be under 80 characters, distinct in tone and focus, and reflect actual content. '
					. 'No generic clickbait.',
					'ai-workflow-automation-ai-agent-hub'
				),
				'alt-text-generation' => __(
					'You are an accessibility expert. Generate concise, descriptive alt text for screen readers. '
					. 'Under 125 characters. No "Image of" prefix. Plain text only.',
					'ai-workflow-automation-ai-agent-hub'
				),
			];
		}

		return $this->default_prompts;
	}

	/**
	 * Register post meta for prompts and history.
	 */
	public function register_meta(): void {
		$tools = [ 'generate-excerpt', 'image-generation', 'summarization', 'suggest-titles', 'alt-text-generation' ];

		foreach ( $tools as $tool ) {
			// Per-post prompt.
			register_post_meta(
				'',
				"_awfah_prompt_{$tool}",
				[
					'type'          => 'string',
					'single'        => true,
					'show_in_rest'  => true,
					'auth_callback' => static function () {
						return current_user_can( 'edit_posts' );
					},
				]
			);

			// Per-post generation history.
			register_post_meta(
				'',
				"_awfah_history_{$tool}",
				[
					'type'          => 'array',
					'single'        => true,
					'show_in_rest'  => [
						'schema' => [
							'type'  => 'array',
							'items' => [
								'type'       => 'object',
								'properties' => [
									'result'    => [ 'type' => 'string' ],
									'prompt'    => [ 'type' => 'string' ],
									'tone'      => [ 'type' => 'string' ],
									'style'     => [ 'type' => 'string' ],
									'provider'  => [ 'type' => 'string' ],
									'image_id'  => [ 'type' => 'integer' ],
									'image_url' => [ 'type' => 'string' ],
									'timestamp' => [ 'type' => 'string' ],
								],
							],
						],
					],
					'auth_callback' => static function () {
						return current_user_can( 'edit_posts' );
					},
				]
			);
		}

		// AI-generated flag on attachments (for AILabel component).
		register_post_meta(
			'attachment',
			'_ai_generated',
			[
				'type'          => 'integer',
				'single'        => true,
				'show_in_rest'  => true,
				'auth_callback' => static function () {
					return current_user_can( 'upload_files' );
				},
			]
		);
	}

	/**
	 * Enqueue the excerpt generation experiment script in the block editor.
	 */
	public function enqueue_excerpt_generation(): void {
		$settings = Plugin::get_settings();

		// Only if content_steward module is enabled.
		if ( empty( $settings['modules']['content_steward'] ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		// Only for post types that support excerpts.
		if ( ! post_type_supports( $screen->post_type, 'excerpt' ) ) {
			return;
		}

		// Exclude attachment post type.
		if ( 'attachment' === $screen->post_type ) {
			return;
		}

		$build_dir  = AWFAH_PLUGIN_DIR . 'assets/js/experiments-build/excerpt-generation/';
		$build_url  = AWFAH_PLUGIN_URL . 'assets/js/experiments-build/excerpt-generation/';
		$asset_file = $build_dir . 'index.asset.php';

		// Check if the build exists.
		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset = require $asset_file;

		wp_enqueue_script(
			'aah-excerpt-generation',
			$build_url . 'index.js',
			$asset['dependencies'] ?? [],
			$asset['version'] ?? AWFAH_VERSION,
			true
		);

		// Get the post ID.
		global $post;
		$post_id = $post ? (int) $post->ID : 0;

		$default_prompts = $this->get_default_prompts();

		wp_localize_script(
			'aah-excerpt-generation',
			'aahExcerptGeneration',
			[
				'enabled'       => true,
				'isPro'         => awfah_is_pro_active(),
				'abilitySlug'   => 'awfah-content/generate-excerpt',
				'abilityPath'   => rest_url( 'wp-abilities/v1/abilities/awfah-content/generate-excerpt/run' ),
				'promptsUrl'    => rest_url( 'awfah/v1/prompts/generate-excerpt' ),
				'templatesUrl'  => rest_url( 'awfah/v1/prompts/generate-excerpt/templates' ),
				'explainUrl'    => rest_url( 'awfah/v1/prompts/generate-excerpt/explain' ),
				'providersUrl'  => rest_url( 'awfah/v1/providers' ),
				'modelsUrlBase' => rest_url( 'awfah/v1/providers' ),
				'wpNonce'       => wp_create_nonce( 'wp_rest' ),
				'postId'        => $post_id,
				'defaultPrompt' => $default_prompts['generate-excerpt'],
				'tones'         => [
					[
						'value' => '',
						'label' => __( 'Default', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'professional',
						'label' => __( 'Professional', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'casual',
						'label' => __( 'Casual', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'friendly',
						'label' => __( 'Friendly', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'academic',
						'label' => __( 'Academic', 'ai-workflow-automation-ai-agent-hub' ),
					],
				],
			]
		);

		// Enqueue inline styles for the experiment.
		wp_add_inline_style(
			'wp-components',
			$this->get_excerpt_generation_styles()
		);
	}

	/**
	 * Inline CSS for the excerpt generation experiment.
	 */
	private function get_excerpt_generation_styles(): string {
		return '
			.aah-excerpt-panel { padding: 8px 0; }
			.aah-excerpt-panel .aah-tone-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
			.aah-excerpt-panel .aah-tone-chip { font-size: 11px; padding: 2px 8px; border-radius: 12px; }
			.aah-excerpt-panel .aah-tone-chip.is-active { background: #007cba; color: #fff; }
			.aah-excerpt-panel .aah-result-area { margin-top: 12px; padding: 12px; background: #f0f0f0; border-radius: 4px; }
			.aah-excerpt-panel .aah-result-text { font-size: 13px; line-height: 1.5; margin-bottom: 8px; }
			.aah-excerpt-panel .aah-quality-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 12px; color: #757575; }
			.aah-excerpt-panel .aah-quality-indicator { height: 6px; border-radius: 3px; background: #ddd; flex: 1; overflow: hidden; }
			.aah-excerpt-panel .aah-quality-fill { height: 100%; border-radius: 3px; transition: width 0.3s; }
			.aah-excerpt-panel .aah-quality-good { background: #00a32a; }
			.aah-excerpt-panel .aah-quality-ok { background: #dba617; }
			.aah-excerpt-panel .aah-quality-poor { background: #cc1818; }
			.aah-excerpt-panel .aah-action-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
			.aah-excerpt-panel .aah-settings-popover .components-popover__content { padding: 16px; min-width: 320px; }
			.aah-excerpt-panel .aah-history-item { padding: 8px; margin-bottom: 4px; background: #fff; border: 1px solid #ddd; border-radius: 4px; cursor: pointer; }
			.aah-excerpt-panel .aah-history-item:hover { border-color: #007cba; }
			.aah-excerpt-panel .aah-history-meta { font-size: 11px; color: #757575; margin-top: 4px; }
			.aah-excerpt-panel .aah-context-warning { padding: 8px 10px; margin-bottom: 8px; background: #fcf0e3; border: 1px solid #dba617; border-radius: 4px; font-size: 12px; color: #6e4b00; line-height: 1.4; }
			.aah-excerpt-panel .aah-shortcut-hint { font-size: 11px; color: #999; text-align: right; margin-bottom: 6px; }
			.aah-excerpt-panel .aah-templates-section { margin-bottom: 8px; }
			.aah-excerpt-panel .aah-batch-results { margin-top: 10px; }
			.aah-excerpt-panel .aah-batch-result-card { padding: 10px; margin-bottom: 6px; background: #f0f0f0; border: 1px solid #ddd; border-radius: 4px; }
			.aah-excerpt-panel .aah-batch-result-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 12px; font-weight: 600; }
			.aah-excerpt-panel .aah-batch-result-text { font-size: 12px; line-height: 1.4; margin: 0; }
			.aah-excerpt-panel .aah-explanation-box { margin-top: 8px; padding: 8px 10px; background: #f0f7ff; border: 1px solid #b3d7ff; border-radius: 4px; display: flex; align-items: flex-start; gap: 8px; }
			.aah-excerpt-panel .aah-usage-info { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #999; margin-bottom: 8px; }
		';
	}

	/**
	 * Enqueue the image generation experiment script in the block editor.
	 */
	public function enqueue_image_generation(): void {
		$settings = Plugin::get_settings();

		if ( empty( $settings['modules']['content_steward'] ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		// Only for post types that support featured images (thumbnails).
		if ( ! post_type_supports( $screen->post_type, 'thumbnail' ) ) {
			return;
		}

		if ( 'attachment' === $screen->post_type ) {
			return;
		}

		$build_dir  = AWFAH_PLUGIN_DIR . 'assets/js/experiments-build/image-generation/';
		$build_url  = AWFAH_PLUGIN_URL . 'assets/js/experiments-build/image-generation/';
		$asset_file = $build_dir . 'index.asset.php';

		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset = require $asset_file;

		wp_enqueue_script(
			'aah-image-generation',
			$build_url . 'index.js',
			$asset['dependencies'] ?? [],
			$asset['version'] ?? AWFAH_VERSION,
			true
		);

		global $post;
		$post_id = $post ? (int) $post->ID : 0;

		$default_prompts = $this->get_default_prompts();

		wp_localize_script(
			'aah-image-generation',
			'aahImageGeneration',
			[
				'enabled'       => true,
				'isPro'         => awfah_is_pro_active(),
				'abilitySlug'   => 'awfah-content/generate-feature-image',
				'abilityPath'   => rest_url( 'wp-abilities/v1/abilities/awfah-content/generate-feature-image/run' ),
				'promptsUrl'    => rest_url( 'awfah/v1/prompts/image-generation' ),
				'templatesUrl'  => rest_url( 'awfah/v1/prompts/image-generation/templates' ),
				'explainUrl'    => rest_url( 'awfah/v1/prompts/image-generation/explain' ),
				'providersUrl'  => rest_url( 'awfah/v1/providers' ),
				'modelsUrlBase' => rest_url( 'awfah/v1/providers' ),
				'wpNonce'       => wp_create_nonce( 'wp_rest' ),
				'postId'        => $post_id,
				'defaultPrompt' => $default_prompts['image-generation'],
				'styles'        => [
					[
						'value' => '',
						'label' => __( 'Default', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'photo-realistic',
						'label' => __( 'Photo-realistic', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'illustration',
						'label' => __( 'Illustration', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'abstract',
						'label' => __( 'Abstract', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'minimalist',
						'label' => __( 'Minimalist', 'ai-workflow-automation-ai-agent-hub' ),
					],
					[
						'value' => 'watercolor',
						'label' => __( 'Watercolor', 'ai-workflow-automation-ai-agent-hub' ),
					],
				],
			]
		);

		wp_add_inline_style( 'wp-components', $this->get_image_generation_styles() );
	}

	/**
	 * Inline CSS for the image generation experiment.
	 */
	private function get_image_generation_styles(): string {
		return '
			.aah-image-panel { padding: 8px 0; }
			.aah-image-inline-button { margin-bottom: 4px; }
			.aah-style-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
			.aah-style-chip { font-size: 11px; padding: 2px 8px; border-radius: 12px; }
			.aah-style-chip.is-active { background: #007cba; color: #fff; }
			.aah-result-area { margin-top: 12px; padding: 12px; background: #f0f0f0; border-radius: 4px; }
			.aah-result-image { width: 100%; border-radius: 4px; margin-bottom: 8px; }
			.aah-image-meta { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 12px; color: #757575; flex-wrap: wrap; }
			.aah-action-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
			.aah-history-item { padding: 8px; margin-bottom: 4px; background: #fff; border: 1px solid #ddd; border-radius: 4px; cursor: pointer; display: flex; align-items: center; }
			.aah-history-item:hover { border-color: #007cba; }
			.aah-history-thumb { width: 60px; height: 40px; object-fit: cover; border-radius: 3px; margin-right: 8px; flex-shrink: 0; }
			.aah-history-meta { font-size: 11px; color: #757575; margin-top: 4px; }
			.aah-progress-steps { padding: 12px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 4px; margin-top: 8px; }
			.aah-progress-step { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 12px; }
			.aah-progress-step:last-child { margin-bottom: 0; }
			.aah-progress-step.is-complete { color: #00a32a; }
			.aah-progress-step.is-active { color: #007cba; font-weight: 600; }
			.aah-progress-step.is-pending { color: #999; }
			.aah-ai-label { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; color: #007cba; padding: 2px 8px; background: #f0f7ff; border-radius: 3px; margin-top: 8px; }
			.aah-context-warning { padding: 8px 10px; margin-bottom: 8px; background: #fcf0e3; border: 1px solid #dba617; border-radius: 4px; font-size: 12px; color: #6e4b00; line-height: 1.4; }
			.aah-shortcut-hint { font-size: 11px; color: #999; text-align: right; margin-bottom: 6px; }
			.aah-templates-section { margin-bottom: 8px; }
			.aah-batch-results { margin-top: 10px; }
			.aah-batch-result-card { padding: 10px; margin-bottom: 6px; background: #f0f0f0; border: 1px solid #ddd; border-radius: 4px; }
			.aah-batch-result-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 12px; font-weight: 600; }
			.aah-batch-result-thumb { width: 100%; border-radius: 4px; margin-top: 4px; }
			.aah-explanation-box { margin-top: 8px; padding: 8px 10px; background: #f0f7ff; border: 1px solid #b3d7ff; border-radius: 4px; display: flex; align-items: flex-start; gap: 8px; }
			.aah-image-popover .components-popover__content { overflow-y: auto; }
			.aah-usage-info { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #999; margin-bottom: 8px; }
		';
	}

	/**
	 * Enqueue the summarization experiment script in the block editor.
	 */
	public function enqueue_summarization(): void {
		$settings = Plugin::get_settings();
		if ( empty( $settings['modules']['content_steward'] ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || 'attachment' === $screen->post_type ) {
			return;
		}
		if ( ! post_type_supports( $screen->post_type, 'editor' ) ) {
			return;
		}

		$build_dir  = AWFAH_PLUGIN_DIR . 'assets/js/experiments-build/summarization/';
		$build_url  = AWFAH_PLUGIN_URL . 'assets/js/experiments-build/summarization/';
		$asset_file = $build_dir . 'index.asset.php';

		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset = require $asset_file;

		wp_enqueue_script(
			'aah-summarization',
			$build_url . 'index.js',
			$asset['dependencies'] ?? [],
			$asset['version'] ?? AWFAH_VERSION,
			true
		);

		global $post;
		$post_id         = $post ? (int) $post->ID : 0;
		$default_prompts = $this->get_default_prompts();

		wp_localize_script(
			'aah-summarization',
			'aahSummarization',
			[
				'enabled'       => true,
				'isPro'         => awfah_is_pro_active(),
				'abilitySlug'   => 'awfah-content/summarize',
				'abilityPath'   => rest_url( 'wp-abilities/v1/abilities/awfah-content/summarize/run' ),
				'promptsUrl'    => rest_url( 'awfah/v1/prompts/summarization' ),
				'templatesUrl'  => rest_url( 'awfah/v1/prompts/summarization/templates' ),
				'explainUrl'    => rest_url( 'awfah/v1/prompts/summarization/explain' ),
				'providersUrl'  => rest_url( 'awfah/v1/providers' ),
				'modelsUrlBase' => rest_url( 'awfah/v1/providers' ),
				'wpNonce'       => wp_create_nonce( 'wp_rest' ),
				'postId'        => $post_id,
				'defaultPrompt' => $default_prompts['summarization'],
				'lengths'       => [
					[ 'value' => 'short',  'label' => __( 'Short', 'ai-workflow-automation-ai-agent-hub' ) ],
					[ 'value' => 'medium', 'label' => __( 'Medium', 'ai-workflow-automation-ai-agent-hub' ) ],
					[ 'value' => 'long',   'label' => __( 'Long', 'ai-workflow-automation-ai-agent-hub' ) ],
				],
			]
		);

		wp_add_inline_style( 'wp-components', $this->get_summarization_styles() );
	}

	private function get_summarization_styles(): string {
		return '
			.aah-summarization-panel { padding: 8px 0; }
			.aah-summarization-panel .aah-length-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
			.aah-summarization-panel .aah-length-chip { font-size: 11px; padding: 2px 8px; border-radius: 12px; }
			.aah-summarization-panel .aah-length-chip.is-active { background: #007cba; color: #fff; }
			.aah-summarization-panel .aah-result-area { margin-top: 12px; padding: 12px; background: #f0f0f0; border-radius: 4px; }
			.aah-summarization-panel .aah-result-text { font-size: 13px; line-height: 1.5; margin-bottom: 8px; }
			.aah-summarization-panel .aah-quality-bar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 12px; color: #757575; }
			.aah-summarization-panel .aah-action-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
			.aah-summarization-panel .aah-context-warning { padding: 8px 10px; margin-bottom: 8px; background: #fcf0e3; border: 1px solid #dba617; border-radius: 4px; font-size: 12px; color: #6e4b00; line-height: 1.4; }
			.aah-summarization-panel .aah-shortcut-hint { font-size: 11px; color: #999; text-align: right; margin-bottom: 6px; }
			.aah-summarization-panel .aah-explanation-box { margin-top: 8px; padding: 8px 10px; background: #f0f7ff; border: 1px solid #b3d7ff; border-radius: 4px; display: flex; align-items: flex-start; gap: 8px; }
			.aah-summarization-panel .aah-history-item { padding: 8px; margin-bottom: 4px; background: #fff; border: 1px solid #ddd; border-radius: 4px; cursor: pointer; }
			.aah-summarization-panel .aah-history-item:hover { border-color: #007cba; }
			.aah-summarization-panel .aah-history-meta { font-size: 11px; color: #757575; margin-top: 4px; }
			.aah-summarization-panel .aah-usage-info { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #999; margin-bottom: 8px; }
			.aah-summarization-panel .aah-batch-results { margin-top: 10px; }
			.aah-summarization-panel .aah-batch-result-card { padding: 10px; margin-bottom: 6px; background: #f0f0f0; border: 1px solid #ddd; border-radius: 4px; }
			.aah-summarization-panel .aah-batch-result-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 12px; font-weight: 600; }
			.aah-summarization-panel .aah-batch-result-text { font-size: 12px; line-height: 1.4; margin: 0; }
			.aah-summarization-popover .components-popover__content { overflow-y: auto; }
		';
	}

	/**
	 * Enqueue the title generation experiment script in the block editor.
	 */
	public function enqueue_title_generation(): void {
		$settings = Plugin::get_settings();
		if ( empty( $settings['modules']['content_steward'] ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || 'attachment' === $screen->post_type ) {
			return;
		}
		if ( ! post_type_supports( $screen->post_type, 'title' ) ) {
			return;
		}

		$build_dir  = AWFAH_PLUGIN_DIR . 'assets/js/experiments-build/title-generation/';
		$build_url  = AWFAH_PLUGIN_URL . 'assets/js/experiments-build/title-generation/';
		$asset_file = $build_dir . 'index.asset.php';

		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset = require $asset_file;

		wp_enqueue_script(
			'aah-title-generation',
			$build_url . 'index.js',
			$asset['dependencies'] ?? [],
			$asset['version'] ?? AWFAH_VERSION,
			true
		);

		global $post;
		$post_id         = $post ? (int) $post->ID : 0;
		$default_prompts = $this->get_default_prompts();

		wp_localize_script(
			'aah-title-generation',
			'aahTitleGeneration',
			[
				'enabled'       => true,
				'isPro'         => awfah_is_pro_active(),
				'abilitySlug'   => 'awfah-content/suggest-titles',
				'abilityPath'   => rest_url( 'wp-abilities/v1/abilities/awfah-content/suggest-titles/run' ),
				'promptsUrl'    => rest_url( 'awfah/v1/prompts/suggest-titles' ),
				'templatesUrl'  => rest_url( 'awfah/v1/prompts/suggest-titles/templates' ),
				'explainUrl'    => rest_url( 'awfah/v1/prompts/suggest-titles/explain' ),
				'providersUrl'  => rest_url( 'awfah/v1/providers' ),
				'modelsUrlBase' => rest_url( 'awfah/v1/providers' ),
				'wpNonce'       => wp_create_nonce( 'wp_rest' ),
				'postId'        => $post_id,
				'defaultPrompt' => $default_prompts['suggest-titles'],
				'candidates'    => 5,
			]
		);

		wp_add_inline_style( 'wp-components', $this->get_title_generation_styles() );
	}

	private function get_title_generation_styles(): string {
		return '
			.aah-title-toolbar-wrapper { position: relative; }
			.aah-title-toolbar-container { position: absolute; top: -60px; left: 0; right: 0; z-index: 10; display: none; }
			.aah-title-modal .aah-title-option { padding: 12px; margin-bottom: 8px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 4px; }
			.aah-title-modal .aah-title-option:hover { border-color: #007cba; }
			.aah-title-modal .aah-title-actions { display: flex; gap: 8px; margin-top: 8px; }
			.aah-title-modal .aah-usage-info { font-size: 11px; color: #999; margin-top: 8px; }
			.aah-title-toolbar-button { margin-top: 1px; }
		';
	}

	/**
	 * Enqueue the alt-text generation experiment script in the block editor.
	 */
	public function enqueue_alt_text_generation(): void {
		$settings = Plugin::get_settings();
		if ( empty( $settings['modules']['content_steward'] ) ) {
			return;
		}

		$build_dir  = AWFAH_PLUGIN_DIR . 'assets/js/experiments-build/alt-text-generation/';
		$build_url  = AWFAH_PLUGIN_URL . 'assets/js/experiments-build/alt-text-generation/';
		$asset_file = $build_dir . 'index.asset.php';

		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset = require $asset_file;

		wp_enqueue_script(
			'aah-alt-text-generation',
			$build_url . 'index.js',
			$asset['dependencies'] ?? [],
			$asset['version'] ?? AWFAH_VERSION,
			true
		);

		$default_prompts = $this->get_default_prompts();

		wp_localize_script(
			'aah-alt-text-generation',
			'aahAltTextGeneration',
			[
				'enabled'       => true,
				'isPro'         => awfah_is_pro_active(),
				'abilityPath'   => rest_url( 'wp-abilities/v1/abilities/awfah-content/generate-alt-text/run' ),
				'promptsUrl'    => rest_url( 'awfah/v1/prompts/alt-text-generation' ),
				'explainUrl'    => rest_url( 'awfah/v1/prompts/alt-text-generation/explain' ),
				'providersUrl'  => rest_url( 'awfah/v1/providers' ),
				'modelsUrlBase' => rest_url( 'awfah/v1/providers' ),
				'wpNonce'       => wp_create_nonce( 'wp_rest' ),
				'defaultPrompt' => $default_prompts['alt-text-generation'],
			]
		);

		wp_add_inline_style( 'wp-components', $this->get_alt_text_generation_styles() );
	}

	private function get_alt_text_generation_styles(): string {
		return '
			.aah-alt-text-panel { padding: 12px; }
			.aah-alt-text-panel .aah-alt-preview { padding: 10px; margin: 8px 0; background: #f0f0f0; border-radius: 4px; }
			.aah-alt-text-panel .aah-alt-actions { display: flex; gap: 8px; margin-top: 8px; }
			.aah-alt-text-panel .aah-usage-info { font-size: 11px; color: #999; margin-top: 4px; }
		';
	}
}
