<?php
/**
 * Admin settings and product UI.
 *
 * @package MicropediaAISearchOptimizer
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/** Settings page. */
class Micropedia_AI_Settings {
	/** Menu slugs. */
	const SLUG_DASHBOARD = 'micropedia-ai-search-optimizer';
	const SLUG_SETTINGS  = 'micropedia-ai-settings';
	const SLUG_FILES     = 'micropedia-ai-files';
	const SLUG_CRAWLERS  = 'micropedia-ai-crawlers';
	const SLUG_SCHEMA    = 'micropedia-ai-schema';
	const SLUG_SCORE     = 'micropedia-ai-score';

	/** Init hooks. */
	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
		add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
		add_action( 'admin_init', array( __CLASS__, 'maybe_dismiss_review_notice' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
	}

	/** Add admin menu. */
	public static function admin_menu() {
		add_menu_page(
			esc_html__( 'Micropedia AI', 'micropedia-ai-search-optimizer' ),
			esc_html__( 'Micropedia AI', 'micropedia-ai-search-optimizer' ),
			'manage_options',
			self::SLUG_DASHBOARD,
			array( __CLASS__, 'render_dashboard' ),
			'dashicons-welcome-view-site',
			58
		);

		add_submenu_page( self::SLUG_DASHBOARD, __( 'Dashboard', 'micropedia-ai-search-optimizer' ), __( 'Dashboard', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_DASHBOARD, array( __CLASS__, 'render_dashboard' ) );
		add_submenu_page( self::SLUG_DASHBOARD, __( 'Settings', 'micropedia-ai-search-optimizer' ), __( 'Settings', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_SETTINGS, array( __CLASS__, 'render_settings' ) );
		add_submenu_page( self::SLUG_DASHBOARD, __( 'AI Files', 'micropedia-ai-search-optimizer' ), __( 'AI Files', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_FILES, array( __CLASS__, 'render_files' ) );
		add_submenu_page( self::SLUG_DASHBOARD, __( 'Crawler Control', 'micropedia-ai-search-optimizer' ), __( 'Crawler Control', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_CRAWLERS, array( __CLASS__, 'render_crawlers' ) );
		add_submenu_page( self::SLUG_DASHBOARD, __( 'Schema', 'micropedia-ai-search-optimizer' ), __( 'Schema', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_SCHEMA, array( __CLASS__, 'render_schema' ) );
		add_submenu_page( self::SLUG_DASHBOARD, __( 'AI Score', 'micropedia-ai-search-optimizer' ), __( 'AI Score', 'micropedia-ai-search-optimizer' ), 'manage_options', self::SLUG_SCORE, array( __CLASS__, 'render_score' ) );
	}

	/** Register settings. */
	public static function register_settings() {
		register_setting(
			'micropedia_ai_settings_group',
			'micropedia_ai_options',
			array(
				'type'              => 'array',
				'sanitize_callback' => array( __CLASS__, 'sanitize_options' ),
				'default'           => micropedia_ai_default_options(),
			)
		);
	}

	/** Enqueue admin assets. */
	public static function enqueue_assets( $hook ) {
		if ( false === strpos( (string) $hook, 'micropedia-ai' ) ) {
			return;
		}
		wp_enqueue_style( 'micropedia-ai-admin', MICROPEDIA_AI_SEARCH_OPTIMIZER_URL . 'admin/assets/css/admin.css', array(), MICROPEDIA_AI_SEARCH_OPTIMIZER_VERSION );
	}

	/** Sanitize options. */
	public static function sanitize_options( $input ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return micropedia_ai_default_options();
		}

		$input   = is_array( $input ) ? $input : array();
		$current = wp_parse_args( get_option( 'micropedia_ai_options', array() ), micropedia_ai_default_options() );
		$options = $current;

		$checkbox = array( 'enable_llms', 'enable_schema', 'enable_crawler_control' );
		foreach ( $checkbox as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$options[ $key ] = ! empty( $input[ $key ] ) ? 1 : 0;
			}
		}

		$pro_checkbox = array( 'enable_llms_full', 'enable_markdown', 'enable_ai_score' );
		foreach ( $pro_checkbox as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$options[ $key ] = micropedia_ai_is_pro() && ! empty( $input[ $key ] ) ? 1 : 0;
			}
		}

		$allowed_languages = array( '', 'it_IT', 'fr_FR', 'es_ES', 'en_US' );
		if ( array_key_exists( 'plugin_language', $input ) ) {
			$lang = sanitize_text_field( wp_unslash( $input['plugin_language'] ) );
			$options['plugin_language'] = in_array( $lang, $allowed_languages, true ) ? $lang : '';
		}

		if ( array_key_exists( 'posts_limit', $input ) ) {
			$options['posts_limit'] = min( 100, max( 1, absint( $input['posts_limit'] ) ) );
		}

		if ( array_key_exists( 'cache_ttl', $input ) ) {
			$options['cache_ttl'] = min( DAY_IN_SECONDS, max( 300, absint( $input['cache_ttl'] ) ) );
		}

		$crawler_keys = array( 'crawler_oai_searchbot', 'crawler_gptbot', 'crawler_google_extended', 'crawler_perplexitybot', 'crawler_claudebot' );
		foreach ( $crawler_keys as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$value = sanitize_key( wp_unslash( $input[ $key ] ) );
				$options[ $key ] = in_array( $value, array( 'allow', 'disallow', 'neutral' ), true ) ? $value : 'neutral';
			}
		}

		$schema_types = array( 'Article', 'Organization', 'Person' );
		if ( array_key_exists( 'schema_base_type', $input ) ) {
			$type = sanitize_text_field( wp_unslash( $input['schema_base_type'] ) );
			$options['schema_base_type'] = in_array( $type, $schema_types, true ) ? $type : 'Organization';
		}

		foreach ( array( 'schema_base_name', 'schema_base_id', 'schema_base_description', 'schema_base_same_as' ) as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$options[ $key ] = sanitize_textarea_field( wp_unslash( $input[ $key ] ) );
			}
		}

		foreach ( array( 'schema_base_url', 'schema_base_image' ) as $key ) {
			if ( array_key_exists( $key, $input ) ) {
				$options[ $key ] = esc_url_raw( wp_unslash( $input[ $key ] ) );
			}
		}

		delete_transient( 'micropedia_ai_llms_txt' );
		delete_transient( 'micropedia_ai_llms_full_txt' );
		return $options;
	}

	/** Shared shell open. */
	private static function shell_open( $title = '' ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}
		$current = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : self::SLUG_DASHBOARD;
		?>
		<div class="micropedia-ai-wrap"><div class="micropedia-ai-layout">
			<aside class="micropedia-ai-sidebar">
				<div class="micropedia-ai-brand"><img src="<?php echo esc_url( MICROPEDIA_AI_SEARCH_OPTIMIZER_URL . 'admin/assets/images/icon-128x128.png' ); ?>" alt="" /><div><strong>Micropedia</strong><span>AI Search Optimizer</span></div></div>
				<div class="micropedia-ai-developer-badge"><?php echo esc_html__( 'Developed by', 'micropedia-ai-search-optimizer' ); ?> <a target="_blank" rel="noopener" href="<?php echo esc_url( MICROPEDIA_AI_DEVELOPER_URL ); ?>"><?php echo esc_html( MICROPEDIA_AI_DEVELOPER_NAME ); ?></a></div>
				<nav class="micropedia-ai-nav">
					<?php self::nav_link( self::SLUG_DASHBOARD, __( 'Dashboard', 'micropedia-ai-search-optimizer' ), $current ); ?>
					<?php self::nav_link( self::SLUG_SETTINGS, __( 'Settings', 'micropedia-ai-search-optimizer' ), $current ); ?>
					<?php self::nav_link( self::SLUG_FILES, __( 'AI Files', 'micropedia-ai-search-optimizer' ), $current ); ?>
					<?php self::nav_link( self::SLUG_CRAWLERS, __( 'Crawler Control', 'micropedia-ai-search-optimizer' ), $current ); ?>
					<?php self::nav_link( self::SLUG_SCHEMA, __( 'Schema', 'micropedia-ai-search-optimizer' ), $current ); ?>
					<?php self::nav_link( self::SLUG_SCORE, __( 'AI Score', 'micropedia-ai-search-optimizer' ), $current ); ?>
				</nav>
				<div class="micropedia-ai-pro-card"><b>PRO</b><p><?php echo esc_html__( 'Unlock PRO features like llms-full.txt, Markdown export and AI Score.', 'micropedia-ai-search-optimizer' ); ?></p><a class="micropedia-ai-button" target="_blank" rel="noopener" href="<?php echo esc_url( micropedia_ai_get_pro_url() ); ?>"><?php echo esc_html__( 'Upgrade to PRO', 'micropedia-ai-search-optimizer' ); ?></a></div>
			</aside><main class="micropedia-ai-content">
			<?php if ( self::SLUG_DASHBOARD === $current ) : ?>
				<div class="micropedia-ai-hero"><div class="micropedia-ai-hero-content"><h1>Micropedia<span>AI Search Optimizer</span></h1><p><?php echo esc_html__( 'Optimization for AI Search & LLM', 'micropedia-ai-search-optimizer' ); ?></p></div></div>
			<?php else : ?>
				<h1 class="micropedia-ai-page-title"><?php echo esc_html( $title ); ?></h1>
			<?php endif; ?>
		<?php self::review_notice(); ?>
		<?php
		return true;
	}

	/** Shared shell close. */
	private static function shell_close() {
		echo '<div class="micropedia-ai-admin-footer">Micropedia AI Search Optimizer &mdash; Developed by <a target="_blank" rel="noopener" href="' . esc_url( MICROPEDIA_AI_DEVELOPER_URL ) . '">' . esc_html( MICROPEDIA_AI_DEVELOPER_NAME ) . '</a></div>';
		echo '</main></div></div>';
	}

	/** Render nav link. */
	private static function nav_link( $slug, $label, $current ) {
		$class = $slug === $current ? ' class="is-active"' : '';
		echo '<a' . $class . ' href="' . esc_url( admin_url( 'admin.php?page=' . $slug ) ) . '"><span>' . esc_html( $label ) . '</span></a>';
	}


	/** Dismiss the review invitation for the current admin user. */
	public static function maybe_dismiss_review_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( empty( $_GET['micropedia_ai_dismiss_review'] ) ) {
			return;
		}

		check_admin_referer( 'micropedia_ai_dismiss_review' );
		update_user_meta( get_current_user_id(), 'micropedia_ai_review_notice_dismissed', 1 );

		$redirect = remove_query_arg( array( 'micropedia_ai_dismiss_review', '_wpnonce' ) );
		wp_safe_redirect( $redirect );
		exit;
	}

	/** Render a small multilingual review invitation. */
	private static function review_notice() {
		if ( get_user_meta( get_current_user_id(), 'micropedia_ai_review_notice_dismissed', true ) ) {
			return;
		}

		$review_url  = 'https://wordpress.org/support/plugin/micropedia-ai-search-optimizer/reviews/#new-post';
		$dismiss_url = wp_nonce_url( add_query_arg( 'micropedia_ai_dismiss_review', '1' ), 'micropedia_ai_dismiss_review' );
		?>
		<div class="micropedia-ai-review-banner">
			<div>
				<strong><?php echo esc_html__( 'Do you like Micropedia AI Search Optimizer?', 'micropedia-ai-search-optimizer' ); ?></strong>
				<span><?php echo esc_html__( 'A short review on WordPress.org helps the project grow and keeps the free version useful.', 'micropedia-ai-search-optimizer' ); ?></span>
			</div>
			<div class="micropedia-ai-review-actions">
				<a class="micropedia-ai-button" href="<?php echo esc_url( $review_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html__( 'Leave a review', 'micropedia-ai-search-optimizer' ); ?></a>
				<a class="micropedia-ai-review-dismiss" href="<?php echo esc_url( $dismiss_url ); ?>"><?php echo esc_html__( 'Maybe later', 'micropedia-ai-search-optimizer' ); ?></a>
			</div>
		</div>
		<?php
	}

	/** Dashboard page. */
	public static function render_dashboard() {
		if ( ! self::shell_open() ) { return; }
		$options = wp_parse_args( get_option( 'micropedia_ai_options', array() ), micropedia_ai_default_options() );
		?>
		<h1 class="micropedia-ai-page-title">Micropedia AI Search Optimizer</h1>
		<div class="micropedia-ai-disclaimer"><?php echo esc_html__( 'This plugin improves AI-readable structure and accessibility. It does not guarantee inclusion, ranking, indexing, visibility, traffic, or citation by AI systems.', 'micropedia-ai-search-optimizer' ); ?></div>
		<div class="micropedia-ai-developer-card"><strong><?php echo esc_html__( 'Developed by', 'micropedia-ai-search-optimizer' ); ?> <?php echo esc_html( MICROPEDIA_AI_DEVELOPER_NAME ); ?></strong><span><?php echo esc_html__( 'An AI SEO / GEO layer for WordPress content readability and LLM accessibility.', 'micropedia-ai-search-optimizer' ); ?></span></div>
		<div class="micropedia-ai-grid">
			<?php self::feature_card( __( 'AI Readiness Score', 'micropedia-ai-search-optimizer' ), ! empty( $options['enable_ai_score'] ), admin_url( 'admin.php?page=' . self::SLUG_SCORE ), true ); ?>
			<?php self::feature_card( 'llms.txt', ! empty( $options['enable_llms'] ), home_url( '/llms.txt' ), false ); ?>
			<?php self::feature_card( 'llms-full.txt', ! empty( $options['enable_llms_full'] ), home_url( '/llms-full.txt' ), true ); ?>
			<?php self::feature_card( __( 'Markdown Export', 'micropedia-ai-search-optimizer' ), ! empty( $options['enable_markdown'] ), admin_url( 'admin.php?page=' . self::SLUG_FILES ), true ); ?>
			<?php self::feature_card( __( 'Crawler Control', 'micropedia-ai-search-optimizer' ), ! empty( $options['enable_crawler_control'] ), admin_url( 'admin.php?page=' . self::SLUG_CRAWLERS ), false ); ?>
			<?php self::feature_card( __( 'JSON-LD Schema', 'micropedia-ai-search-optimizer' ), ! empty( $options['enable_schema'] ), admin_url( 'admin.php?page=' . self::SLUG_SCHEMA ), false ); ?>
		</div>
		<h2><?php echo esc_html__( 'Overview', 'micropedia-ai-search-optimizer' ); ?></h2>
		<div class="micropedia-ai-card"><div class="micropedia-ai-overview"><div><span><?php echo esc_html__( 'Indexed posts', 'micropedia-ai-search-optimizer' ); ?></span><strong><?php echo esc_html( (string) wp_count_posts( 'post' )->publish ); ?></strong></div><div><span><?php echo esc_html__( 'Indexed pages', 'micropedia-ai-search-optimizer' ); ?></span><strong><?php echo esc_html( (string) wp_count_posts( 'page' )->publish ); ?></strong></div><div><span><?php echo esc_html__( 'Last update', 'micropedia-ai-search-optimizer' ); ?></span><strong><?php echo esc_html( date_i18n( get_option( 'date_format' ) ) ); ?></strong></div><div><span><?php echo esc_html__( 'Cache status', 'micropedia-ai-search-optimizer' ); ?></span><strong class="micropedia-ai-active"><?php echo esc_html__( 'Active', 'micropedia-ai-search-optimizer' ); ?></strong></div></div></div>
		<?php
		self::shell_close();
	}

	/** Feature card. */
	private static function feature_card( $title, $enabled, $url, $pro = false ) {
		$is_locked = $pro && ! micropedia_ai_is_pro();
		?>
		<div class="micropedia-ai-card"><h2><?php echo esc_html( $title ); ?><?php if ( $is_locked ) : ?><span class="micropedia-ai-pro">PRO</span><?php endif; ?></h2><div class="micropedia-ai-status"><span class="<?php echo $enabled ? 'micropedia-ai-active' : 'micropedia-ai-disabled'; ?>"><?php echo esc_html( $enabled ? __( 'Active', 'micropedia-ai-search-optimizer' ) : __( 'Disabled', 'micropedia-ai-search-optimizer' ) ); ?></span><a class="micropedia-ai-link" target="<?php echo 0 === strpos( $url, 'http' ) ? '_blank' : '_self'; ?>" href="<?php echo esc_url( $is_locked ? micropedia_ai_get_pro_url() : $url ); ?>"><?php echo esc_html( $is_locked ? __( 'Upgrade to PRO', 'micropedia-ai-search-optimizer' ) : __( 'View', 'micropedia-ai-search-optimizer' ) ); ?> →</a></div></div>
		<?php
	}

	/** Settings page. */
	public static function render_settings() {
		if ( ! self::shell_open( __( 'Settings', 'micropedia-ai-search-optimizer' ) ) ) { return; }
		$options = wp_parse_args( get_option( 'micropedia_ai_options', array() ), micropedia_ai_default_options() );
		?>
		<div class="micropedia-ai-card micropedia-ai-settings-card"><h2><?php echo esc_html__( 'General settings', 'micropedia-ai-search-optimizer' ); ?></h2><form method="post" action="options.php">
		<?php settings_fields( 'micropedia_ai_settings_group' ); ?>
		<?php self::checkbox_field( 'enable_llms', __( 'Enable llms.txt', 'micropedia-ai-search-optimizer' ), $options ); ?>
		<?php self::checkbox_field( 'enable_llms_full', __( 'Enable llms-full.txt', 'micropedia-ai-search-optimizer' ), $options, true ); ?>
		<?php self::checkbox_field( 'enable_markdown', __( 'Enable Markdown exports', 'micropedia-ai-search-optimizer' ), $options, true ); ?>
		<?php self::checkbox_field( 'enable_schema', __( 'Enable JSON-LD when no SEO schema provider is detected', 'micropedia-ai-search-optimizer' ), $options ); ?>
		<?php self::checkbox_field( 'enable_crawler_control', __( 'Enable AI crawler preferences in robots.txt', 'micropedia-ai-search-optimizer' ), $options ); ?>
		<?php self::checkbox_field( 'enable_ai_score', __( 'Enable AI readiness score column', 'micropedia-ai-search-optimizer' ), $options, true ); ?>
		<div class="micropedia-ai-field"><label for="micropedia-ai-plugin-language"><?php echo esc_html__( 'Plugin language', 'micropedia-ai-search-optimizer' ); ?></label><select id="micropedia-ai-plugin-language" name="micropedia_ai_options[plugin_language]"><?php self::language_options( $options['plugin_language'] ); ?></select></div>
		<div class="micropedia-ai-field"><label for="micropedia-ai-posts-limit"><?php echo esc_html__( 'Posts limit', 'micropedia-ai-search-optimizer' ); ?></label><input id="micropedia-ai-posts-limit" type="number" min="1" max="100" name="micropedia_ai_options[posts_limit]" value="<?php echo esc_attr( (string) $options['posts_limit'] ); ?>" /></div>
		<div class="micropedia-ai-footer-actions"><button class="micropedia-ai-button" type="submit"><?php echo esc_html__( 'Save changes', 'micropedia-ai-search-optimizer' ); ?></button></div>
		</form></div>
		<?php
		self::shell_close();
	}

	/** Checkbox field. */
	private static function checkbox_field( $key, $label, $options, $pro = false ) {
		$locked = $pro && ! micropedia_ai_is_pro();
		?>
		<div class="micropedia-ai-field"><label for="micropedia-ai-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?><?php if ( $locked ) : ?><span class="micropedia-ai-pro"><?php echo esc_html__( 'PRO only', 'micropedia-ai-search-optimizer' ); ?></span><?php endif; ?></label><span><input type="hidden" name="micropedia_ai_options[<?php echo esc_attr( $key ); ?>]" value="0" /><input id="micropedia-ai-<?php echo esc_attr( $key ); ?>" type="checkbox" name="micropedia_ai_options[<?php echo esc_attr( $key ); ?>]" value="1" <?php checked( ! empty( $options[ $key ] ) ); ?> <?php disabled( $locked ); ?> /></span></div>
		<?php
	}

	/** Language options. */
	private static function language_options( $selected ) {
		$languages = array( '' => __( 'Use WordPress language', 'micropedia-ai-search-optimizer' ), 'en_US' => 'English', 'it_IT' => 'Italiano', 'fr_FR' => 'Français', 'es_ES' => 'Español' );
		foreach ( $languages as $code => $label ) {
			echo '<option value="' . esc_attr( $code ) . '" ' . selected( $selected, $code, false ) . '>' . esc_html( $label ) . '</option>';
		}
	}

	/** AI files page. */
	public static function render_files() {
		if ( ! self::shell_open( __( 'AI Files', 'micropedia-ai-search-optimizer' ) ) ) { return; }
		?>
		<div class="micropedia-ai-card micropedia-ai-settings-card"><h2><?php echo esc_html__( 'AI-readable endpoints', 'micropedia-ai-search-optimizer' ); ?></h2><div class="micropedia-ai-files-list"><a target="_blank" href="<?php echo esc_url( home_url( '/llms.txt' ) ); ?>">/llms.txt</a><a target="_blank" href="<?php echo esc_url( home_url( '/llms-full.txt' ) ); ?>">/llms-full.txt <span class="micropedia-ai-pro">PRO</span></a><a href="<?php echo esc_url( micropedia_ai_get_pro_url() ); ?>" target="_blank">/micropedia-ai-md/post-slug.md <span class="micropedia-ai-pro">PRO</span></a></div><p class="micropedia-ai-muted"><?php echo esc_html__( 'Markdown and full-content endpoints are available in PRO.', 'micropedia-ai-search-optimizer' ); ?></p></div>
		<?php self::shell_close();
	}

	/** Crawlers page. */
	public static function render_crawlers() {
		if ( ! self::shell_open( __( 'Crawler Control', 'micropedia-ai-search-optimizer' ) ) ) { return; }
		$options = wp_parse_args( get_option( 'micropedia_ai_options', array() ), micropedia_ai_default_options() );
		?>
		<div class="micropedia-ai-card"><h2><?php echo esc_html__( 'Manage AI crawler preferences through robots.txt', 'micropedia-ai-search-optimizer' ); ?></h2><form method="post" action="options.php"><?php settings_fields( 'micropedia_ai_settings_group' ); ?><input type="hidden" name="micropedia_ai_options[enable_crawler_control]" value="1" /><table class="micropedia-ai-table"><thead><tr><th><?php echo esc_html__( 'AI crawler', 'micropedia-ai-search-optimizer' ); ?></th><th><?php echo esc_html__( 'Status', 'micropedia-ai-search-optimizer' ); ?></th><th><?php echo esc_html__( 'Action', 'micropedia-ai-search-optimizer' ); ?></th></tr></thead><tbody><?php self::crawler_row( 'GPTBot', 'crawler_gptbot', $options ); self::crawler_row( 'OAI-SearchBot', 'crawler_oai_searchbot', $options ); self::crawler_row( 'Google-Extended', 'crawler_google_extended', $options ); self::crawler_row( 'PerplexityBot', 'crawler_perplexitybot', $options ); self::crawler_row( 'ClaudeBot', 'crawler_claudebot', $options ); ?></tbody></table><div class="micropedia-ai-footer-actions"><button class="micropedia-ai-button" type="submit"><?php echo esc_html__( 'Save changes', 'micropedia-ai-search-optimizer' ); ?></button></div></form></div>
		<?php self::shell_close();
	}

	/** Crawler row. */
	private static function crawler_row( $label, $key, $options ) {
		$value = isset( $options[ $key ] ) ? $options[ $key ] : 'neutral';
		$status_class = 'allow' === $value ? 'micropedia-ai-active' : 'micropedia-ai-disabled';
		?>
		<tr><td><?php echo esc_html( $label ); ?></td><td><strong class="<?php echo esc_attr( $status_class ); ?>"><?php echo esc_html( __( ucfirst( $value ), 'micropedia-ai-search-optimizer' ) ); ?></strong></td><td><select name="micropedia_ai_options[<?php echo esc_attr( $key ); ?>]"><?php foreach ( array( 'neutral' => __( 'Neutral', 'micropedia-ai-search-optimizer' ), 'allow' => __( 'Allow', 'micropedia-ai-search-optimizer' ), 'disallow' => __( 'Disallow', 'micropedia-ai-search-optimizer' ) ) as $option_value => $text ) : ?><option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $value, $option_value ); ?>><?php echo esc_html( $text ); ?></option><?php endforeach; ?></select></td></tr>
		<?php
	}

	/** Schema page. */
	public static function render_schema() {
		if ( ! self::shell_open( __( 'Schema', 'micropedia-ai-search-optimizer' ) ) ) { return; }
		$has_provider = Micropedia_AI_Compatibility::has_seo_schema_provider();
		$options      = wp_parse_args( get_option( 'micropedia_ai_options', array() ), micropedia_ai_default_options() );
		$types        = array( 'Article', 'Organization', 'Person' );
		?>
		<div class="micropedia-ai-card micropedia-ai-settings-card">
			<h2><?php echo esc_html__( 'JSON-LD Schema', 'micropedia-ai-search-optimizer' ); ?></h2>
			<?php if ( $has_provider && ! micropedia_ai_is_pro() ) : ?><div class="micropedia-ai-notice"><?php echo esc_html__( 'A SEO schema provider has been detected. Micropedia will not duplicate schema markup.', 'micropedia-ai-search-optimizer' ); ?></div><?php endif; ?>
			<p><?php echo esc_html__( 'FREE includes editable base schema for Article, Organization and Person. PRO unlocks every schema.org type, nested entities and advanced JSON-LD graphs.', 'micropedia-ai-search-optimizer' ); ?></p>
			<form method="post" action="options.php">
				<?php settings_fields( 'micropedia_ai_settings_group' ); ?>
				<input type="hidden" name="micropedia_ai_options[enable_schema]" value="1" />
				<div class="micropedia-ai-field"><label><?php echo esc_html__( 'Base schema type', 'micropedia-ai-search-optimizer' ); ?></label><select name="micropedia_ai_options[schema_base_type]"><?php foreach ( $types as $type ) : ?><option value="<?php echo esc_attr( $type ); ?>" <?php selected( $options['schema_base_type'], $type ); ?>><?php echo esc_html( $type ); ?></option><?php endforeach; ?></select></div>
				<div class="micropedia-ai-field"><label><?php echo esc_html__( 'Name', 'micropedia-ai-search-optimizer' ); ?></label><input class="regular-text" name="micropedia_ai_options[schema_base_name]" value="<?php echo esc_attr( $options['schema_base_name'] ); ?>" /></div>
				<div class="micropedia-ai-field"><label>@id</label><input class="regular-text" name="micropedia_ai_options[schema_base_id]" value="<?php echo esc_attr( $options['schema_base_id'] ); ?>" /></div>
				<div class="micropedia-ai-field"><label>URL</label><input class="regular-text" name="micropedia_ai_options[schema_base_url]" value="<?php echo esc_attr( $options['schema_base_url'] ); ?>" /></div>
				<div class="micropedia-ai-field"><label><?php echo esc_html__( 'Description', 'micropedia-ai-search-optimizer' ); ?></label><textarea class="large-text" rows="3" name="micropedia_ai_options[schema_base_description]"><?php echo esc_textarea( $options['schema_base_description'] ); ?></textarea></div>
				<div class="micropedia-ai-field"><label><?php echo esc_html__( 'Image / logo URL', 'micropedia-ai-search-optimizer' ); ?></label><input class="regular-text" name="micropedia_ai_options[schema_base_image]" value="<?php echo esc_attr( $options['schema_base_image'] ); ?>" /></div>
				<div class="micropedia-ai-field"><label>sameAs</label><textarea class="large-text" rows="4" name="micropedia_ai_options[schema_base_same_as]"><?php echo esc_textarea( $options['schema_base_same_as'] ); ?></textarea><p class="description"><?php echo esc_html__( 'One URL per line.', 'micropedia-ai-search-optimizer' ); ?></p></div>
				<div class="micropedia-ai-footer-actions"><button class="micropedia-ai-button" type="submit"><?php echo esc_html__( 'Save basic schema', 'micropedia-ai-search-optimizer' ); ?></button><?php if ( ! micropedia_ai_is_pro() ) : ?><a class="micropedia-ai-button micropedia-ai-button-secondary" target="_blank" rel="noopener" href="<?php echo esc_url( micropedia_ai_get_pro_url() ); ?>"><?php echo esc_html__( 'Unlock advanced Schema Builder PRO', 'micropedia-ai-search-optimizer' ); ?></a><?php endif; ?></div>
			</form>
		</div>
		<?php self::shell_close();
	}

	/** Score page. */
	public static function render_score() {
		if ( ! self::shell_open( __( 'AI Readiness Score', 'micropedia-ai-search-optimizer' ) ) ) { return; }
		?>
		<div class="micropedia-ai-grid micropedia-ai-score-grid">
			<div class="micropedia-ai-card micropedia-ai-scorebox">
				<h2><?php echo esc_html__( 'Your AI Readiness Score', 'micropedia-ai-search-optimizer' ); ?></h2>
				<div class="micropedia-ai-score-number">78</div>
				<div class="micropedia-ai-score-label"><?php echo esc_html__( 'Good', 'micropedia-ai-search-optimizer' ); ?></div>
				<p class="micropedia-ai-muted"><?php echo esc_html__( 'Your content is well-optimized for AI systems.', 'micropedia-ai-search-optimizer' ); ?></p>
			</div>
			<div class="micropedia-ai-card">
				<h2><?php echo esc_html__( 'Checks', 'micropedia-ai-search-optimizer' ); ?> <span class="micropedia-ai-pro">PRO</span></h2>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'H1 Tag', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Found', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'H2 Structure', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Good', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'Content Length', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Good', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'Author Information', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Found', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'Last Updated', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Recent', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
				<div class="micropedia-ai-check micropedia-ai-warning"><span><?php echo esc_html__( 'Schema Markup', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Missing', 'micropedia-ai-search-optimizer' ); ?> ⚠</b></div>
				<div class="micropedia-ai-check"><span><?php echo esc_html__( 'Internal Links', 'micropedia-ai-search-optimizer' ); ?></span><b><?php echo esc_html__( 'Good', 'micropedia-ai-search-optimizer' ); ?> ✓</b></div>
			</div>
		</div>
		<div class="micropedia-ai-card">
			<h2><?php echo esc_html__( 'PRO analysis', 'micropedia-ai-search-optimizer' ); ?></h2>
			<p><?php echo esc_html__( 'Upgrade to unlock per-post issue analysis, advanced AI score checks, content recommendations and Markdown/full endpoint generation.', 'micropedia-ai-search-optimizer' ); ?></p>
			<a class="micropedia-ai-button" href="<?php echo esc_url( micropedia_ai_get_pro_url() ); ?>" target="_blank" rel="noopener"><?php echo esc_html__( 'Upgrade to PRO', 'micropedia-ai-search-optimizer' ); ?></a>
		</div>
		<?php self::shell_close();
	}
}
