<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Arcai_Frontend {

	public function register_hooks() {
		add_action( 'init', array( $this, 'register_block' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
		add_action( 'wp_footer', array( $this, 'render_bubble' ) );
		add_shortcode( 'archivist_ai', array( $this, 'render_inline' ) );
	}

	private function configured() {
		return '' !== Arcai_Settings::get( 'api_key' );
	}

	private function visitor_allowed() {
		return ! Arcai_Settings::get( 'require_login' ) || is_user_logged_in();
	}

	private function bubble_active() {
		return $this->configured() && (bool) Arcai_Settings::get( 'bubble_enabled' ) && $this->visitor_allowed();
	}

	public function register_assets() {
		wp_register_style( 'arcai-chat', ARCAI_PLUGIN_URL . 'assets/css/chat.css', array(), ARCAI_VERSION );
		wp_register_script( 'arcai-chat', ARCAI_PLUGIN_URL . 'assets/js/chat.js', array(), ARCAI_VERSION, true );

		wp_localize_script(
			'arcai-chat',
			'arcaiChat',
			array(
				'endpoint'       => esc_url_raw( rest_url( 'archivist-ai/v1/chat' ) ),
				'streamEndpoint' => esc_url_raw( rest_url( 'archivist-ai/v1/chat/stream' ) ),
				'nonce'          => is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : '',
				'greeting'       => Arcai_Settings::get( 'greeting' ),
				'color'          => Arcai_Settings::get( 'bubble_color' ),
				'maxTurns'       => 10,
				'i18n'           => array(
					'placeholder' => __( 'Ask about our articles…', 'archivist-ai' ),
					'send'        => __( 'Send', 'archivist-ai' ),
					'sources'     => __( 'Sources', 'archivist-ai' ),
					'error'       => __( 'Something went wrong. Please try again.', 'archivist-ai' ),
					'turnLimit'   => __( 'This conversation has reached its limit — refresh to start a new one.', 'archivist-ai' ),
					'title'       => __( 'Archive search', 'archivist-ai' ),
				),
			)
		);

		if ( $this->bubble_active() ) {
			$this->enqueue_assets();
		}
	}

	private function enqueue_assets() {
		wp_enqueue_style( 'arcai-chat' );
		wp_enqueue_script( 'arcai-chat' );
	}

	public function render_bubble() {
		if ( ! $this->bubble_active() || is_admin() ) {
			return;
		}
		echo '<div id="arcai-root" aria-live="polite"></div>';
	}

	/**
	 * [archivist_ai] shortcode and block render: an inline chat panel.
	 * When present on a page, the widget mounts here instead of the bubble.
	 */
	public function render_inline() {
		if ( ! $this->configured() ) {
			return current_user_can( 'manage_options' )
				? '<p><em>' . esc_html__( 'Archivist AI: add your API key in the plugin settings to enable the chat.', 'archivist-ai' ) . '</em></p>'
				: '';
		}
		if ( ! $this->visitor_allowed() ) {
			return '<p class="arcai-login-notice">' . sprintf(
				/* translators: %s: login URL */
				wp_kses_post( __( 'Please <a href="%s">log in</a> to use the archive search.', 'archivist-ai' ) ),
				esc_url( wp_login_url( get_permalink() ) )
			) . '</p>';
		}
		$this->enqueue_assets();
		return '<div class="arcai-inline-host" data-arcai-inline aria-live="polite"></div>';
	}

	public function register_block() {
		wp_register_script(
			'arcai-block',
			ARCAI_PLUGIN_URL . 'assets/js/block.js',
			array( 'wp-blocks', 'wp-element', 'wp-i18n' ),
			ARCAI_VERSION,
			true
		);

		register_block_type(
			'archivist-ai/chat',
			array(
				'editor_script'   => 'arcai-block',
				'render_callback' => array( $this, 'render_inline' ),
			)
		);
	}
}
