<?php
/**
 * AI Form Builder chat widget partial.
 *
 * @package BasicForms
 */

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

use WP_PowerSuite\AI\ConnectionSettings;
use BasicForms\AiSettings;

$ai_mode       = isset( $ai_mode ) ? sanitize_key( (string) $ai_mode ) : 'create';
$is_edit_mode  = 'edit' === $ai_mode;
$ai_public     = AiSettings::getPublic();
$has_api_key   = ! empty( $ai_public['has_api_key'] );
$provider      = (string) ( $ai_public['provider'] ?? 'openai' );
$model         = (string) ( $ai_public['model'] ?? '' );
$providers     = class_exists( ConnectionSettings::class ) ? ConnectionSettings::providerLabels() : [];
$settings_url  = admin_url( 'admin.php?page=powersuite-basic-forms&view=settings' );
$connections_url = admin_url( 'admin.php?page=powersuite-settings&tab=ai-connections' );
?>
<div id="wpps-bf-ai-chat" class="wpps-bf-ai-chat" data-ai-mode="<?php echo esc_attr( $ai_mode ); ?>" aria-hidden="true">
	<button type="button" class="wpps-bf-ai-chat-launcher" id="wpps-bf-ai-chat-launcher" aria-expanded="false" aria-controls="wpps-bf-ai-chat-panel">
		<span class="wpps-bf-ai-chat-launcher__icon wpps-bf-ai-icon" aria-hidden="true">
			<?php include __DIR__ . '/ai-icon.php'; ?>
		</span>
		<span class="wpps-bf-ai-chat-launcher__label"><?php esc_html_e( 'AI Assistant', 'wppowersuite-lite' ); ?></span>
	</button>

	<div class="wpps-bf-ai-chat-panel" id="wpps-bf-ai-chat-panel" role="dialog" aria-modal="false" aria-labelledby="wpps-bf-ai-chat-title" hidden>
		<div class="wpps-bf-ai-chat-panel__header">
			<div class="wpps-bf-ai-chat-panel__title-wrap">
				<span class="wpps-bf-ai-icon" aria-hidden="true"><?php include __DIR__ . '/ai-icon.php'; ?></span>
				<div>
					<h2 id="wpps-bf-ai-chat-title"><?php echo $is_edit_mode ? esc_html__( 'AI Form Assistant', 'wppowersuite-lite' ) : esc_html__( 'AI Form Builder', 'wppowersuite-lite' ); ?></h2>
					<?php if ( $has_api_key ) : ?>
						<p class="wpps-bf-ai-chat-panel__meta">
							<?php
							printf(
								/* translators: 1: provider label, 2: model id */
								esc_html__( '%1$s · %2$s', 'wppowersuite-lite' ),
								esc_html( $providers[ $provider ] ?? $provider ),
								esc_html( $model )
							);
							?>
						</p>
					<?php endif; ?>
				</div>
			</div>
			<div class="wpps-bf-ai-chat-panel__actions">
				<button type="button" class="button-link wpps-bf-ai-chat-clear" id="wpps-bf-ai-chat-clear" title="<?php esc_attr_e( 'Clear chat', 'wppowersuite-lite' ); ?>">
					<span class="dashicons dashicons-trash" aria-hidden="true"></span>
					<span class="screen-reader-text"><?php esc_html_e( 'Clear chat', 'wppowersuite-lite' ); ?></span>
				</button>
				<button type="button" class="button-link wpps-bf-ai-chat-close" id="wpps-bf-ai-chat-close" title="<?php esc_attr_e( 'Minimize', 'wppowersuite-lite' ); ?>">
					<span class="dashicons dashicons-minus" aria-hidden="true"></span>
					<span class="screen-reader-text"><?php esc_html_e( 'Minimize', 'wppowersuite-lite' ); ?></span>
				</button>
			</div>
		</div>

		<div class="wpps-bf-ai-chat-panel__body">
			<?php if ( ! $has_api_key ) : ?>
				<div class="notice notice-warning inline wpps-bf-ai-chat__notice">
					<p>
						<?php esc_html_e( 'Configure an AI provider and model before generating forms.', 'wppowersuite-lite' ); ?>
						<a href="<?php echo esc_url( $settings_url ); ?>"><?php esc_html_e( 'Basic Forms settings', 'wppowersuite-lite' ); ?></a>
						<?php esc_html_e( '·', 'wppowersuite-lite' ); ?>
						<a href="<?php echo esc_url( $connections_url ); ?>"><?php esc_html_e( 'AI Connections', 'wppowersuite-lite' ); ?></a>
					</p>
				</div>
			<?php endif; ?>

			<div class="wpps-bf-ai-chat-messages" id="wpps-bf-ai-chat-messages" aria-live="polite" aria-relevant="additions"></div>
		</div>

		<div class="wpps-bf-ai-chat-panel__composer">
			<label class="screen-reader-text" for="wpps-bf-ai-prompt"><?php esc_html_e( 'Message', 'wppowersuite-lite' ); ?></label>
			<textarea
				id="wpps-bf-ai-prompt"
				class="wpps-bf-input wpps-bf-ai-chat__input"
				rows="2"
				maxlength="8000"
				placeholder="<?php echo esc_attr( $is_edit_mode ? __( 'Ask for changes to this form…', 'wppowersuite-lite' ) : __( 'Describe the form you want to build…', 'wppowersuite-lite' ) ); ?>"
				<?php disabled( ! $has_api_key ); ?>
			></textarea>
			<div class="wpps-bf-ai-chat__composer-meta">
				<div class="wpps-bf-ai-chat__composer-tools">
					<button type="button" class="button-link wpps-bf-ai-improve" id="wpps-bf-ai-improve" title="<?php esc_attr_e( 'Improve prompt', 'wppowersuite-lite' ); ?>" <?php disabled( ! $has_api_key ); ?>>
						<span class="wpps-bf-ai-improve__icon" aria-hidden="true"><?php include __DIR__ . '/ai-icon.php'; ?></span>
						<span class="wpps-bf-ai-improve__spinner" hidden><span class="wpps-bf-ai-spinner wpps-bf-ai-spinner--accent"></span></span>
						<span class="screen-reader-text"><?php esc_html_e( 'Improve prompt', 'wppowersuite-lite' ); ?></span>
					</button>
					<button type="button" class="button-link wpps-bf-ai-improve-undo" id="wpps-bf-ai-improve-undo" hidden>
						<?php esc_html_e( 'Undo', 'wppowersuite-lite' ); ?>
					</button>
					<span class="description wpps-bf-ai-chat__char-count">
						<span id="wpps-bf-ai-prompt-count">0</span>
						<?php esc_html_e( '/ 8000 characters', 'wppowersuite-lite' ); ?>
					</span>
				</div>
				<button type="button" class="button button-primary" id="wpps-bf-ai-send" <?php disabled( ! $has_api_key ); ?>>
					<span class="wpps-bf-ai-send__label"><?php esc_html_e( 'Send', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-bf-ai-send__loader" hidden><span class="wpps-bf-ai-spinner" aria-hidden="true"></span><?php esc_html_e( 'Sending…', 'wppowersuite-lite' ); ?></span>
				</button>
			</div>
		</div>
	</div>
</div>
