<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since 1.0.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
namespace Wisewand\Utils;

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

class Debug_Helper {

	public static function test_api_client_loading() {
		$results = array();

		// Test 1: Check if class exists
		$results['class_exists'] = class_exists( 'Wisewand\\Utils\\Wisewand_API_Client' );

		// Test 2: Check if API key is stored
		$api_key                   = get_option( 'wisewand_api_key', '' );
		$results['api_key_stored'] = ! empty( $api_key );
		$results['api_key_length'] = strlen( $api_key );

		// Test 3: Test static methods
		try {
			$results['is_configured'] = Wisewand_API_Client::is_api_key_configured();
			$results['masked_key']    = Wisewand_API_Client::get_masked_api_key();
		} catch ( Exception $e ) {
			$results['static_methods_error'] = $e->getMessage();
		}

		// Test 4: Try to instantiate API client
		try {
			if ( $results['api_key_stored'] ) {
				$api_client               = new Wisewand_API_Client( $api_key );
				$results['instantiation'] = 'success';

				// Test 5: Try API call
				$test_result = $api_client->test_connection();
				if ( is_wp_error( $test_result ) ) {
					$results['api_call'] = 'error: ' . $test_result->get_error_message();
				} else {
					$results['api_call']           = 'success';
					$results['articles_available'] = count( $test_result['items'] ?? array() );
				}
			} else {
				$results['instantiation'] = 'skipped (no API key)';
			}
		} catch ( Exception $e ) {
			$results['instantiation'] = 'error: ' . $e->getMessage();
		}

		return $results;
	}

	public static function render_debug_info() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$debug_info = self::test_api_client_loading();

		?>
		<div class="wisewand-debug-info" style="background: #f8f9fa; border: 1px solid #dee2e6; padding: 15px; margin: 20px 0; border-radius: 4px;">
			<h3>🔍 Wisewand API Debug Information</h3>
			<table style="width: 100%; border-collapse: collapse;">
				<?php foreach ( $debug_info as $key => $value ) : ?>
				<tr style="border-bottom: 1px solid #dee2e6;">
					<td style="padding: 8px; font-weight: bold; width: 200px;"><?php echo esc_html( $key ); ?></td>
					<td style="padding: 8px;">
						<?php if ( is_bool( $value ) ) : ?>
							<span style="color: <?php echo $value ? 'green' : 'red'; ?>;">
								<?php echo $value ? '✅ Yes' : '❌ No'; ?>
							</span>
						<?php else : ?>
							<code><?php echo esc_html( is_array( $value ) ? wp_json_encode( $value ) : $value ); ?></code>
						<?php endif; ?>
					</td>
				</tr>
				<?php endforeach; ?>
			</table>
		</div>
		<?php
	}
}