/**
 * API Settings Page JavaScript
 *
 * @package Wisewand
 * @since 1.0.0
 */

jQuery(document).ready(function($) {
	'use strict';

	// Toggle Wisewand API key visibility
	$('#toggle-api-key').on('click', function(e) {
		e.preventDefault();
		const $input = $('#wisewand_api_key');
		const type = $input.attr('type') === 'password' ? 'text' : 'password';
		$input.attr('type', type);
	});

	// Toggle OpenAI API key visibility
	$('#toggle-openai-key').on('click', function(e) {
		e.preventDefault();
		const $input = $('#wisewand_openai_api_key');
		const type = $input.attr('type') === 'password' ? 'text' : 'password';
		$input.attr('type', type);
	});

	// Test API connection (basic test)
	$('#test-api-connection').on('click', function(e) {
		e.preventDefault();

		const $button = $(this);
		const originalText = $button.text();

		$button.prop('disabled', true).text(wisewandApiSettings.i18n.testing);

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			data: {
				action: 'wisewand_test_api_connection',
				nonce: wisewandApiSettings.nonce
			},
			success: function(response) {
				if (response.success) {
					alert(wisewandApiSettings.i18n.connectionSuccess);
				} else {
					alert(wisewandApiSettings.i18n.connectionFailed + ' ' + (response.data?.message || 'Unknown error'));
				}
			},
			error: function() {
				alert(wisewandApiSettings.i18n.networkError);
			},
			complete: function() {
				$button.prop('disabled', false).text(originalText);
			}
		});
	});

	// Detailed API test
	$('#test-api-detailed').on('click', function(e) {
		e.preventDefault();

		const $button = $(this);
		const $status = $('#connection-status');
		const $info = $('#api-info');
		const $results = $('#wisewand-api-test-results');

		$button.prop('disabled', true).text(wisewandApiSettings.i18n.testing);
		$status.removeClass().addClass('wisewand-status-testing').text(wisewandApiSettings.i18n.testing);

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			data: {
				action: 'wisewand_detailed_api_test',
				nonce: wisewandApiSettings.nonceDetailed
			},
			success: function(response) {
				if (response.success) {
					$status.removeClass().addClass('wisewand-status-success').text(wisewandApiSettings.i18n.connected);

					let infoHtml = '<div class="wisewand-api-info">';
					if (response.data.rate_limit) {
						infoHtml += '<p><strong>' + wisewandApiSettings.i18n.rateLimit + '</strong> ' + response.data.rate_limit + '</p>';
					}
					if (response.data.articles_count !== undefined) {
						infoHtml += '<p><strong>' + wisewandApiSettings.i18n.availableArticles + '</strong> ' + response.data.articles_count + '</p>';
					}
					if (response.data.projects_count !== undefined) {
						infoHtml += '<p><strong>' + wisewandApiSettings.i18n.projects + '</strong> ' + response.data.projects_count + '</p>';
					}
					infoHtml += '</div>';

					$info.html(infoHtml);
					$results.html('<div class="notice notice-success"><p>' + wisewandApiSettings.i18n.detailedSuccess + '</p></div>').show();

				} else {
					$status.removeClass().addClass('wisewand-status-error').text(wisewandApiSettings.i18n.error);
					$info.html('<div class="wisewand-error">' + (response.data?.message || 'Unknown error') + '</div>');
					$results.html('<div class="notice notice-error"><p>' + wisewandApiSettings.i18n.connectionFailed + ' ' + (response.data?.message || 'Unknown error') + '</p></div>').show();
				}
			},
			error: function() {
				$status.removeClass().addClass('wisewand-status-error').text(wisewandApiSettings.i18n.error);
				$info.html('<div class="wisewand-error">' + wisewandApiSettings.i18n.networkError + '</div>');
				$results.html('<div class="notice notice-error"><p>' + wisewandApiSettings.i18n.networkError + '</p></div>').show();
			},
			complete: function() {
				$button.prop('disabled', false).text(wisewandApiSettings.i18n.runDetailedTest);
			}
		});
	});

	// Remove API key
	$('#remove-api-key').on('click', function(e) {
		e.preventDefault();

		if (!confirm(wisewandApiSettings.i18n.confirmRemove)) {
			return;
		}

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			data: {
				action: 'wisewand_remove_api_key',
				nonce: wisewandApiSettings.nonceRemove
			},
			success: function(response) {
				if (response.success) {
					location.reload();
				} else {
					alert(wisewandApiSettings.i18n.removeFailed + ' ' + (response.data?.message || 'Unknown error'));
				}
			},
			error: function() {
				alert(wisewandApiSettings.i18n.networkError);
			}
		});
	});
});
