<?php
/**
 * Meta & Schema settings view.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$keys = array(
    'seo_meta_enabled', 'seo_force_takeover', 'seo_og_enabled', 'seo_twitter_enabled', 'seo_schema_enabled',
    'seo_breadcrumbs_enabled', 'seo_canonical_enabled', 'seo_remove_wp_emoji',
    'seo_clean_head', 'seo_default_twitter_card', 'seo_schema_organization',
    'robots_custom_enabled', 'robots_custom_content',
    'seo_schema_faq_enabled', 'geo_blocked_ai_bots', 'geo_llms_enabled',
);

$org = (array) Seoforge_Settings::get( 'seo_schema_organization', array( 'name' => '', 'url' => '', 'logo' => '' ) );
$competitor = Seoforge_Compat::competitor_label();
?>

<?php if ( $competitor ) : ?>
<div class="seoforge-card" style="border-left:4px solid #f59e0b;">
    <h2><?php
        printf(
            /* translators: %s competitor plugin name */
            esc_html__( 'Detected SEO plugin: %s', 'brainwerk-seo-suite' ),
            '<strong>' . esc_html( $competitor ) . '</strong>'
        );
    ?></h2>
    <p><?php
        printf(
            /* translators: %1$s and %2$s are both the competing SEO plugin name (same value, repeated for grammar in some languages) */
            esc_html__( 'By default Brainwerk SEO Suite stays silent on meta tags, schema and the sitemap when %1$s is active. This avoids duplicate output that hurts SEO. You can override below — but only do that after deactivating %2$s, otherwise both plugins will write tags.', 'brainwerk-seo-suite' ),
            esc_html( $competitor ),
            esc_html( $competitor )
        );
    ?></p>
    <p>
        <label>
            <input type="checkbox" form="seoforge-meta-form" name="seoforge[seo_force_takeover]" value="1"
                <?php checked( Seoforge_Settings::get( 'seo_force_takeover', false ) ); ?>>
            <?php
            printf(
                /* translators: %s competitor plugin name */
                esc_html__( 'Force Brainwerk SEO Suite to take over (ignores %s)', 'brainwerk-seo-suite' ),
                esc_html( $competitor )
            );
            ?>
        </label>
    </p>
</div>
<?php endif; ?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Meta tags & Schema.org', 'brainwerk-seo-suite' ); ?></h2>
    <form method="post" action="" id="seoforge-meta-form">
        <?php wp_nonce_field( Seoforge_Admin::NONCE ); ?>
        <input type="hidden" name="seoforge_action" value="save_settings">
        <?php foreach ( $keys as $k ) : ?>
            <input type="hidden" name="seoforge_tab_keys[]" value="<?php echo esc_attr( $k ); ?>">
        <?php endforeach; ?>

        <table class="form-table">
            <tr><th><?php esc_html_e( 'Manage <title> + meta description', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_meta_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_meta_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Open Graph tags', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_og_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_og_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Twitter Cards', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_twitter_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_twitter_enabled', true ) ); ?>><span></span></label>
                    <select name="seoforge[seo_default_twitter_card]" style="margin-left:12px;">
                        <option value="summary"               <?php selected( Seoforge_Settings::get( 'seo_default_twitter_card' ), 'summary' ); ?>>summary</option>
                        <option value="summary_large_image"   <?php selected( Seoforge_Settings::get( 'seo_default_twitter_card' ), 'summary_large_image' ); ?>>summary_large_image</option>
                    </select></td></tr>
            <tr><th><?php esc_html_e( 'JSON-LD Schema (Article + Org + Breadcrumbs)', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_schema_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_schema_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Breadcrumbs in Schema', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_breadcrumbs_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_breadcrumbs_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'FAQ schema (FAQPage)', 'brainwerk-seo-suite' ); ?> 🤖</th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_schema_faq_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_schema_faq_enabled', true ) ); ?>><span></span></label>
                    <p class="description"><?php esc_html_e( 'Outputs FAQPage JSON-LD from the per-post FAQ fields (in the post editor) — great for AI answers and rich results.', 'brainwerk-seo-suite' ); ?></p>
                </td></tr>
            <tr><th><?php esc_html_e( 'Canonical URLs', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[seo_canonical_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_canonical_enabled', true ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Remove WP emoji & clean &lt;head&gt;', 'brainwerk-seo-suite' ); ?></th>
                <td><label><input type="checkbox" name="seoforge[seo_remove_wp_emoji]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_remove_wp_emoji', true ) ); ?>>
                    <?php esc_html_e( 'Remove emoji scripts', 'brainwerk-seo-suite' ); ?></label><br>
                    <label><input type="checkbox" name="seoforge[seo_clean_head]" value="1"
                    <?php checked( Seoforge_Settings::get( 'seo_clean_head', true ) ); ?>>
                    <?php esc_html_e( 'Remove generator + WLW manifest links', 'brainwerk-seo-suite' ); ?></label></td></tr>
        </table>

        <h3><?php esc_html_e( 'Organization (used in JSON-LD)', 'brainwerk-seo-suite' ); ?></h3>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Name', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="text" class="regular-text" name="seoforge[seo_schema_organization][name]" value="<?php echo esc_attr( $org['name'] ?? '' ); ?>"></td></tr>
            <tr><th><?php esc_html_e( 'URL', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="url" class="regular-text" name="seoforge[seo_schema_organization][url]" value="<?php echo esc_attr( $org['url'] ?? '' ); ?>"></td></tr>
            <tr><th><?php esc_html_e( 'Logo URL', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="url" class="regular-text" name="seoforge[seo_schema_organization][logo]" value="<?php echo esc_attr( $org['logo'] ?? '' ); ?>"></td></tr>
        </table>

        <h3><?php esc_html_e( 'Custom robots.txt', 'brainwerk-seo-suite' ); ?></h3>
        <p class="description">
            <?php esc_html_e( 'When enabled, the content below is served at /robots.txt. The sitemap line is appended automatically if missing.', 'brainwerk-seo-suite' ); ?>
        </p>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Enable custom robots.txt', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[robots_custom_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'robots_custom_enabled', false ) ); ?>><span></span></label></td></tr>
            <tr><th><?php esc_html_e( 'Content', 'brainwerk-seo-suite' ); ?></th>
                <td><textarea name="seoforge[robots_custom_content]" rows="8" cols="60" class="large-text code"
                    ><?php echo esc_textarea( (string) Seoforge_Settings::get( 'robots_custom_content', '' ) ); ?></textarea></td></tr>
        </table>

        <h3><?php esc_html_e( 'AI & answer engines (GEO)', 'brainwerk-seo-suite' ); ?> 🤖</h3>
        <p class="description">
            <?php esc_html_e( 'Generative Engine Optimization: control how AI crawlers (ChatGPT, Gemini, Claude, Perplexity, …) may use your site, and publish an llms.txt content manifest for them.', 'brainwerk-seo-suite' ); ?>
        </p>
        <table class="form-table">
            <tr><th><?php esc_html_e( 'Publish llms.txt', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch"><input type="checkbox" name="seoforge[geo_llms_enabled]" value="1"
                    <?php checked( Seoforge_Settings::get( 'geo_llms_enabled', false ) ); ?>><span></span></label>
                    <p class="description"><?php
                        printf(
                            /* translators: %s is the llms.txt URL */
                            esc_html__( 'Serves a Markdown content map at %s for AI crawlers.', 'brainwerk-seo-suite' ),
                            '<code>' . esc_html( home_url( '/llms.txt' ) ) . '</code>'
                        );
                    ?></p>
                </td></tr>
            <tr><th><?php esc_html_e( 'Block AI crawlers', 'brainwerk-seo-suite' ); ?></th>
                <td>
                    <p class="description"><?php esc_html_e( 'Checked crawlers get a Disallow rule in robots.txt. Leave all unchecked to allow them (recommended for visibility in AI answers).', 'brainwerk-seo-suite' ); ?></p>
                    <?php
                    $blocked_bots = (array) Seoforge_Settings::get( 'geo_blocked_ai_bots', array() );
                    foreach ( Seoforge_Robots::ai_bots() as $token => $label ) : ?>
                        <label style="display:inline-block; min-width:280px; margin:3px 0;">
                            <input type="checkbox" name="seoforge[geo_blocked_ai_bots][]" value="<?php echo esc_attr( $token ); ?>"
                                <?php checked( in_array( $token, $blocked_bots, true ) ); ?>>
                            <?php echo esc_html( $label ); ?>
                        </label>
                    <?php endforeach; ?>
                </td></tr>
        </table>

        <?php submit_button( __( 'Save', 'brainwerk-seo-suite' ) ); ?>
    </form>
</div>
