<?php
/**
 * Verification Script for v1.3.0 Fixes
 *
 * Run this script after deploying v1.3.0 to verify all fixes are working.
 *
 * @package Wisewand
 * @since 1.3.0
 * @license GPL-2.0-or-later
 */

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

/**
 * Verification Test Suite
 */
class Wisewand_V130_Verification {

	/**
	 * Run all verification tests
	 *
	 * @return array Test results.
	 */
	public static function run_all_tests() {
		$results = array();

		$results['test_1'] = self::test_unique_constraint();
		$results['test_2'] = self::test_migration_10_completed();
		$results['test_3'] = self::test_multilingual_keywords();
		$results['test_4'] = self::test_schema_version();

		return $results;
	}

	/**
	 * Test 1: Verify UNIQUE constraint includes language_code
	 *
	 * @return array Test result.
	 */
	private static function test_unique_constraint() {
		global $wpdb;

		$table_name = $wpdb->prefix . 'wisewand_saved_keywords';

		// Check for new constraint
		$query = $wpdb->prepare(
			"SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
			WHERE TABLE_SCHEMA = %s
			AND TABLE_NAME = %s
			AND INDEX_NAME = 'keyword_language'",
			DB_NAME,
			$table_name
		);

		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query already prepared
		$count = (int) $wpdb->get_var( $query );

		$expected = 3; // keyword, language_code, location_code

		return array(
			'name'     => 'UNIQUE Constraint Update',
			'status'   => $count === $expected ? 'PASS' : 'FAIL',
			'expected' => $expected . ' columns in keyword_language constraint',
			'actual'   => $count . ' columns found',
			'message'  => $count === $expected
				? 'Multilingual keyword support enabled'
				: 'UNIQUE constraint not updated correctly',
		);
	}

	/**
	 * Test 2: Verify Migration 10 completed
	 *
	 * @return array Test result.
	 */
	private static function test_migration_10_completed() {
		$completed = get_option( 'wisewand_migration_10_completed' );

		return array(
			'name'     => 'Migration 10 Completion',
			'status'   => $completed ? 'PASS' : 'FAIL',
			'expected' => 'Migration completed timestamp',
			'actual'   => $completed ? 'Completed at ' . $completed : 'Not completed',
			'message'  => $completed
				? 'Migration 10 successfully applied'
				: 'Migration 10 not run - plugin may need reactivation',
		);
	}

	/**
	 * Test 3: Test multilingual keyword saving
	 *
	 * @return array Test result.
	 */
	private static function test_multilingual_keywords() {
		global $wpdb;

		$table_name  = $wpdb->prefix . 'wisewand_saved_keywords';
		$test_prefix = 'TEST_V130_';
		$timestamp   = time();

		// Clean up any previous test data
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM `{$table_name}` WHERE keyword LIKE %s",
				$test_prefix . '%'
			)
		);

		// Test keyword
		$test_keyword = $test_prefix . $timestamp;

		// Try saving same keyword in 3 languages
		$test_data = array(
			array( 'keyword' => $test_keyword, 'language_code' => 'en', 'location_code' => 2840 ),
			array( 'keyword' => $test_keyword, 'language_code' => 'fr', 'location_code' => 2250 ),
			array( 'keyword' => $test_keyword, 'language_code' => 'de', 'location_code' => 2276 ),
		);

		$inserted = 0;
		$errors   = array();

		foreach ( $test_data as $data ) {
			$result = $wpdb->insert( $table_name, $data );

			if ( $result !== false ) {
				++$inserted;
			} else {
				$errors[] = $wpdb->last_error;
			}
		}

		// Clean up test data
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM `{$table_name}` WHERE keyword LIKE %s",
				$test_prefix . '%'
			)
		);

		$expected = 3;

		return array(
			'name'     => 'Multilingual Keyword Support',
			'status'   => $inserted === $expected ? 'PASS' : 'FAIL',
			'expected' => $expected . ' keywords saved (same text, different languages)',
			'actual'   => $inserted . ' keywords saved',
			'message'  => $inserted === $expected
				? 'Multilingual keywords work correctly'
				: 'Failed to save multilingual keywords: ' . implode( ', ', $errors ),
		);
	}

	/**
	 * Test 4: Verify schema version updated
	 *
	 * @return array Test result.
	 */
	private static function test_schema_version() {
		$schema_version = \Wisewand\Database\Database_Schema::SCHEMA_VERSION;
		$expected       = '1.3.0';

		return array(
			'name'     => 'Schema Version Update',
			'status'   => $schema_version === $expected ? 'PASS' : 'FAIL',
			'expected' => $expected,
			'actual'   => $schema_version,
			'message'  => $schema_version === $expected
				? 'Schema version correctly updated'
				: 'Schema version not updated',
		);
	}

	/**
	 * Display test results
	 *
	 * @param array $results Test results.
	 */
	public static function display_results( $results ) {
		echo '<div style="padding: 20px; background: #1a1a1a; color: #fff; font-family: monospace;">';
		echo '<h2 style="color: #8b5cf6;">Wisewand v1.3.0 Verification Results</h2>';

		$total  = count( $results );
		$passed = 0;

		foreach ( $results as $result ) {
			$status_color = $result['status'] === 'PASS' ? '#10b981' : '#ef4444';
			$status_icon  = $result['status'] === 'PASS' ? '✓' : '✗';

			if ( $result['status'] === 'PASS' ) {
				++$passed;
			}

			echo '<div style="margin: 20px 0; padding: 15px; background: #2a2a2a; border-left: 4px solid ' . esc_attr( $status_color ) . ';">';
			echo '<div style="font-size: 16px; font-weight: bold; color: ' . esc_attr( $status_color ) . ';">';
			echo esc_html( $status_icon ) . ' ' . esc_html( $result['name'] ) . ' - ' . esc_html( $result['status'] );
			echo '</div>';
			echo '<div style="margin-top: 8px; color: #888;">Expected: ' . esc_html( $result['expected'] ) . '</div>';
			echo '<div style="color: #aaa;">Actual: ' . esc_html( $result['actual'] ) . '</div>';
			echo '<div style="margin-top: 8px; color: #fff;">' . esc_html( $result['message'] ) . '</div>';
			echo '</div>';
		}

		echo '<div style="margin-top: 30px; padding: 20px; background: #2a2a2a; border: 2px solid ' . ( $passed === $total ? '#10b981' : '#ef4444' ) . ';">';
		echo '<h3 style="margin: 0; color: ' . ( $passed === $total ? '#10b981' : '#ef4444' ) . ';">';
		echo esc_html( $passed . ' / ' . $total ) . ' Tests Passed';
		echo '</h3>';

		if ( $passed === $total ) {
			echo '<p style="color: #10b981; margin: 10px 0 0 0;">All fixes verified successfully! System is production ready.</p>';
		} else {
			echo '<p style="color: #ef4444; margin: 10px 0 0 0;">Some tests failed. Please review errors above.</p>';
		}
		echo '</div>';

		echo '</div>';
	}
}

// Auto-run if accessed as admin page
if ( current_user_can( 'manage_options' ) ) {
	$results = Wisewand_V130_Verification::run_all_tests();
	Wisewand_V130_Verification::display_results( $results );
}
