<?php
// phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals
 defined( 'ABSPATH' ) || exit; ?>

<div class="zs-seo-metabox">

    <!-- Live Audit Button -->
    <div class="zs-seo-audit-bar">
        <button type="button" id="zs-seo-run-audit" class="button button-primary" data-post-id="<?php echo esc_attr( $post->ID ); ?>">
            🔍 <?php esc_html_e('Run SEO Audit', 'zs-seo-audit-optimization'); ?>
        </button>
        <span id="zs-seo-score-badge" class="zs-seo-badge" style="display:none;"></span>
    </div>
    <div id="zs-seo-audit-results" style="margin-top:12px;"></div>

    <hr>

    <!-- On-Page SEO -->
    <div class="zs-seo-section">
        <h3>📄 <?php esc_html_e('On-Page SEO', 'zs-seo-audit-optimization'); ?></h3>

        <div class="zs-seo-field">
            <label for="zs_seo_title"><strong><?php esc_html_e('SEO Title', 'zs-seo-audit-optimization'); ?></strong></label>
            <input type="text" id="zs_seo_title" name="zs_seo_title"
                   value="<?php echo esc_attr( $title ); ?>"
                   placeholder="<?php echo esc_attr( get_the_title( $post->ID ) ); ?>"
                   class="widefat" maxlength="60" />
            <p class="description"><?php esc_html_e('Optimal: 50–60 characters.', 'zs-seo-audit-optimization'); ?> <span id="zs-seo-title-count">0</span>/60</p>
        </div>

        <div class="zs-seo-field">
            <label for="zs_seo_description"><strong><?php esc_html_e('Meta Description', 'zs-seo-audit-optimization'); ?></strong></label>
            <textarea id="zs_seo_description" name="zs_seo_description" class="widefat" rows="3" maxlength="160"><?php echo esc_textarea( $desc ); ?></textarea>
            <p class="description"><?php esc_html_e('Optimal: 150–160 characters.', 'zs-seo-audit-optimization'); ?> <span id="zs-seo-desc-count">0</span>/160</p>
        </div>

        <div class="zs-seo-field">
            <label for="zs_seo_canonical"><strong><?php esc_html_e('Canonical URL', 'zs-seo-audit-optimization'); ?></strong></label>
            <input type="url" id="zs_seo_canonical" name="zs_seo_canonical"
                   value="<?php echo esc_url( $canonical ); ?>"
                   placeholder="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"
                   class="widefat" />
            <p class="description"><?php esc_html_e('Leave blank to use default permalink.', 'zs-seo-audit-optimization'); ?></p>
        </div>

        <div class="zs-seo-field zs-seo-inline">
            <label>
                <input type="checkbox" name="zs_seo_noindex" value="1" <?php checked( $noindex, 1 ); ?> />
                <?php esc_html_e('No Index (hide from search engines)', 'zs-seo-audit-optimization'); ?>
            </label>
            &nbsp;&nbsp;
            <label>
                <input type="checkbox" name="zs_seo_nofollow" value="1" <?php checked( $nofollow, 1 ); ?> />
                <?php esc_html_e('No Follow (don\'t follow links)', 'zs-seo-audit-optimization'); ?>
            </label>
        </div>
    </div>

    <hr>

    <!-- GEO / FAQ Schema -->
    <div class="zs-seo-section">
        <h3>🤖 <?php esc_html_e('GEO — AI Search (FAQ Schema)', 'zs-seo-audit-optimization'); ?></h3>
        <p class="description"><?php esc_html_e('FAQs generate JSON-LD schema to help ChatGPT, Gemini, and Google AI Overview cite your page.', 'zs-seo-audit-optimization'); ?></p>

        <div id="zs-seo-faq-list">
            <?php foreach ( $faqs as $i => $faq ) : ?>
            <div class="zs-seo-faq-row" data-index="<?php echo esc_attr( $i ); ?>">
                <input type="text" name="zs_seo_faq_question[]"
                       value="<?php echo esc_attr( $faq['question'] ); ?>"
                       placeholder="<?php esc_attr_e('Question', 'zs-seo-audit-optimization'); ?>"
                       class="widefat" style="margin-bottom:5px;" />
                <textarea name="zs_seo_faq_answer[]" class="widefat" rows="2"
                          placeholder="<?php esc_attr_e('Answer', 'zs-seo-audit-optimization'); ?>"><?php echo esc_textarea( $faq['answer'] ); ?></textarea>
                <button type="button" class="button zs-seo-remove-faq" style="margin-top:4px;">
                    ✕ <?php esc_html_e('Remove', 'zs-seo-audit-optimization'); ?>
                </button>
                <hr style="border-color:#eee;">
            </div>
            <?php endforeach; ?>
        </div>

        <button type="button" id="zs-seo-add-faq" class="button">
            + <?php esc_html_e('Add FAQ', 'zs-seo-audit-optimization'); ?>
        </button>
    </div>

</div>
