<?php
/**
 * Provides the admin area view for the plugin's main settings page.
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://trynbuy.edubrazeal.com/
 * @since      1.0.0
 *
 * @package    Ai_Try_N_Buy
 * @subpackage Ai_Try_N_Buy/admin/views
 */

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

if ( ! current_user_can( 'manage_options' ) ) {
	wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'virtual-ai-fit-and-style-assistant' ) );
}

$options = get_option( 'aitnb_settings' );

$available_models = get_transient( 'aitnb_gemini_models_list' );
if ( false === $available_models ) {
	$available_models = array();
}

?>
<div class="wrap">

	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

	<form method="post" action="options.php" enctype="multipart/form-data">

		<?php
		settings_fields( 'aitnb_option_group' );
		?>

		<table class="form-table">
			<tbody>
				<tr valign="top">
					<th scope="row">
						<?php esc_html_e( 'API Data Consent', 'virtual-ai-fit-and-style-assistant' ); ?>
					</th>
					<td>
						<fieldset>
							<label for="aitnb_api_data_consent">
								<input type="checkbox" id="aitnb_api_data_consent" name="aitnb_settings[api_data_consent]" value="1" <?php checked( isset( $options['api_data_consent'] ) ? $options['api_data_consent'] : 0, 1 ); ?> />
								<strong><?php esc_html_e( 'I consent to sending product and user data to third-party APIs (Google) to enable AI features.', 'virtual-ai-fit-and-style-assistant' ); ?></strong>
							</label>
							<p class="description">
								<?php esc_html_e( 'To function, this plugin sends data such as product details, user measurements, and images to external services like the Google Gemini and Vision APIs. You must agree to this data transmission to use the AI features. This consent is required.', 'virtual-ai-fit-and-style-assistant' ); ?>
							</p>
						</fieldset>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_google_api_key"><?php esc_html_e( 'Google Cloud Vision API Key', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<input type="password" id="aitnb_google_api_key" name="aitnb_settings[google_api_key]" value="<?php echo isset( $options['google_api_key'] ) ? esc_attr( $options['google_api_key'] ) : ''; ?>" class="regular-text" autocomplete="off" />
						<p class="description">
							<?php
							echo wp_kses(
								__( 'Enter your API key for the Google Cloud Vision service. Get your key from the <a href="https://console.cloud.google.com/apis/credentials" target="_blank" rel="noopener noreferrer">Google Cloud Console</a>.', 'virtual-ai-fit-and-style-assistant' ),
								array(
									'a' => array(
										'href'   => array(),
										'target' => array(),
										'rel'    => array(),
									),
								)
							);
							?>
						</p>
						<div>
							<button type="button" class="button aitnb-test-api-button" data-api="google_vision"><?php esc_html_e( 'Test Connection', 'virtual-ai-fit-and-style-assistant' ); ?></button>
							<span class="spinner"></span>
							<span class="aitnb-api-test-result"></span>
						</div>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_gemini_api_key"><?php esc_html_e( 'Gemini API Key', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<input type="password" id="aitnb_gemini_api_key" name="aitnb_settings[gemini_api_key]" value="<?php echo isset( $options['gemini_api_key'] ) ? esc_attr( $options['gemini_api_key'] ) : ''; ?>" class="regular-text" autocomplete="off" />
						<p class="description">
							<?php
							echo wp_kses(
								__( 'Enter your API key from <a href="https://aistudio.google.com/app/apikey" target="_blank" rel="noopener noreferrer">Google AI Studio</a>. To get a key, log in and click "Get API key".', 'virtual-ai-fit-and-style-assistant' ),
								array(
									'a' => array(
										'href'   => array(),
										'target' => array(),
										'rel'    => array(),
									),
								)
							);
							?>
						</p>
						<div>
							<button type="button" class="button aitnb-test-api-button" data-api="gemini"><?php esc_html_e( 'Test Connection', 'virtual-ai-fit-and-style-assistant' ); ?></button>
							<span class="spinner"></span>
							<span class="aitnb-api-test-result"></span>
						</div>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<?php esc_html_e( 'Gemini Models', 'virtual-ai-fit-and-style-assistant' ); ?>
					</th>
					<td>
						<button type="button" class="button" id="aitnb-fetch-models-btn"><?php esc_html_e( 'Fetch Available Models', 'virtual-ai-fit-and-style-assistant' ); ?></button>
						<span class="spinner" id="aitnb-fetch-spinner"></span>
						<span id="aitnb-fetch-result" class="aitnb-api-test-result"></span>
						<p class="description"><?php esc_html_e( 'Click this button after entering your Gemini API key to populate the model lists below.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_gemini_fit_model"><?php esc_html_e( 'Fit Recommendation Model', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<select id="aitnb_gemini_fit_model" name="aitnb_settings[gemini_fit_model]">
							<?php if ( empty( $available_models ) && isset( $options['gemini_fit_model'] ) && ! empty( $options['gemini_fit_model'] ) ) : ?>
								<option value="<?php echo esc_attr( $options['gemini_fit_model'] ); ?>" selected><?php echo esc_html( $options['gemini_fit_model'] ); ?></option>
							<?php else : ?>
								<option value=""><?php esc_html_e( '-- Select a Model --', 'virtual-ai-fit-and-style-assistant' ); ?></option>
								<?php foreach ( $available_models as $model ) : ?>
									<option value="<?php echo esc_attr( $model['id'] ); ?>" <?php selected( isset( $options['gemini_fit_model'] ) ? $options['gemini_fit_model'] : '', $model['id'] ); ?>>
										<?php echo esc_html( $model['name'] ); ?>
									</option>
								<?php endforeach; ?>
							<?php endif; ?>
						</select>
						<p class="description"><?php esc_html_e( 'Select the model for the "Find My Size" feature. We recommend a "Flash" model for speed.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_gemini_style_model"><?php esc_html_e( 'Style Assistant Model', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<select id="aitnb_gemini_style_model" name="aitnb_settings[gemini_style_model]">
							<?php if ( empty( $available_models ) && isset( $options['gemini_style_model'] ) && ! empty( $options['gemini_style_model'] ) ) : ?>
								<option value="<?php echo esc_attr( $options['gemini_style_model'] ); ?>" selected><?php echo esc_html( $options['gemini_style_model'] ); ?></option>
							<?php else : ?>
								<option value=""><?php esc_html_e( '-- Select a Model --', 'virtual-ai-fit-and-style-assistant' ); ?></option>
								<?php foreach ( $available_models as $model ) : ?>
									<option value="<?php echo esc_attr( $model['id'] ); ?>" <?php selected( isset( $options['gemini_style_model'] ) ? $options['gemini_style_model'] : '', $model['id'] ); ?>>
										<?php echo esc_html( $model['name'] ); ?>
									</option>
								<?php endforeach; ?>
							<?php endif; ?>
						</select>
						<p class="description"><?php esc_html_e( 'Select the model for the "Complete The Look" feature. We recommend a "Pro" model for quality.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_style_assistant_frequency"><?php esc_html_e( 'Style Assistant Update Frequency', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<select id="aitnb_style_assistant_frequency" name="aitnb_settings[style_assistant_frequency]">
							<?php
							$frequencies = array(
								'daily'      => __( 'Daily', 'virtual-ai-fit-and-style-assistant' ),
								'twicedaily' => __( 'Twice Daily', 'virtual-ai-fit-and-style-assistant' ),
								'weekly'     => __( 'Weekly', 'virtual-ai-fit-and-style-assistant' ),
								'twoweekly'  => __( 'Every Two Weeks', 'virtual-ai-fit-and-style-assistant' ),
								'monthly'    => __( 'Monthly', 'virtual-ai-fit-and-style-assistant' ),
								'manual'     => __( 'Manual Only', 'virtual-ai-fit-and-style-assistant' ),
							);
							$current_freq = isset( $options['style_assistant_frequency'] ) ? $options['style_assistant_frequency'] : 'daily';
							foreach ( $frequencies as $value => $label ) {
								echo '<option value="' . esc_attr( $value ) . '" ' . selected( $current_freq, $value, false ) . '>' . esc_html( $label ) . '</option>';
							}
							?>
						</select>
						<p class="description"><?php esc_html_e( 'Select how often the AI should generate and cache new style recommendations for all products. "Manual Only" prevents automatic runs.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<label for="aitnb_product_page_hook"><?php esc_html_e( '"Complete The Look" Display Location', 'virtual-ai-fit-and-style-assistant' ); ?></label>
					</th>
					<td>
						<select id="aitnb_product_page_hook" name="aitnb_settings[product_page_hook]">
							<?php
							$hooks = array(
								'woocommerce_after_single_product_summary' => __( 'After Product Summary (Recommended)', 'virtual-ai-fit-and-style-assistant' ),
								'woocommerce_product_meta_end'             => __( 'Inside Product Meta Section', 'virtual-ai-fit-and-style-assistant' ),
								'woocommerce_after_single_product'         => __( 'After Main Product Content', 'virtual-ai-fit-and-style-assistant' ),
							);
							$current_hook = isset( $options['product_page_hook'] ) ? $options['product_page_hook'] : 'woocommerce_after_single_product_summary';
							foreach ( $hooks as $value => $label ) {
								echo '<option value="' . esc_attr( $value ) . '" ' . selected( $current_hook, $value, false ) . '>' . esc_html( $label ) . '</option>';
							}
							?>
						</select>
						<p class="description"><?php esc_html_e( 'If the "Complete The Look" section isn\'t appearing, your theme may not support the selected location. Try a different option.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
					</td>
				</tr>

				<tr valign="top">
					<th scope="row">
						<?php esc_html_e( 'Data Handling', 'virtual-ai-fit-and-style-assistant' ); ?>
					</th>
					<td>
						<fieldset>
							<label for="aitnb_delete_data_on_uninstall">
								<input type="checkbox" id="aitnb_delete_data_on_uninstall" name="aitnb_settings[delete_data_on_uninstall]" value="1" <?php checked( isset( $options['delete_data_on_uninstall'] ) ? $options['delete_data_on_uninstall'] : 0, 1 ); ?> />
								<?php esc_html_e( 'Delete all plugin data (analytics table and settings) upon uninstallation.', 'virtual-ai-fit-and-style-assistant' ); ?>
							</label>
							<p class="description">
								<?php esc_html_e( 'Check this box if you want the plugin to completely remove all of its data when you delete it from the plugins screen. This action cannot be undone.', 'virtual-ai-fit-and-style-assistant' ); ?>
							</p>
						</fieldset>
					</td>
				</tr>
				
				<tr>
					<th colspan="2">
						<h2 class="aitnb-settings-section-header"><?php esc_html_e( 'Export / Import Settings', 'virtual-ai-fit-and-style-assistant' ); ?></h2>
					</th>
				</tr>

				<tr valign="top">
					<th scope="row">
						<?php esc_html_e( 'Export', 'virtual-ai-fit-and-style-assistant' ); ?>
					</th>
					<td>
						<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=aitnb-main&aitnb_action=export_settings' ), 'aitnb_export_action', 'aitnb_export_nonce' ) ); ?>" class="button">
							<?php esc_html_e( 'Export Settings', 'virtual-ai-fit-and-style-assistant' ); ?>
						</a>
						<p class="description">
							<?php esc_html_e( 'Click this button to download a JSON file of your current plugin settings.', 'virtual-ai-fit-and-style-assistant' ); ?>
						</p>
					</td>
				</tr>
				
				<tr valign="top">
					<th scope="row">
						<?php esc_html_e( 'Import', 'virtual-ai-fit-and-style-assistant' ); ?>
					</th>
					<td>
						<?php wp_nonce_field( 'aitnb_import_action', 'aitnb_import_nonce' ); ?>
						<input type="file" name="aitnb_import_file" id="aitnb_import_file" accept=".json" />
						<input type="submit" name="aitnb_import_settings" class="button" value="<?php esc_attr_e( 'Import Settings', 'virtual-ai-fit-and-style-assistant' ); ?>" />
						<p class="description">
							<?php esc_html_e( 'Select a JSON file to import settings. This will overwrite your current settings.', 'virtual-ai-fit-and-style-assistant' ); ?>
						</p>
					</td>
				</tr>

			</tbody>
		</table>

		<?php submit_button(); ?>

	</form>

</div>