<?php
/**
 * Frontend widget rendering and assets.
 *
 * @package Replyan_AI
 */

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

final class Replyan_AI_Frontend {
	public function register_hooks(): void {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'wp_footer', array( $this, 'render_floating_chat' ) );
		add_shortcode( 'replyan_ai', array( $this, 'render_shortcode' ) );
	}

	public function enqueue_assets(): void {
		$settings = Replyan_AI_Settings::get();

		if ( '1' !== $settings['enabled'] ) {
			return;
		}

		Replyan_AI_Assets::enqueue( 'replyan-ai-frontend', 'src/frontend/main.jsx' );

		wp_localize_script(
			'replyan-ai-frontend',
			'replyan_ai_frontend',
			array(
				'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
				'homeUrl'        => home_url(),
				'restUrl'        => esc_url_raw( rest_url( 'replyan/v1/' ) ),
				'restNonce'      => wp_create_nonce( 'wp_rest' ),
				'nonce'          => wp_create_nonce( Replyan_AI_Settings::NONCE_ACTION ),
				'title'          => $settings['title'],
				'welcomeMessage' => $settings['welcome_message'],
				'placeholder'    => $settings['placeholder'],
				'primaryColor'   => $settings['primary_color'],
				'autoOpen'       => '1' === $settings['auto_open'],
				'enableUserSound' => '1' === $settings['enable_user_sound'],
				'userSoundFile'  => $settings['user_sound_file'],
				'pluginUrl'      => REPLYAN_AI_URL,
				'i18n'           => array(
					'open'       => __( 'Open chat', 'replyan-ai' ),
					'close'      => __( 'Close chat', 'replyan-ai' ),
					'send'       => __( 'Send', 'replyan-ai' ),
					'thinking'   => __( 'Thinking...', 'replyan-ai' ),
					'error'      => __( 'Sorry, I could not send that message. Please try again.', 'replyan-ai' ),
					'emptyInput' => __( 'Please type a message first.', 'replyan-ai' ),
				),
			)
		);
	}

	public function render_floating_chat(): void {
		$settings = Replyan_AI_Settings::get();

		if ( '1' !== $settings['enabled'] ) {
			return;
		}

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $this->get_chat_markup( 'replyan-ai-floating replyan-ai-' . esc_attr( $settings['position'] ) );
	}

	public function render_shortcode( $atts ): string {
		$settings = Replyan_AI_Settings::get();

		if ( '1' !== $settings['enabled'] ) {
			return '';
		}

		$atts = shortcode_atts( array(
			'agent' => '',
		), $atts );

		return $this->get_chat_markup( 'replyan-ai-inline', $atts['agent'] );
	}

	private function get_chat_markup( string $classes, string $agent_id = '' ): string {
		$settings = Replyan_AI_Settings::get();

		ob_start();
		$display_mode = false !== strpos( $classes, 'replyan-ai-inline' ) ? 'inline' : 'floating';
		?>
		<div
			class="replyan-ai-root <?php echo esc_attr( $classes ); ?>"
			style="--replyan-ai-primary: <?php echo esc_attr( $settings['primary_color'] ); ?>;"
			data-mode="<?php echo esc_attr( $display_mode ); ?>"
			data-position="<?php echo esc_attr( $settings['position'] ); ?>"
			data-title="<?php echo esc_attr( $settings['title'] ); ?>"
			data-agent="<?php echo esc_attr( $agent_id ); ?>"
		></div>
		<?php
		return (string) ob_get_clean();
	}
}
