<?php
/**
 * GEO Audit page.
 *
 * Scans all site fields and shows a visual GEO optimization score.
 * Motivates users to fill fields or purchase the AI population service.
 *
 * @package Dennis_SEO
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class Dennis_SEO_Audit {

	const MENU_SLUG = 'dennis-seo-audit';

	/**
	 * Global settings checks with weights.
	 */
	const GLOBAL_CHECKS = array(
		'site_name'            => array( 'label' => 'Site name', 'weight' => 3 ),
		'business_description' => array( 'label' => 'Business description', 'weight' => 3 ),
		'site_type'            => array( 'label' => 'Organization type', 'weight' => 2 ),
		'org_logo'             => array( 'label' => 'Logo', 'weight' => 2 ),
		'org_phone'            => array( 'label' => 'Phone number', 'weight' => 2 ),
		'org_email'            => array( 'label' => 'Email', 'weight' => 2 ),
		'org_address_street'   => array( 'label' => 'Street address', 'weight' => 2 ),
		'org_address_city'     => array( 'label' => 'City', 'weight' => 1 ),
		'org_address_zip'      => array( 'label' => 'Postal code', 'weight' => 1 ),
		'org_address_country'  => array( 'label' => 'Country', 'weight' => 1 ),
		'org_service_area'     => array( 'label' => 'Service area', 'weight' => 2 ),
		'org_geo_lat'          => array( 'label' => 'Geo latitude', 'weight' => 2 ),
		'org_geo_lng'          => array( 'label' => 'Geo longitude', 'weight' => 2 ),
		'org_opening_hours'    => array( 'label' => 'Opening hours', 'weight' => 1 ),
		'_social'              => array( 'label' => 'Social profile (at least 1)', 'weight' => 2 ),
		'default_og_image'     => array( 'label' => 'Default OG image', 'weight' => 2 ),
		'llms_enabled'         => array( 'label' => 'llms.txt enabled', 'weight' => 3 ),
		'llms_header'          => array( 'label' => 'llms.txt header text', 'weight' => 2 ),
		'_homepage_title'      => array( 'label' => 'Homepage title', 'weight' => 1 ),
		'_homepage_desc'       => array( 'label' => 'Homepage description', 'weight' => 2 ),
	);

	/**
	 * Get total weight for global checks. Calculated dynamically to stay in sync.
	 */
	private static function get_global_max_weight(): int {
		static $weight = null;
		if ( null === $weight ) {
			$weight = array_sum( array_column( self::GLOBAL_CHECKS, 'weight' ) );
		}
		return $weight;
	}

	/**
	 * Per-page field checks with weights.
	 */
	const PAGE_CHECKS = array(
		'title'       => array( 'label' => 'SEO Title', 'weight' => 5, 'dennis_key' => '_dennis_seo_title', 'yoast_key' => '_yoast_wpseo_title' ),
		'description' => array( 'label' => 'Meta Description', 'weight' => 5, 'dennis_key' => '_dennis_seo_description', 'yoast_key' => '_yoast_wpseo_metadesc' ),
		'keyphrase'   => array( 'label' => 'Focus Keyphrase', 'weight' => 3, 'dennis_key' => '_dennis_seo_keyphrase', 'yoast_key' => '_yoast_wpseo_focuskw' ),
		'schema'      => array( 'label' => 'Schema Type', 'weight' => 4, 'dennis_key' => '_dennis_seo_schema_type', 'yoast_key' => '' ),
		'llms_desc'   => array( 'label' => 'llms.txt Description', 'weight' => 3, 'dennis_key' => '_dennis_seo_llms_description', 'yoast_key' => '' ),
	);

	/**
	 * Get total weight for page checks. Calculated dynamically to stay in sync.
	 */
	private static function get_page_max_weight(): int {
		static $weight = null;
		if ( null === $weight ) {
			$weight = array_sum( array_column( self::PAGE_CHECKS, 'weight' ) );
		}
		return $weight;
	}

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ), 9 );
		add_action( 'wp_ajax_dennis_seo_run_audit', array( $this, 'ajax_run_audit' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Quick score check for conditional UI. Cached for 15 minutes.
	 */
	public static function quick_score(): int {
		$cached = get_transient( 'dseo_quick_score' );
		if ( false !== $cached ) {
			return (int) $cached;
		}
		$audit        = new self();
		$global       = $audit->check_global();
		$pages        = $audit->check_pages();
		$llms         = $audit->check_llms();
		$global_score = ( $global['filled_weight'] / self::get_global_max_weight() ) * 30;
		$page_score   = $pages['total_pages'] > 0
			? ( $pages['filled_weight'] / ( $pages['total_pages'] * self::get_page_max_weight() ) ) * 60
			: 0;
		$llms_score   = ( $llms['filled_weight'] / 10 ) * 10;
		$total        = (int) round( min( 100, max( 0, $global_score + $page_score + $llms_score ) ) );
		set_transient( 'dseo_quick_score', $total, 900 );
		return $total;
	}

	public function enqueue_assets( string $hook ): void {
		if ( false === strpos( $hook, self::MENU_SLUG ) ) {
			return;
		}
		wp_enqueue_style( 'dseo-audit', DENNIS_SEO_URL . 'assets/css/admin-audit.css', array(), DENNIS_SEO_VERSION );
		wp_enqueue_script( 'dseo-audit', DENNIS_SEO_URL . 'assets/js/admin-audit.js', array(), DENNIS_SEO_VERSION, true );
		wp_localize_script( 'dseo-audit', 'dseoAudit', array(
			'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
			'auditNonce'     => wp_create_nonce( 'dennis_seo_audit' ),
			'populateNonce'  => wp_create_nonce( 'dennis_seo_generate_key' ),
			'homeUrl'        => home_url(),
			'statusNonce'    => wp_create_nonce( 'dennis_seo_check_status' ),
			'dennisLogoUrl'  => plugins_url( 'assets/dennis-white.png', dirname( __FILE__ ) ),
			'clearFieldsUrl' => admin_url( 'admin.php?page=dennis-seo#dseo-clear-fields' ),
			'i18n' => array(
				'globalSettings'   => __( 'Global Settings', 'dennis-geo' ),
				'pageSeo'          => __( 'Page SEO', 'dennis-geo' ),
				'pages'            => __( 'pages', 'dennis-geo' ),
				'recommendations'  => __( 'Recommendations', 'dennis-geo' ),
				'scoreCritical'    => __( 'Critical: Your technical AI visibility is very low. Most fields are missing.', 'dennis-geo' ),
				'scoreNeedsWork'   => __( 'Needs work: Some technical fields are set, but major gaps remain.', 'dennis-geo' ),
				'scoreGood'        => __( 'Good progress: Most fields are configured. Keep going to complete your setup.', 'dennis-geo' ),
				'scoreAlmost'      => __( 'Almost there! Just a few fields left to complete your technical AI visibility.', 'dennis-geo' ),
				'scorePerfect'     => __( 'Congratulations! Your technical AI visibility is now fully configured!', 'dennis-geo' ),
				'recGreatStart'    => __( 'Great start! Head over to Dennis GEO → Settings and fill in your business name, description, and contact details. Even a few fields will boost your score significantly.', 'dennis-geo' ),
				'recPartialGlobal' => __( 'Your global settings are partially filled. A quick 5-minute pass through Dennis GEO → Settings (address, phone, social profiles) will move the score fast.', 'dennis-geo' ),
				'recAlmostGlobal'  => __( "Almost there on global settings — just a few fields left. Check Dennis GEO → Settings for the gaps.", 'dennis-geo' ),
				'recGeoMissing'    => __( "Geo coordinates are missing. On the Settings page there's an easy helper — just paste a Google Maps URL and the coordinates are extracted automatically.", 'dennis-geo' ),
				'recPhoneMissing'  => __( 'Add your phone number in Settings. Use international format (e.g. +46701234567). This powers the telephone field in your Organization schema.', 'dennis-geo' ),
				'recSocialMissing' => __( 'Add at least one social profile (Facebook, Instagram, LinkedIn, or Google Business) in Settings → Social. These go into your schema as sameAs links.', 'dennis-geo' ),
				'recOgImageMissing'=> __( 'Set a default OG image in Settings → Social. This is the image shown when your pages are shared on social media without a featured image.', 'dennis-geo' ),
				'recLlmsDisabled'  => __( 'Enable llms.txt in Settings → llms.txt. This makes your site discoverable by AI crawlers in a format they can read efficiently.', 'dennis-geo' ),
				'recPagesMissing'  => __( 'Some pages are missing SEO fields. Focus on titles and meta descriptions first — those have the highest impact on both search engines and AI citations.', 'dennis-geo' ),
				'recPagesNone'     => __( 'None of your pages have SEO fields filled yet. Edit any page and scroll to the Dennis GEO meta box to start — or let Dennis handle it all at once.', 'dennis-geo' ),
				'recLlmsFullMissing' => __( 'Generate your llms-full.txt content in Settings → llms-full.txt. This gives AI crawlers the full text of your site in one clean file.', 'dennis-geo' ),
				'recAddFaq'        => __( "Add FAQ sections using the WordPress Details block on your top pages. Dennis GEO auto-generates FAQPage schema from these — it's the highest-leverage signal for AI citations.", 'dennis-geo' ),
				'recSubmitSitemap' => __( 'Submit your sitemap to Google Search Console so crawlers pick up the structured changes.', 'dennis-geo' ),
				'tooManyFields'    => __( 'Too many fields to fill manually?', 'dennis-geo' ),
				'fillOneClick'     => __( 'Fill every SEO & GEO field in one click.', 'dennis-geo' ),
				'aiDescription'    => __( 'Our AI reads every page on your site and fills all fields automatically — titles, descriptions, keyphrases, schema types, llms.txt, and more. One-time payment of $19. No subscription.', 'dennis-geo' ),
				'fieldsLeftAsIs'   => __( "Fields you've already filled are left as they are. Want all fields rewritten?", 'dennis-geo' ),
				'clearFields'      => __( 'Clear Dennis GEO fields', 'dennis-geo' ),
				'first'            => __( 'first.', 'dennis-geo' ),
				'serviceDisclaimer'=> __( 'By using this service, your page content will be sent to dennisgeo.com for AI processing.', 'dennis-geo' ),
				'checkout'         => __( 'Checkout — $19', 'dennis-geo' ),
				'techComplete'     => __( 'Technical foundation complete!', 'dennis-geo' ),
				'techCompleteDesc' => __( 'Your technical AI visibility fields are configured. The next step is content optimization: FAQ sections, knowledge pages, expert credentials, and fresh content. A complete GEO audit analyzes these areas and gives you a prioritized action plan.', 'dennis-geo' ),
				'learnMore'        => __( 'Complete GEO Audit?', 'dennis-geo' ),
				'runAgain'         => __( 'Run Audit Again', 'dennis-geo' ),
				'auditFailed'      => __( 'Audit failed', 'dennis-geo' ),
				'somethingWrong'   => __( 'Something went wrong. Please try again.', 'dennis-geo' ),
				'tryAgain'         => __( 'Try Again', 'dennis-geo' ),
				'checkoutOpened'   => __( 'Checkout opened in a new tab', 'dennis-geo' ),
				'dennisWorking'    => __( "Once payment goes through, Dennis gets to work — reading your site and filling in every SEO field. Give him a minute or two. It normally doesn't take long, he's really efficient.", 'dennis-geo' ),
				'notifyWhenDone'   => __( "We'll let you know here when he's finished. Feel free to close this and come back later.", 'dennis-geo' ),
				'generatingKey'    => __( 'Generating key...', 'dennis-geo' ),
				'errorTryAgain'    => __( 'Error — try again', 'dennis-geo' ),
				'populated'        => __( 'Your site has been populated!', 'dennis-geo' ),
				'reloadAudit'      => __( 'All SEO & GEO fields are now filled. Reload the page to see the updated audit score.', 'dennis-geo' ),
				'reloadPage'       => __( 'Reload page', 'dennis-geo' ),
			),
		) );
	}

	/**
	 * Register the top-level menu and GEO Audit submenu.
	 */
	public function register_menu(): void {
		add_menu_page(
			__( 'Dennis GEO', 'dennis-geo' ),
			__( 'Dennis GEO', 'dennis-geo' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render_page' ),
			'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyMCAyMCI+PGltYWdlIGhyZWY9ImRhdGE6aW1hZ2UvcG5nO2Jhc2U2NCxpVkJPUncwS0dnb0FBQUFOU1VoRVVnQUFBQlFBQUFBVUNBWUFBQUNOaVIwTkFBQUIya2xFUVZSNG5KMlV3V29VUVJDR3Y1cmRUY1NGU01oQk1BZlJnT0JCUFFnUzBKUGU5T2doNkFPWUJ4RGZRQytlQkVGdm5uME9Ud3FLSUJLUTZGRlJJWkVvMGVodTV2T3dOV1oyTmpzQkM0YXU3cTcrNjYrdWZ4cit3OVNZdGpleG9SYkFETkFIVGdOTHdCbmdCUEFDZUFqc0FCRVI1VUdaaXh4dnFaL2QzKzYzWVJTTk1sU1hnQUh3SGlpQllZWU04MXRSWDZublc4dXZOdFI1OWFOYXFydkpxcXpGRFhMdFNjNW42NkJGRXpnaXZnRi84bzQ2TWJLaXR0OUw1bDMxSExBYUVWYlhWUTlVamM0bzE2ZUltTnJKUExjR2ZBVTIxRG15d2MwN1pGaTZDQnl0cmJ1ZkQxd0Fsb0Vqd0dPZ3AwYTk1SWdJZ1FYZ25icGRMZFlEMHQwRXRvQ1R3Qnh3THlKMkFMcTE0REpacmdNL0k2TGZVdkloNEFmd0cvZ2VFV3RxTWFGTHRlZ0VxR2Rid0ZCZnExZlVWWFZSN2JUSnA1dUN2TnNDZUgxQ0htbGpUVkU3UUpHcEJvMG1WSEZENEVQNnMycTM3ZDhtMlYxVXQxTFlkVkh2cHVEZnFxZW1zVVF0MU9QcVRmV3ArcXNwbDJwZUpWQTMxUWZxWlhYaEg4c3NkVWE5clc3VUhvRmhJNm5OZWRvWDlVYmlqSk5XRDZzcjZyTUdxMnFzU3RiUmEzUkhuYStJVFpUZG1GOVRYNmEvN2JnOVVvOU5PMXNIaWJxbTFKNTZWVjFXbjZ0djFFdkZYbng3aHh2Z0hkalRWVXFrZnhEUVg0bEI4UGdzL3NHa0FBQUFBRWxGVGtTdVFtQ0MiIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIvPjwvc3ZnPg==',
			80
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'GEO Audit', 'dennis-geo' ),
			__( 'GEO Audit', 'dennis-geo' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render_page' )
		);
	}

	/**
	 * AJAX handler — runs the audit and returns JSON.
	 */
	public function ajax_run_audit(): void {
		check_ajax_referer( 'dennis_seo_audit', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Unauthorized' );
		}

		$global = $this->check_global();
		$pages  = $this->check_pages();
		$llms   = $this->check_llms();

		$global_score = ( $global['filled_weight'] / self::get_global_max_weight() ) * 30;
		$page_score   = $pages['total_pages'] > 0
			? ( $pages['filled_weight'] / ( $pages['total_pages'] * self::get_page_max_weight() ) ) * 60
			: 0;
		$llms_score   = ( $llms['filled_weight'] / 10 ) * 10;

		$total = (int) round( $global_score + $page_score + $llms_score );
		$total = min( 100, max( 0, $total ) );

		wp_send_json_success( array(
			'total'  => $total,
			'global' => $global,
			'pages'  => $pages,
			'llms'   => $llms,
			'scores' => array(
				'global' => (int) round( ( $global['filled_weight'] / self::get_global_max_weight() ) * 100 ),
				'pages'  => $pages['total_pages'] > 0 ? (int) round( ( $pages['filled_weight'] / ( $pages['total_pages'] * self::get_page_max_weight() ) ) * 100 ) : 0,
				'llms'   => (int) round( ( $llms['filled_weight'] / 10 ) * 100 ),
			),
		) );
	}

	/**
	 * Check global settings.
	 */
	private static function translate_label( string $label ): string {
		$labels = array(
			'Site name'                 => __( 'Site name', 'dennis-geo' ),
			'Business description'      => __( 'Business description', 'dennis-geo' ),
			'Organization type'         => __( 'Organization type', 'dennis-geo' ),
			'Logo'                      => __( 'Logo', 'dennis-geo' ),
			'Phone number'              => __( 'Phone number', 'dennis-geo' ),
			'Email'                     => __( 'Email', 'dennis-geo' ),
			'Street address'            => __( 'Street address', 'dennis-geo' ),
			'City'                      => __( 'City', 'dennis-geo' ),
			'Postal code'               => __( 'Postal code', 'dennis-geo' ),
			'Country'                   => __( 'Country', 'dennis-geo' ),
			'Service area'              => __( 'Service area', 'dennis-geo' ),
			'Geo latitude'              => __( 'Geo latitude', 'dennis-geo' ),
			'Geo longitude'             => __( 'Geo longitude', 'dennis-geo' ),
			'Opening hours'             => __( 'Opening hours', 'dennis-geo' ),
			'Social profile (at least 1)' => __( 'Social profile (at least 1)', 'dennis-geo' ),
			'Default OG image'          => __( 'Default OG image', 'dennis-geo' ),
			'llms.txt enabled'          => __( 'llms.txt enabled', 'dennis-geo' ),
			'llms.txt header text'      => __( 'llms.txt header text', 'dennis-geo' ),
			'Homepage title'            => __( 'Homepage title', 'dennis-geo' ),
			'Homepage description'      => __( 'Homepage description', 'dennis-geo' ),
			'SEO Title'                 => __( 'SEO Title', 'dennis-geo' ),
			'Meta Description'          => __( 'Meta Description', 'dennis-geo' ),
			'Focus Keyphrase'           => __( 'Focus Keyphrase', 'dennis-geo' ),
			'Schema Type'               => __( 'Schema Type', 'dennis-geo' ),
			'llms.txt Description'      => __( 'llms.txt Description', 'dennis-geo' ),
			'llms-full.txt has content' => __( 'llms-full.txt has content', 'dennis-geo' ),
			'llms-full.txt is substantial (>5KB)' => __( 'llms-full.txt is substantial (>5KB)', 'dennis-geo' ),
		);
		return $labels[ $label ] ?? $label;
	}

	private function check_global(): array {
		$opts    = Dennis_SEO_Settings::get_all();
		$checks  = array();
		$filled  = 0;

		foreach ( self::GLOBAL_CHECKS as $key => $check ) {
			$passed = false;

			if ( '_social' === $key ) {
				$passed = ! empty( $opts['social_facebook'] ) || ! empty( $opts['social_instagram'] ) || ! empty( $opts['social_linkedin'] ) || ! empty( $opts['social_google'] );
			} elseif ( '_homepage_title' === $key ) {
				$passed = ! empty( $opts['homepage_title'] );
				if ( ! $passed ) {
					$front_id = (int) get_option( 'page_on_front' );
					if ( $front_id ) {
						$passed = ! empty( get_post_meta( $front_id, '_dennis_seo_title', true ) );
					}
				}
			} elseif ( '_homepage_desc' === $key ) {
				$passed = ! empty( $opts['homepage_description'] );
				if ( ! $passed ) {
					$front_id = (int) get_option( 'page_on_front' );
					if ( $front_id ) {
						$passed = ! empty( get_post_meta( $front_id, '_dennis_seo_description', true ) );
					}
				}
			} elseif ( 'org_logo' === $key ) {
				$passed = ! empty( $opts['org_logo'] ) || get_theme_mod( 'custom_logo' );
			} elseif ( 'llms_enabled' === $key ) {
				$passed = ! empty( $opts['llms_enabled'] );
			} else {
				$passed = ! empty( $opts[ $key ] );
			}

			$checks[] = array(
				'label'  => self::translate_label( $check['label'] ),
				'weight' => $check['weight'],
				'passed' => $passed,
			);

			if ( $passed ) {
				$filled += $check['weight'];
			}
		}

		return array(
			'checks'        => $checks,
			'filled_weight' => $filled,
			'max_weight'    => self::get_global_max_weight(),
		);
	}

	/**
	 * Check per-page fields across all published content.
	 */
	private function check_pages(): array {
		$post_types = get_post_types( array( 'public' => true ), 'names' );
		unset( $post_types['attachment'] );

		$posts = get_posts( array(
			'post_type'      => array_values( $post_types ),
			'post_status'    => 'publish',
			'posts_per_page' => 2000,
		) );

		// Exclude noindexed pages.
		$posts = array_filter( $posts, function ( $post ) {
			return '1' !== get_post_meta( $post->ID, '_dennis_seo_robots_noindex', true );
		} );

		$yoast_active = defined( 'WPSEO_VERSION' );
		$total_pages  = count( $posts );
		$filled       = 0;
		$field_stats  = array();

		foreach ( self::PAGE_CHECKS as $field_key => $check ) {
			$field_stats[ $field_key ] = array(
				'label'  => self::translate_label( $check['label'] ),
				'filled' => 0,
				'total'  => $total_pages,
			);
		}

		foreach ( $posts as $post ) {
			foreach ( self::PAGE_CHECKS as $field_key => $check ) {
				$has_value = ! empty( get_post_meta( $post->ID, $check['dennis_key'], true ) );

				// Check Yoast fallback for applicable fields.
				if ( ! $has_value && $yoast_active && ! empty( $check['yoast_key'] ) ) {
					$has_value = ! empty( get_post_meta( $post->ID, $check['yoast_key'], true ) );
				}

				if ( $has_value ) {
					$filled += $check['weight'];
					$field_stats[ $field_key ]['filled']++;
				}
			}
		}

		return array(
			'total_pages'   => $total_pages,
			'filled_weight' => $filled,
			'max_weight'    => $total_pages * self::get_page_max_weight(),
			'field_stats'   => array_values( $field_stats ),
		);
	}

	/**
	 * Check llms-full.txt content.
	 */
	private function check_llms(): array {
		$content = Dennis_SEO_Settings::get( 'llms_full_content', '' );
		$checks  = array();
		$filled  = 0;

		$has_content = ! empty( trim( $content ) );
		$checks[] = array( 'label' => self::translate_label( 'llms-full.txt has content' ), 'weight' => 5, 'passed' => $has_content );
		if ( $has_content ) {
			$filled += 5;
		}

		$is_substantial = strlen( $content ) > 5000;
		$checks[] = array( 'label' => self::translate_label( 'llms-full.txt is substantial (>5KB)' ), 'weight' => 5, 'passed' => $is_substantial );
		if ( $is_substantial ) {
			$filled += 5;
		}

		return array(
			'checks'        => $checks,
			'filled_weight' => $filled,
			'max_weight'    => 10,
		);
	}

	/**
	 * Render the audit page.
	 */
	public function render_page(): void {
		?>

		<div class="dseo-audit">
			<div class="dseo-audit-header">
				<span class="dashicons dashicons-chart-bar"></span>
				<h1><?php esc_html_e( 'GEO Audit', 'dennis-geo' ); ?></h1>
			</div>

			<!-- Pre-run -->
			<div class="dseo-audit-intro" id="dseo-audit-intro">
				<span class="dashicons dashicons-search" style="font-size:48px;width:48px;height:48px;color:#7e5cef;margin-bottom:16px;"></span>
				<h2 style="margin:0 0 8px;font-size:20px;"><?php esc_html_e( 'How visible is your site to AI search engines?', 'dennis-geo' ); ?></h2>
				<p><?php esc_html_e( 'This audit checks your technical AI visibility — schema markup, meta tags, llms.txt, and AI crawler settings. It scores how well the technical foundation is configured for AI systems like ChatGPT, Perplexity, and Google AI Overviews.', 'dennis-geo' ); ?></p>
				<button class="dseo-run-btn" id="dseo-run-btn">
					<span class="dashicons dashicons-controls-play" style="margin-top:2px;"></span>
					<?php esc_html_e( 'Run GEO Audit', 'dennis-geo' ); ?>
				</button>
			</div>

			<!-- Loading -->
			<div class="dseo-audit-loading" id="dseo-audit-loading">
				<h2 style="margin:0 0 8px;font-size:18px;"><?php esc_html_e( 'Running GEO Audit...', 'dennis-geo' ); ?></h2>
				<div class="dseo-loading-pct" id="dseo-loading-pct">0%</div>
				<div class="dseo-loading-bar"><div class="dseo-loading-fill" id="dseo-loading-fill"></div></div>
				<div class="dseo-loading-steps">
					<div class="dseo-loading-step" id="dseo-step-1"><?php esc_html_e( 'Checking global settings', 'dennis-geo' ); ?><span class="dseo-dots"></span></div>
					<div class="dseo-loading-step" id="dseo-step-2"><?php esc_html_e( 'Scanning pages and posts', 'dennis-geo' ); ?><span class="dseo-dots"></span></div>
					<div class="dseo-loading-step" id="dseo-step-3"><?php esc_html_e( 'Analyzing schema configuration', 'dennis-geo' ); ?><span class="dseo-dots"></span></div>
					<div class="dseo-loading-step" id="dseo-step-4"><?php esc_html_e( 'Checking llms.txt and AI files', 'dennis-geo' ); ?><span class="dseo-dots"></span></div>
					<div class="dseo-loading-step" id="dseo-step-5"><?php esc_html_e( 'Calculating GEO score', 'dennis-geo' ); ?><span class="dseo-dots"></span></div>
				</div>
			</div>

			<!-- Results -->
			<div class="dseo-audit-results" id="dseo-audit-results"></div>
		</div>

		<?php
	}
}
