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

if ( isset( $_POST['zs_seo_save_settings'] ) && check_admin_referer( 'zs_seo_settings' ) ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__('Unauthorized user.', 'zs-seo-audit-optimization') );
    }
    update_option( 'zs_seo_schema_enabled',  isset( $_POST['schema_enabled'] )  ? 1 : 0 );
    update_option( 'zs_seo_geo_enabled',     isset( $_POST['geo_enabled'] )     ? 1 : 0 );
    update_option( 'zs_seo_auto_meta',       isset( $_POST['auto_meta'] )       ? 1 : 0 );
    update_option( 'zs_seo_sitemap_enabled', isset( $_POST['sitemap_enabled'] ) ? 1 : 0 );
    echo '<div class="notice notice-success"><p>' . esc_html__('Settings saved!', 'zs-seo-audit-optimization') . '</p></div>';
}

$schema_enabled  = get_option( 'zs_seo_schema_enabled',  true );
$geo_enabled     = get_option( 'zs_seo_geo_enabled',     true );
$auto_meta       = get_option( 'zs_seo_auto_meta',       true );
$sitemap_enabled = get_option( 'zs_seo_sitemap_enabled', true );
?>

<div class="wrap zs-seo-wrap">
    <div class="zs-seo-header">
        <h1>⚙️ <?php esc_html_e('Settings', 'zs-seo-audit-optimization'); ?></h1>
    </div>

    <form method="post" action="">
        <?php wp_nonce_field( 'zs_seo_settings' ); ?>

        <div class="zs-seo-box">
            <h2><?php esc_html_e('General', 'zs-seo-audit-optimization'); ?></h2>
            <table class="form-table">
                <tr>
                    <th><?php esc_html_e('Auto Meta Tags', 'zs-seo-audit-optimization'); ?></th>
                    <td>
                        <label>
                            <input type="checkbox" name="auto_meta" value="1" <?php checked( $auto_meta, 1 ); ?> />
                            <?php esc_html_e('Auto-inject meta description, robots, canonical on all pages', 'zs-seo-audit-optimization'); ?>
                        </label>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e('Schema Markup', 'zs-seo-audit-optimization'); ?></th>
                    <td>
                        <label>
                            <input type="checkbox" name="schema_enabled" value="1" <?php checked( $schema_enabled, 1 ); ?> />
                            <?php esc_html_e('Enable JSON-LD schema (Article, WebSite, Organization, Breadcrumb)', 'zs-seo-audit-optimization'); ?>
                        </label>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e('GEO / AI Search', 'zs-seo-audit-optimization'); ?></th>
                    <td>
                        <label>
                            <input type="checkbox" name="geo_enabled" value="1" <?php checked( $geo_enabled, 1 ); ?> />
                            <?php esc_html_e('Enable FAQ schema for AI search (ChatGPT, Gemini, Google AI Overview)', 'zs-seo-audit-optimization'); ?>
                        </label>
                    </td>
                </tr>
            </table>
        </div>

        <div class="zs-seo-box">
            <h2><?php esc_html_e('Plugin Info', 'zs-seo-audit-optimization'); ?></h2>
            <table class="form-table">
                <tr>
                    <th><?php esc_html_e('Version', 'zs-seo-audit-optimization'); ?></th>
                    <td><code><?php echo esc_html( ZS_SEO_VERSION ); ?></code></td>
                </tr>
                <tr>
                    <th><?php esc_html_e('WordPress', 'zs-seo-audit-optimization'); ?></th>
                    <td><code><?php echo esc_html( get_bloginfo( 'version' ) ); ?></code></td>
                </tr>
                <tr>
                    <th><?php esc_html_e('PHP', 'zs-seo-audit-optimization'); ?></th>
                    <td><code><?php echo esc_html( PHP_VERSION ); ?></code></td>
                </tr>
            </table>
        </div>

        <?php submit_button( __('Save Settings', 'zs-seo-audit-optimization'), 'primary', 'zs_seo_save_settings' ); ?>
    </form>
</div>
