<?php
/**
 * Gutenberg Panel (Internal Link Suggester).
 *
 * Registers a block-editor sidebar panel that mirrors the classic-editor meta
 * box: scan the current content, list internal-link suggestions, and insert a
 * real link with one click — this time into Gutenberg's block content.
 *
 * It reuses the SAME AJAX endpoint as the classic meta box
 * (SuggesterMetaBox::AJAX_ACTION / ::NONCE), so there is one suggestion engine
 * and one contract, not two. This class only handles enqueuing the no-build
 * JavaScript on the block editor and passing config to it.
 *
 * No build step: the panel is plain JavaScript using the global `wp.*`
 * packages (element, plugins, editPost, components, data), loaded with the
 * matching wp-* script dependencies so they're guaranteed present.
 *
 * @package Platform\Helm\Modules\Link_Suggester
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Link_Suggester;

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

final class GutenbergPanel {

	private const HANDLE = 'platform-helm-link-suggester-gutenberg';

	public function register_hooks(): void {
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue' ] );
	}

	public function enqueue(): void {
		$settings = SuggesterSettings::get();
		if ( ! $settings['enabled'] ) {
			return;
		}

		// Only load on post types where the Suggester is active.
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		$ptype  = $screen && isset( $screen->post_type ) ? (string) $screen->post_type : '';
		if ( '' === $ptype || ! in_array( $ptype, (array) $settings['editor_post_types'], true ) ) {
			return;
		}

		// The user must be able to edit this content.
		if ( ! current_user_can( 'edit_posts' ) ) {
			return;
		}

		wp_enqueue_script(
			self::HANDLE,
			PLATFORM_HELM_URL . 'assets/js/link-suggester-gutenberg.js',
			[
				'wp-element',
				'wp-plugins',
				'wp-edit-post',
				'wp-components',
				'wp-data',
				'wp-i18n',
				'wp-compose',
			],
			PLATFORM_HELM_VERSION,
			true
		);

		wp_localize_script(
			self::HANDLE,
			'PlatformHelmLSGutenberg',
			[
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'action'  => SuggesterMetaBox::AJAX_ACTION,
				'nonce'   => wp_create_nonce( SuggesterMetaBox::NONCE ),
				'i18n'    => [
					'panelTitle'   => __( 'Internal Link Suggestions', 'platform-helm' ),
					'scan'         => __( 'Scan content for links', 'platform-helm' ),
					'scanning'     => __( 'Scanning…', 'platform-helm' ),
					'intro'        => __( 'Find existing posts you could link to from this content.', 'platform-helm' ),
					'none'         => __( 'No internal link opportunities found in this content.', 'platform-helm' ),
					'failed'       => __( 'Scan failed. Try again.', 'platform-helm' ),
					'insert'       => __( 'Insert link', 'platform-helm' ),
					'insertedOk'   => __( 'Linked ✓', 'platform-helm' ),
					'notFound'     => __( 'Phrase not found in a linkable text block', 'platform-helm' ),
					'copy'         => __( 'Copy URL', 'platform-helm' ),
					'copied'       => __( 'Copied!', 'platform-helm' ),
					'external'     => __( 'external', 'platform-helm' ),
					'rescan'       => __( 'Rescan', 'platform-helm' ),
				],
			]
		);

		// Tiny stylesheet for the panel, inlined onto the handle.
		wp_add_inline_style( 'wp-edit-post', $this->inline_css() );
	}

	private function inline_css(): string {
		return '
		.phelm-gb-intro{font-size:12px;color:#5C6B82;margin:0 0 10px;}
		.phelm-gb-item{border:1px solid #e0e0e0;border-radius:6px;padding:8px 10px;margin-bottom:8px;background:#fff;}
		.phelm-gb-phrase{font-weight:600;color:#0047AB;font-size:12px;}
		.phelm-gb-ext{display:inline-block;font-size:9px;font-weight:700;text-transform:uppercase;letter-spacing:.3px;color:#7A5C00;background:#FFF3CD;border:1px solid #FFE08A;border-radius:3px;padding:0 4px;margin-left:4px;vertical-align:middle;}
		.phelm-gb-target{font-size:11px;color:#555;margin:2px 0 4px;}
		.phelm-gb-snippet{font-size:11px;color:#888;font-style:italic;margin-bottom:6px;line-height:1.4;}
		.phelm-gb-actions{display:flex;gap:6px;}
		.phelm-gb-empty{font-size:12px;color:#777;}
		.phelm-gb-note{font-size:11px;color:#B85C00;margin-top:4px;}
		';
	}
}
