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

$enabled    = (bool) Seoforge_Settings::get( 'sitemap_enabled', true );
$post_types = (array) Seoforge_Settings::get( 'sitemap_include_post_types', array( 'post', 'page' ) );
$taxonomies = (array) Seoforge_Settings::get( 'sitemap_include_taxonomies', array( 'category', 'post_tag' ) );
$max_chunk  = (int) Seoforge_Settings::get( 'sitemap_max_per_chunk', 1000 );

$all_pt = get_post_types( array( 'public' => true ), 'objects' );
$all_tx = get_taxonomies( array( 'public' => true ), 'objects' );
$keys   = array( 'sitemap_enabled', 'sitemap_include_post_types', 'sitemap_include_taxonomies', 'sitemap_max_per_chunk' );
?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'XML Sitemap', 'brainwerk-seo-suite' ); ?></h2>
    <p>
        <?php
        printf(
            /* translators: %s sitemap URL */
            esc_html__( 'Sitemap URL: %s', 'brainwerk-seo-suite' ),
            '<code><a href="' . esc_url( home_url( '/seoforge-sitemap.xml' ) ) . '" target="_blank">' . esc_html( home_url( '/seoforge-sitemap.xml' ) ) . '</a></code>'
        );
        ?>
    </p>
    <p class="description">
        <?php esc_html_e( 'Submit this URL to Google Search Console and Bing Webmaster Tools to speed up indexing.', 'brainwerk-seo-suite' ); ?>
    </p>

    <form method="post" action="">
        <?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( 'Enable sitemap', 'brainwerk-seo-suite' ); ?></th>
                <td><label class="seoforge-switch">
                    <input type="checkbox" name="seoforge[sitemap_enabled]" value="1" <?php checked( $enabled ); ?>>
                    <span></span></label></td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Include post types', 'brainwerk-seo-suite' ); ?></th>
                <td>
                    <?php foreach ( $all_pt as $slug => $obj ) : ?>
                        <label style="display:inline-block;margin-right:18px;">
                            <input type="checkbox" name="seoforge[sitemap_include_post_types][]" value="<?php echo esc_attr( $slug ); ?>"
                                <?php checked( in_array( $slug, $post_types, true ) ); ?>>
                            <?php echo esc_html( $obj->labels->singular_name ); ?>
                            <small>(<?php echo esc_html( $slug ); ?>)</small>
                        </label>
                    <?php endforeach; ?>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'Include taxonomies', 'brainwerk-seo-suite' ); ?></th>
                <td>
                    <?php foreach ( $all_tx as $slug => $obj ) : ?>
                        <label style="display:inline-block;margin-right:18px;">
                            <input type="checkbox" name="seoforge[sitemap_include_taxonomies][]" value="<?php echo esc_attr( $slug ); ?>"
                                <?php checked( in_array( $slug, $taxonomies, true ) ); ?>>
                            <?php echo esc_html( $obj->labels->singular_name ); ?>
                            <small>(<?php echo esc_html( $slug ); ?>)</small>
                        </label>
                    <?php endforeach; ?>
                </td>
            </tr>
            <tr>
                <th><?php esc_html_e( 'URLs per file', 'brainwerk-seo-suite' ); ?></th>
                <td><input type="number" name="seoforge[sitemap_max_per_chunk]" value="<?php echo esc_attr( $max_chunk ); ?>" min="50" max="50000" step="50" class="small-text"></td>
            </tr>
        </table>
        <?php submit_button( __( 'Save sitemap settings', 'brainwerk-seo-suite' ) ); ?>
    </form>
</div>
