<?php
/**
 * Admin settings page.
 *
 * @package Maca_Tjatt
 */

defined( 'ABSPATH' ) || exit;

$maca_tjatt_enabled          = Maca_Tjatt_Service::is_enabled();
$maca_tjatt_floating         = '1' === (string) get_option( 'maca_tjatt_floating', '1' );
$maca_tjatt_index_all_pages  = Maca_Tjatt_Service::index_all_pages();
$maca_tjatt_index_posts      = Maca_Tjatt_Service::index_posts();
$maca_tjatt_chat_model       = Maca_Tjatt_Service::get_chat_model();
$maca_tjatt_rate_limit       = Maca_Tjatt_Service::get_rate_limit();
$maca_tjatt_ai_available     = Maca_Tjatt_Service::is_ai_available();
$maca_tjatt_connectors_url   = Maca_Tjatt_AI_Client::get_connectors_admin_url();
$maca_tjatt_index_status     = Maca_Tjatt_Service::get_index_status();
$maca_tjatt_chunk_count      = Maca_Tjatt_DB::count_chunks();
$maca_tjatt_selected_pages   = Maca_Tjatt_Service::get_page_ids();
$maca_tjatt_chat_name        = trim( (string) get_option( 'maca_tjatt_name', '' ) );
$maca_tjatt_custom_avatar_url = Maca_Tjatt_Service::get_custom_avatar_url();
$maca_tjatt_avatar_url        = Maca_Tjatt_Service::get_avatar_url();
$maca_tjatt_avatar_choice     = Maca_Tjatt_Service::get_avatar_choice_for_settings();
$maca_tjatt_bundled_avatars   = Maca_Tjatt_Service::get_bundled_avatars();
$maca_tjatt_avatar_size      = Maca_Tjatt_Service::get_avatar_size();
$maca_tjatt_avatar_sizes     = Maca_Tjatt_Service::get_avatar_size_options();
$maca_tjatt_avatar_bg_color  = Maca_Tjatt_Service::get_avatar_bg_color();
$maca_tjatt_avatar_dimensions = Maca_Tjatt_Service::get_avatar_dimensions_for_size( $maca_tjatt_avatar_size );
$maca_tjatt_avatar_size_class = Maca_Tjatt_Service::get_avatar_size_class_for_size( $maca_tjatt_avatar_size );
$maca_tjatt_avatar_ring_style = Maca_Tjatt_Service::get_avatar_ring_style( $maca_tjatt_avatar_bg_color );
$maca_tjatt_custom_knowledge = Maca_Tjatt_Service::get_custom_knowledge();
$maca_tjatt_all_pages        = get_pages( array( 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Maca Tjatt', 'maca-tjatt' ); ?></h1>
	<p><?php esc_html_e( 'AI chat for your website using the WordPress AI Client and a local search index. Configure your AI provider under Settings → Connectors.', 'maca-tjatt' ); ?></p>

	<?php if ( ! $maca_tjatt_ai_available ) : ?>
		<div class="notice notice-warning inline">
			<p>
				<?php
				printf(
					wp_kses(
						/* translators: %s: Settings → Connectors admin URL. */
						__( 'No AI provider is configured yet. Go to <a href="%s">Settings → Connectors</a> and connect a provider before enabling the chat.', 'maca-tjatt' ),
						array( 'a' => array( 'href' => array() ) )
					),
					esc_url( $maca_tjatt_connectors_url )
				);
				?>
			</p>
		</div>
	<?php endif; ?>

	<form method="post" action="">
		<?php wp_nonce_field( 'maca_tjatt_settings', 'maca_tjatt_nonce' ); ?>
		<input type="hidden" name="maca_tjatt_action" value="save_settings" />

		<h2><?php esc_html_e( 'General', 'maca-tjatt' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'Enable chat', 'maca-tjatt' ); ?></th>
				<td><label><input type="checkbox" name="maca_tjatt_enabled" value="1" <?php checked( $maca_tjatt_enabled ); ?> /> <?php esc_html_e( 'Show chat on the front end', 'maca-tjatt' ); ?></label></td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Floating widget', 'maca-tjatt' ); ?></th>
				<td><label><input type="checkbox" name="maca_tjatt_floating" value="1" <?php checked( $maca_tjatt_floating ); ?> /> <?php esc_html_e( 'Show floating chat button on all pages', 'maca-tjatt' ); ?></label></td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_tjatt_name"><?php esc_html_e( 'Assistant name', 'maca-tjatt' ); ?></label></th>
				<td><input name="maca_tjatt_name" id="maca_tjatt_name" type="text" class="regular-text" value="<?php echo esc_attr( $maca_tjatt_chat_name ); ?>" placeholder="<?php esc_attr_e( 'Website assistant', 'maca-tjatt' ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_tjatt_avatar"><?php esc_html_e( 'Avatar', 'maca-tjatt' ); ?></label></th>
				<td>
					<select name="maca_tjatt_avatar" id="maca_tjatt_avatar">
						<option value="default" data-url="<?php echo esc_url( Maca_Tjatt_Service::get_default_avatar_url() ); ?>" <?php selected( $maca_tjatt_avatar_choice, 'default' ); ?>>
							<?php esc_html_e( 'Default avatar', 'maca-tjatt' ); ?>
						</option>
						<?php foreach ( $maca_tjatt_bundled_avatars as $maca_tjatt_avatar_id => $maca_tjatt_avatar_data ) : ?>
							<option value="<?php echo esc_attr( $maca_tjatt_avatar_id ); ?>" data-url="<?php echo esc_url( $maca_tjatt_avatar_data['url'] ); ?>" <?php selected( $maca_tjatt_avatar_choice, $maca_tjatt_avatar_id ); ?>>
								<?php echo esc_html( $maca_tjatt_avatar_data['label'] ); ?>
							</option>
						<?php endforeach; ?>
						<option value="custom" data-url="<?php echo esc_attr( $maca_tjatt_custom_avatar_url ); ?>" <?php selected( $maca_tjatt_avatar_choice, 'custom' ); ?>>
							<?php esc_html_e( 'Custom image URL', 'maca-tjatt' ); ?>
						</option>
					</select>
					<p class="description"><?php esc_html_e( 'Choose a built-in avatar or provide your own image URL.', 'maca-tjatt' ); ?></p>
					<p>
						<label for="maca_tjatt_avatar_size"><strong><?php esc_html_e( 'Avatar size', 'maca-tjatt' ); ?></strong></label><br />
						<select name="maca_tjatt_avatar_size" id="maca_tjatt_avatar_size">
							<?php foreach ( $maca_tjatt_avatar_sizes as $maca_tjatt_size_key => $maca_tjatt_size_label ) : ?>
								<option value="<?php echo esc_attr( $maca_tjatt_size_key ); ?>" <?php selected( $maca_tjatt_avatar_size, $maca_tjatt_size_key ); ?>>
									<?php echo esc_html( $maca_tjatt_size_label ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</p>
					<div class="maca-tjatt maca-tjatt-avatar-preview-wrap <?php echo esc_attr( $maca_tjatt_avatar_size_class ); ?>" id="maca_tjatt_avatar_preview_wrap" style="<?php echo esc_attr( sprintf( '--maca-tjatt-toggle-bg:%s;--maca-tjatt-accent:%s;', $maca_tjatt_avatar_bg_color, $maca_tjatt_avatar_bg_color ) ); ?>">
						<p class="maca-tjatt-avatar-preview">
							<span class="maca-tjatt-avatar-preview-toggle" id="maca_tjatt_avatar_preview_toggle">
								<img id="maca_tjatt_avatar_preview" class="maca-tjatt-avatar-preview-image" src="<?php echo esc_url( Maca_Tjatt_Service::get_avatar_display_url() ); ?>" alt="" width="<?php echo esc_attr( (string) $maca_tjatt_avatar_dimensions['toggle'] ); ?>" height="<?php echo esc_attr( (string) $maca_tjatt_avatar_dimensions['toggle'] ); ?>" style="<?php echo esc_attr( $maca_tjatt_avatar_ring_style ); ?>" />
							</span>
						</p>
					</div>
					<p class="description"><?php esc_html_e( 'Preview shows the floating chat button at the selected size.', 'maca-tjatt' ); ?></p>
					<p>
						<label for="maca_tjatt_avatar_bg_color"><strong><?php esc_html_e( 'Avatar ring color', 'maca-tjatt' ); ?></strong></label><br />
						<input type="hidden" name="maca_tjatt_avatar_bg_color" id="maca_tjatt_avatar_bg_color_hidden" value="<?php echo esc_attr( $maca_tjatt_avatar_bg_color ); ?>" />
						<input type="text" id="maca_tjatt_avatar_bg_color" class="maca-tjatt-color-field" value="<?php echo esc_attr( $maca_tjatt_avatar_bg_color ); ?>" data-default-color="<?php echo esc_attr( Maca_Tjatt_Service::get_default_avatar_bg_color() ); ?>" autocomplete="off" />
					</p>
					<p class="description"><?php esc_html_e( 'Accent color for the avatar ring and chat box (header, send button, links, and user messages). The preview above updates as you pick a color.', 'maca-tjatt' ); ?></p>
					<div class="maca-tjatt-avatar-custom" id="maca_tjatt_avatar_custom" <?php echo 'custom' === $maca_tjatt_avatar_choice ? '' : 'hidden'; ?>>
						<label for="maca_tjatt_avatar_url"><?php esc_html_e( 'Custom image URL', 'maca-tjatt' ); ?></label><br />
						<input name="maca_tjatt_avatar_url" id="maca_tjatt_avatar_url" type="url" class="regular-text" value="<?php echo esc_attr( $maca_tjatt_custom_avatar_url ); ?>" />
					</div>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'AI provider', 'maca-tjatt' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'Provider setup', 'maca-tjatt' ); ?></th>
				<td>
					<p>
						<a href="<?php echo esc_url( $maca_tjatt_connectors_url ); ?>"><?php esc_html_e( 'Open Settings → Connectors', 'maca-tjatt' ); ?></a>
					</p>
					<p class="description"><?php esc_html_e( 'Maca Tjatt uses the WordPress AI Client. Credentials are managed by WordPress, not in this plugin.', 'maca-tjatt' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_tjatt_chat_model"><?php esc_html_e( 'Preferred chat model', 'maca-tjatt' ); ?></label></th>
				<td>
					<input name="maca_tjatt_chat_model" id="maca_tjatt_chat_model" type="text" class="regular-text" value="<?php echo esc_attr( $maca_tjatt_chat_model ); ?>" placeholder="<?php esc_attr_e( 'gpt-4o-mini', 'maca-tjatt' ); ?>" />
					<p class="description"><?php esc_html_e( 'Optional model slug preference. WordPress uses the first available matching model from your connected provider.', 'maca-tjatt' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_tjatt_rate_limit"><?php esc_html_e( 'Rate limit', 'maca-tjatt' ); ?></label></th>
				<td><input name="maca_tjatt_rate_limit" id="maca_tjatt_rate_limit" type="number" min="5" max="200" value="<?php echo esc_attr( (string) $maca_tjatt_rate_limit ); ?>" /> <?php esc_html_e( 'questions per hour per visitor', 'maca-tjatt' ); ?></td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Knowledge index', 'maca-tjatt' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'Index all pages', 'maca-tjatt' ); ?></th>
				<td>
					<label><input type="checkbox" name="maca_tjatt_index_all_pages" value="1" <?php checked( $maca_tjatt_index_all_pages ); ?> /> <?php esc_html_e( 'Index all published pages (no limit)', 'maca-tjatt' ); ?></label>
					<p class="description"><?php esc_html_e( 'If disabled, only selected pages below are indexed.', 'maca-tjatt' ); ?></p>
					<p class="description"><?php esc_html_e( 'Also indexes categories, tags, and all public custom post types. Long content is split into multiple search chunks.', 'maca-tjatt' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Index blog posts', 'maca-tjatt' ); ?></th>
				<td><label><input type="checkbox" name="maca_tjatt_index_posts" value="1" <?php checked( $maca_tjatt_index_posts ); ?> /> <?php esc_html_e( 'Include all published posts', 'maca-tjatt' ); ?></label></td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Selected pages', 'maca-tjatt' ); ?></th>
				<td>
					<select name="maca_tjatt_page_ids[]" multiple size="8" style="min-width:280px;">
						<?php foreach ( $maca_tjatt_all_pages as $maca_tjatt_page ) : ?>
							<option value="<?php echo esc_attr( (string) $maca_tjatt_page->ID ); ?>" <?php selected( in_array( (int) $maca_tjatt_page->ID, $maca_tjatt_selected_pages, true ) ); ?>>
								<?php echo esc_html( $maca_tjatt_page->post_title ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Optional extra pages when «Index all pages» is off, or to ensure specific pages are included.', 'maca-tjatt' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="maca_tjatt_custom_knowledge"><?php esc_html_e( 'Extra knowledge', 'maca-tjatt' ); ?></label></th>
				<td>
					<textarea name="maca_tjatt_custom_knowledge" id="maca_tjatt_custom_knowledge" class="large-text" rows="6"><?php echo esc_textarea( $maca_tjatt_custom_knowledge ); ?></textarea>
					<p class="description"><?php esc_html_e( 'Free text always included in the index (services, FAQ, contact info, etc.).', 'maca-tjatt' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Index status', 'maca-tjatt' ); ?></th>
				<td>
					<p><strong><?php echo esc_html( (string) $maca_tjatt_chunk_count ); ?></strong> <?php esc_html_e( 'indexed chunks', 'maca-tjatt' ); ?></p>
					<?php if ( ! empty( $maca_tjatt_index_status['message'] ) ) : ?>
						<p><?php echo esc_html( (string) $maca_tjatt_index_status['message'] ); ?></p>
					<?php endif; ?>
					<?php if ( ! empty( $maca_tjatt_index_status['built_at'] ) ) : ?>
						<p class="description"><?php echo esc_html( (string) $maca_tjatt_index_status['built_at'] ); ?></p>
					<?php endif; ?>
				</td>
			</tr>
		</table>

		<?php submit_button( __( 'Save settings', 'maca-tjatt' ) ); ?>
	</form>

	<form method="post" action="" style="margin-top:1em;">
		<?php wp_nonce_field( 'maca_tjatt_settings', 'maca_tjatt_nonce' ); ?>
		<input type="hidden" name="maca_tjatt_action" value="build_index" />
		<?php submit_button( __( 'Build search index now', 'maca-tjatt' ), 'secondary' ); ?>
	</form>

	<h2><?php esc_html_e( 'Shortcode', 'maca-tjatt' ); ?></h2>
	<p><code>[maca_tjatt]</code> — <?php esc_html_e( 'Embed the chat inline on any page.', 'maca-tjatt' ); ?></p>

	<h2><?php esc_html_e( 'Test chat', 'maca-tjatt' ); ?></h2>
	<?php if ( $maca_tjatt_chunk_count <= 0 ) : ?>
		<p><?php esc_html_e( 'Build the search index before testing.', 'maca-tjatt' ); ?></p>
	<?php else : ?>
		<div class="maca-tjatt maca-tjatt--admin-test" data-maca-tjatt-admin="1" style="<?php echo esc_attr( sprintf( '--maca-tjatt-toggle-bg:%s;--maca-tjatt-accent:%s;', $maca_tjatt_avatar_bg_color, $maca_tjatt_avatar_bg_color ) ); ?>">
			<div class="maca-tjatt-panel">
				<div class="maca-tjatt-header">
					<div class="maca-tjatt-header-main">
						<img class="maca-tjatt-avatar" src="<?php echo esc_url( Maca_Tjatt_Service::get_avatar_url() ); ?>" alt="" width="44" height="44">
						<strong class="maca-tjatt-title"><?php echo esc_html( Maca_Tjatt_Service::get_display_name() ); ?></strong>
					</div>
				</div>
				<div class="maca-tjatt-messages" role="log" aria-live="polite"></div>
				<form class="maca-tjatt-form">
					<label class="screen-reader-text" for="maca-tjatt-admin-input"><?php esc_html_e( 'Test question', 'maca-tjatt' ); ?></label>
					<textarea id="maca-tjatt-admin-input" class="maca-tjatt-input" rows="2" placeholder="<?php esc_attr_e( 'Ask a test question…', 'maca-tjatt' ); ?>"></textarea>
					<button type="submit" class="maca-tjatt-send"><?php esc_html_e( 'Send', 'maca-tjatt' ); ?></button>
				</form>
			</div>
		</div>
	<?php endif; ?>
</div>
