<?php
/**
 * Settings page template.
 *
 * @package BOOSAIAS_For_WooCommerce
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap">
	<h1><?php echo esc_html__( 'Boostanova AI Settings', 'boostanova-ai-shop-assistant' ); ?></h1>

	<?php settings_errors( 'boosaias_settings' ); ?>

	<?php if ( isset( $_GET['synced'] ) && '1' === $_GET['synced'] ) : // phpcs:ignore WordPress.Security.NonceVerification ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Catalog sync started.', 'boostanova-ai-shop-assistant' ); ?></p></div>
	<?php endif; ?>

	<form method="post" action="options.php">
		<?php
		settings_fields( 'boosaias_settings' );
		?>

		<table class="form-table" role="presentation">
			<!-- License key field: automatically set on plugin activation. Hidden for free tier, will be exposed in Pro plan. -->
			<tr>
				<th scope="row">
					<label for="boosaias_language"><?php echo esc_html__( 'Language', 'boostanova-ai-shop-assistant' ); ?></label>
				</th>
				<td>
					<select id="boosaias_language" name="boosaias_language">
						<?php
						$boosaias_current = get_option( 'boosaias_language', 'uk' );
						$boosaias_langs   = array(
							'uk' => __( 'Ukrainian', 'boostanova-ai-shop-assistant' ),
							'ru' => __( 'Russian', 'boostanova-ai-shop-assistant' ),
							'en' => __( 'English', 'boostanova-ai-shop-assistant' ),
						);
						foreach ( $boosaias_langs as $boosaias_code => $boosaias_label ) {
							printf(
								'<option value="%s"%s>%s</option>',
								esc_attr( $boosaias_code ),
								selected( $boosaias_current, $boosaias_code, false ),
								esc_html( $boosaias_label )
							);
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="boosaias_telegram_chat_id"><?php echo esc_html__( 'Telegram Chat ID', 'boostanova-ai-shop-assistant' ); ?></label>
				</th>
				<td>
					<input
						type="text"
						id="boosaias_telegram_chat_id"
						name="boosaias_telegram_chat_id"
						value="<?php echo esc_attr( get_option( 'boosaias_telegram_chat_id', '' ) ); ?>"
						class="regular-text"
					/>
					<p class="description"><?php echo esc_html__( 'Leads will be sent to this Telegram chat.', 'boostanova-ai-shop-assistant' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="boosaias_welcome_message"><?php echo esc_html__( 'Welcome Message', 'boostanova-ai-shop-assistant' ); ?></label>
				</th>
				<td>
					<input
						type="text"
						id="boosaias_welcome_message"
						name="boosaias_welcome_message"
						value="<?php echo esc_attr( get_option( 'boosaias_welcome_message', '' ) ); ?>"
						class="regular-text"
						placeholder="<?php echo esc_attr__( 'Hello! How can I help?', 'boostanova-ai-shop-assistant' ); ?>"
					/>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="boosaias_store_info"><?php echo esc_html__( 'Store Information', 'boostanova-ai-shop-assistant' ); ?></label>
				</th>
				<td>
					<textarea
						id="boosaias_store_info"
						name="boosaias_store_info"
						rows="7"
						class="large-text"
						placeholder="<?php echo esc_attr__( 'Delivery: Nova Poshta 1-3 days... Payment: card, cash on delivery... Returns: 14 days...', 'boostanova-ai-shop-assistant' ); ?>"
					><?php echo esc_textarea( get_option( 'boosaias_store_info', '' ) ); ?></textarea>
					<p class="description"><?php echo esc_html__( 'Delivery, payment, returns, working hours. The assistant will use this to answer general questions about your store.', 'boostanova-ai-shop-assistant' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="boosaias_widget_color"><?php echo esc_html__( 'Widget Color', 'boostanova-ai-shop-assistant' ); ?></label>
				</th>
				<td>
					<input
						type="color"
						id="boosaias_widget_color"
						name="boosaias_widget_color"
						value="<?php echo esc_attr( get_option( 'boosaias_widget_color', '#2271b1' ) ); ?>"
					/>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<?php esc_html_e( 'Widget Branding', 'boostanova-ai-shop-assistant' ); ?>
				</th>
				<td>
					<label>
						<input
							type="checkbox"
							name="boosaias_show_branding"
							value="1"
							<?php checked( 1, get_option( 'boosaias_show_branding', 0 ) ); ?>
						/>
						<?php esc_html_e( 'Show "Powered by Boostanova" link in widget footer', 'boostanova-ai-shop-assistant' ); ?>
					</label>
					<p class="description"><?php esc_html_e( 'Uncheck to hide the Boostanova branding link from the chat widget.', 'boostanova-ai-shop-assistant' ); ?></p>
				</td>
			</tr>
		</table>

		<?php submit_button(); ?>
	</form>

	<hr>
	<h2><?php esc_html_e( 'Catalog Sync', 'boostanova-ai-shop-assistant' ); ?></h2>
	<p><?php esc_html_e( 'Send all WooCommerce products to the AI backend. Runs automatically once a day.', 'boostanova-ai-shop-assistant' ); ?></p>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="boosaias_sync_now">
		<?php wp_nonce_field( 'boosaias_sync_now' ); ?>
		<?php submit_button( __( 'Sync Now', 'boostanova-ai-shop-assistant' ), 'secondary', 'submit', false ); ?>
	</form>

	<p style="margin-top:16px;color:#646970;font-size:13px;">
		<?php esc_html_e( 'By using this plugin you agree to our', 'boostanova-ai-shop-assistant' ); ?>
		<a href="https://boostanova.com/en/terms-of-use" target="_blank" rel="noopener"><?php esc_html_e( 'Terms of Use', 'boostanova-ai-shop-assistant' ); ?></a>
		<?php esc_html_e( 'and', 'boostanova-ai-shop-assistant' ); ?>
		<a href="https://boostanova.com/en/privacy-policy" target="_blank" rel="noopener"><?php esc_html_e( 'Privacy Policy', 'boostanova-ai-shop-assistant' ); ?></a>.
	</p>

	<?php
	$boosaias_license_data = get_transient( 'boosaias_license_data' );
	if ( $boosaias_license_data ) {
		echo '<p><strong>' . esc_html__( 'Status:', 'boostanova-ai-shop-assistant' ) . '</strong> ' .
			esc_html( ucfirst( $boosaias_license_data['plan'] ) ) . ' — ' .
			esc_html( $boosaias_license_data['limits']['monthly_messages'] ) . ' ' .
			esc_html__( 'messages/month', 'boostanova-ai-shop-assistant' ) . '</p>';
	}
	?>
</div>
