<?php
/**
 * Settings page view for Voice Logica AI for WooCommerce plugin
 *
 * @package VoiceLogica_AI_WooCommerce
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$voicelogica_ai_logo_url = isset( $logo_url ) ? $logo_url : '';
?>

<div class="wrap voicelogica-settings">
	<?php settings_errors( 'voicelogica_ai' ); ?>
	<header class="voicelogica-header">
		<?php if ( ! empty( $voicelogica_ai_logo_url ) ) : ?>
			<a href="<?php echo esc_url( 'https://www.voicelogica.ai/' ); ?>" target="_blank" rel="noopener noreferrer" class="voicelogica-header__logo-link">
				<img src="<?php echo esc_url( $voicelogica_ai_logo_url ); ?>" alt="<?php esc_attr_e( 'Voice Logica', 'voicelogica-ai-for-woocommerce' ); ?>" class="voicelogica-header__logo" />
			</a>
		<?php else : ?>
			<h1 class="voicelogica-header__title"><?php esc_html_e( 'Voice Logica AI', 'voicelogica-ai-for-woocommerce' ); ?></h1>
		<?php endif; ?>
		<div class="voicelogica-header__brand">
			<p class="voicelogica-header__tagline"><?php esc_html_e( 'AI Voice Agents for your WooCommerce store', 'voicelogica-ai-for-woocommerce' ); ?></p>
		</div>
	</header>

	<?php if ( ! $connected ) : ?>
		<div class="voicelogica-connect-section">
			<h3><?php esc_html_e( 'Transform Your Customer Experience with AI Voice Agents', 'voicelogica-ai-for-woocommerce' ); ?></h3>
			<p><?php esc_html_e( 'Voice Logica AI brings intelligent voice-powered assistance to your WooCommerce store, enabling your customers to interact naturally through voice commands. Our AI Voice Agents understand natural language, answer questions instantly, and are available 24/7—helping you provide exceptional customer service without increasing your support team.', 'voicelogica-ai-for-woocommerce' ); ?></p>
			<p><strong><?php esc_html_e( 'What Voice Logica AI can do for your store:', 'voicelogica-ai-for-woocommerce' ); ?></strong></p>
			<ul class="voicelogica-features-list">
				<li><?php esc_html_e( 'Enable customers to search products by name or code using voice commands', 'voicelogica-ai-for-woocommerce' ); ?></li>
				<li><?php esc_html_e( 'Retrieve and view customer order history instantly', 'voicelogica-ai-for-woocommerce' ); ?></li>
				<li><?php esc_html_e( 'Create new orders through natural voice conversations', 'voicelogica-ai-for-woocommerce' ); ?></li>
				<li><?php esc_html_e( 'Find existing customers or automatically create new accounts', 'voicelogica-ai-for-woocommerce' ); ?></li>
				<li><?php esc_html_e( 'Provide detailed order information by order ID', 'voicelogica-ai-for-woocommerce' ); ?></li>
			</ul>
			<p><?php esc_html_e( 'Connect your store now to start offering voice-powered shopping experiences that delight your customers and streamline your operations.', 'voicelogica-ai-for-woocommerce' ); ?></p>
			<form method="post" action="">
				<?php wp_nonce_field( 'voicelogica_ai_connect', 'voicelogica_ai_nonce' ); ?>
				<p class="submit">
					<button type="submit" name="voicelogica_ai_connect" class="button button-primary">
						<?php esc_html_e( 'Connect to Voice Logica AI', 'voicelogica-ai-for-woocommerce' ); ?>
					</button>
				</p>
			</form>
		</div>
	<?php else : ?>
		<p><?php esc_html_e( 'Your store is connected to Voice Logica AI. Enable the features you want to use below.', 'voicelogica-ai-for-woocommerce' ); ?></p>

		<form method="post" action="">
			<?php wp_nonce_field( 'voicelogica_ai_save_features', 'voicelogica_ai_features_nonce' ); ?>
			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row"><?php esc_html_e( 'View Customer Order History', 'voicelogica-ai-for-woocommerce' ); ?></th>
						<td>
							<label>
								<input type="checkbox"
									name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FEATURE_GET_CUSTOMER_ORDERS ); ?>]"
									value="1"
									<?php checked( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_GET_CUSTOMER_ORDERS ] ) ); ?>
								/>
								<?php esc_html_e( 'Enable Voice Logica AI to retrieve and view all orders placed by customers', 'voicelogica-ai-for-woocommerce' ); ?>
							</label>
						</td>
					</tr>
	
					<tr>
						<th scope="row"><?php esc_html_e( 'Search Products by Product Name', 'voicelogica-ai-for-woocommerce' ); ?></th>
						<td>
							<label>
								<input type="checkbox"
									name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FEATURE_GET_PRODUCT_BY_NAME ); ?>]"
									value="1"
									<?php checked( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_GET_PRODUCT_BY_NAME ] ) ); ?>
								/>
								<?php esc_html_e( 'Enable Voice Logica AI to search and find products in your store by their product name', 'voicelogica-ai-for-woocommerce' ); ?>
							</label>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Search Products by Product Code', 'voicelogica-ai-for-woocommerce' ); ?></th>
						<td>
							<label>
								<input type="checkbox"
									name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FEATURE_GET_PRODUCT_BY_CODE ); ?>]"
									value="1"
									id="voicelogica-get-product-by-code-checkbox"
									<?php checked( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_GET_PRODUCT_BY_CODE ] ) ); ?>
								/>
								<?php esc_html_e( 'Enable Voice Logica AI to search and find products in your store using product codes (ID, SKU, or Global Unique ID)', 'voicelogica-ai-for-woocommerce' ); ?>
							</label>
						</td>
					</tr>
					<?php
					$voicelogica_ai_product_code_type_enabled = ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_GET_PRODUCT_BY_CODE ] );
					$voicelogica_ai_product_code_type_value   = $features[ VoiceLogica_AI_Settings::FIELD_PRODUCT_CODE_TYPE ];
					// When get_product_by_code is enabled, default to 'sku' if empty (user cannot leave empty).
					$voicelogica_ai_product_code_type_display = ( $voicelogica_ai_product_code_type_enabled && '' === $voicelogica_ai_product_code_type_value ) ? 'sku' : $voicelogica_ai_product_code_type_value;
					?>
					<tr id="voicelogica-code-type-row" style="display: <?php echo esc_attr( $voicelogica_ai_product_code_type_enabled ? 'table-row' : 'none' ); ?>;">
						<th scope="row">
							<label for="voicelogica-code-type"><?php esc_html_e( 'Product Code Type', 'voicelogica-ai-for-woocommerce' ); ?></label>
						</th>
						<td>
							<select id="voicelogica-code-type"
								name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FIELD_PRODUCT_CODE_TYPE ); ?>]"
								class="regular-text"
								<?php echo esc_attr( $voicelogica_ai_product_code_type_enabled ? ' required' : '' ); ?>>
								<?php if ( ! $voicelogica_ai_product_code_type_enabled ) : ?>
									<option value=""><?php esc_html_e( 'Select code type...', 'voicelogica-ai-for-woocommerce' ); ?></option>
								<?php endif; ?>
								<option value="id" <?php selected( $voicelogica_ai_product_code_type_display, 'id' ); ?>><?php esc_html_e( 'Product ID', 'voicelogica-ai-for-woocommerce' ); ?></option>
								<option value="sku" <?php selected( $voicelogica_ai_product_code_type_display, 'sku' ); ?>><?php esc_html_e( 'SKU (Stock Keeping Unit)', 'voicelogica-ai-for-woocommerce' ); ?></option>
								<option value="global_unique_id" <?php selected( $voicelogica_ai_product_code_type_display, 'global_unique_id' ); ?>><?php esc_html_e( 'Global Unique ID', 'voicelogica-ai-for-woocommerce' ); ?></option>
							</select>
							<p class="description"><?php esc_html_e( 'Choose which type of product identifier Voice Logica AI should use when searching for products. This determines how products are identified and retrieved.', 'voicelogica-ai-for-woocommerce' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Create New Orders', 'voicelogica-ai-for-woocommerce' ); ?></th>
						<td>
							<label>
								<input type="checkbox"
									name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FEATURE_CREATE_ORDER ); ?>]"
									value="1"
									id="voicelogica-create-order-checkbox"
									<?php checked( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_CREATE_ORDER ] ) ); ?>
								/>
								<?php esc_html_e( 'Enable Voice Logica AI to create new orders in your WooCommerce store through voice commands', 'voicelogica-ai-for-woocommerce' ); ?>
							</label>
						</td>
					</tr>
					<tr id="voicelogica-order-emails-row" style="display: <?php echo esc_attr( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_CREATE_ORDER ] ) ? 'table-row' : 'none' ); ?>;">
						<th scope="row">
							<label for="voicelogica-order-emails"><?php esc_html_e( 'Order Notification Email Addresses', 'voicelogica-ai-for-woocommerce' ); ?></label>
						</th>
						<td>
							<input type="text"
								id="voicelogica-order-emails"
								name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FIELD_NOTIFY_EMAILS_ON_ORDER_CREATION ); ?>]"
								value="<?php echo esc_attr( implode( ', ', $features[ VoiceLogica_AI_Settings::FIELD_NOTIFY_EMAILS_ON_ORDER_CREATION ] ) ); ?>"
								class="regular-text"
								placeholder="<?php esc_attr_e( 'example@email.com, another@email.com', 'voicelogica-ai-for-woocommerce' ); ?>"
							/>
							<p class="description"><?php esc_html_e( 'Enter email addresses (separated by commas) that should receive email notifications whenever Voice Logica AI creates a new order. Leave empty if you don\'t want email notifications.', 'voicelogica-ai-for-woocommerce' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Retrieve Order Details by Order ID', 'voicelogica-ai-for-woocommerce' ); ?></th>
						<td>
							<label>
								<input type="checkbox"
									name="features[<?php echo esc_attr( VoiceLogica_AI_Settings::FEATURE_GET_ORDER_BY_ID ); ?>]"
									value="1"
									<?php checked( ! empty( $features[ VoiceLogica_AI_Settings::FEATURE_GET_ORDER_BY_ID ] ) ); ?>
								/>
								<?php esc_html_e( 'Enable Voice Logica AI to retrieve and view detailed information about specific orders using their order ID', 'voicelogica-ai-for-woocommerce' ); ?>
							</label>
						</td>
					</tr>
				</tbody>
			</table>

			<p class="submit">
				<button type="submit" name="voicelogica_ai_save_features" class="button button-primary">
					<?php esc_html_e( 'Save Settings', 'voicelogica-ai-for-woocommerce' ); ?>
				</button>
			</p>
		</form>

		<div class="voicelogica-disconnect-section">
			<h2><?php esc_html_e( 'Disconnect', 'voicelogica-ai-for-woocommerce' ); ?></h2>
			<p><?php esc_html_e( 'Disconnect your store from Voice Logica AI. This will remove all connection data and API keys.', 'voicelogica-ai-for-woocommerce' ); ?></p>
			<form method="post" action="" onsubmit="return confirm('<?php echo esc_js( __( 'Are you sure you want to disconnect? This will remove all connection data and API keys.', 'voicelogica-ai-for-woocommerce' ) ); ?>');">
				<?php wp_nonce_field( 'voicelogica_ai_disconnect', 'voicelogica_ai_disconnect_nonce' ); ?>
				<p class="submit">
					<button type="submit" name="voicelogica_ai_disconnect" class="button button-link-delete">
						<?php esc_html_e( 'Disconnect from Voice Logica AI', 'voicelogica-ai-for-woocommerce' ); ?>
					</button>
				</p>
			</form>
		</div>
	<?php endif; ?>
</div>
