<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class SS_Schema_Preview {

    public static function init() {
        add_action('add_meta_boxes', [self::class, 'register_meta_box']);
    }

    public static function register_meta_box() {

        $post_types = ['post', 'page', 'ss_glossary', 'ss_qa'];

        foreach ($post_types as $pt) {
            add_meta_box(
                'ss_universal_schema_preview',
                'AI Preview (What Models See)',
                [self::class, 'render'],
                $pt,
                'side',
                'high'
            );
        }
    }

    public static function render($post) {

        $schema = get_post_meta($post->ID, '_ss_schema_json', true);

        echo '<p><strong>Generated Schema:</strong></p>';

        echo '<textarea readonly rows="14" style="width:100%;font-family:monospace;background:#fafafa;border:1px solid #ccc;padding:10px;">';

        if ($schema) {
            echo esc_textarea($schema);
        } else {
            echo "⚠️ Schema not generated yet.\n\n";
            echo "👉 Please refresh after publishing.\n";
        }

        echo '</textarea>';

        echo '<p style="font-size:12px;color:#777;">
            Preview shows structure, not AI results.
        </p>';
    }
}