<?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     = 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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ) : esc_html__( 'AI Form Builder', 'wp-powersuite' ); ?></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', 'wp-powersuite' ),
								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', 'wp-powersuite' ); ?>">
					<span class="dashicons dashicons-trash" aria-hidden="true"></span>
					<span class="screen-reader-text"><?php esc_html_e( 'Clear chat', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>">
					<span class="dashicons dashicons-minus" aria-hidden="true"></span>
					<span class="screen-reader-text"><?php esc_html_e( 'Minimize', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?>
						<a href="<?php echo esc_url( $settings_url ); ?>"><?php esc_html_e( 'Basic Forms settings', 'wp-powersuite' ); ?></a>
						<?php esc_html_e( '·', 'wp-powersuite' ); ?>
						<a href="<?php echo esc_url( $connections_url ); ?>"><?php esc_html_e( 'AI Connections', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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…', 'wp-powersuite' ) : __( 'Describe the form you want to build…', 'wp-powersuite' ) ); ?>"
				<?php disabled( ! $has_api_key ); ?>
			></textarea>
			<div class="wpps-bf-ai-chat__composer-meta">
				<span class="description wpps-bf-ai-chat__char-count">
					<span id="wpps-bf-ai-prompt-count">0</span>
					<?php esc_html_e( '/ 8000 characters', 'wp-powersuite' ); ?>
				</span>
				<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', 'wp-powersuite' ); ?></span>
					<span class="wpps-bf-ai-send__loader" hidden><span class="wpps-bf-ai-spinner" aria-hidden="true"></span><?php esc_html_e( 'Sending…', 'wp-powersuite' ); ?></span>
				</button>
			</div>
		</div>
	</div>
</div>
