<?php
/**
 * Livada SEO — Sitemap Settings View
 *
 * @package Livada_SEO
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$sitemap_post_types = (array) Livada_SEO_Loader::get_setting( 'sitemap_post_types', [ 'post', 'page' ] );
$sitemap_taxonomies = (array) Livada_SEO_Loader::get_setting( 'sitemap_taxonomies', [ 'category' ] );
$sitemap_max_urls   = (int)   Livada_SEO_Loader::get_setting( 'sitemap_max_urls', 1000 );

$news_post_types  = (array)  Livada_SEO_Loader::get_setting( 'sitemap_news_post_types', [ 'post' ] );
$news_pub_name    = (string) Livada_SEO_Loader::get_setting( 'sitemap_news_publication_name', '' );
$news_language    = (string) Livada_SEO_Loader::get_setting( 'sitemap_news_language', '' );

$post_types = get_post_types( [ 'public' => true ], 'objects' );
$taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );

$sitemap_url      = home_url( '/sitemap.xml' );
$sitemap_news_url = home_url( '/sitemap-news.xml' );
?>
<div class="wrap livada-seo-wrap has-ls-hero">
<?php include LIVADA_SEO_PATH . 'includes/admin/views/_partials/header.php'; ?>
	<h1><?php esc_html_e( 'Sitemap', 'livada-seo' ); ?></h1>

	<?php if ( isset( $_GET['updated'] ) ) : // phpcs:ignore ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'livada-seo' ); ?></p></div>
	<?php endif; ?>

	<p>
		<?php esc_html_e( 'Your sitemap URL:', 'livada-seo' ); ?>
		<a href="<?php echo esc_url( $sitemap_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $sitemap_url ); ?></a>
	</p>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<?php wp_nonce_field( 'livada_seo_save_sitemap' ); ?>
		<input type="hidden" name="action" value="livada_seo_save_sitemap">

		<table class="form-table" role="presentation">
			<tbody>

				<tr>
					<th scope="row"><?php esc_html_e( 'Include Post Types', 'livada-seo' ); ?></th>
					<td>
						<fieldset>
							<legend class="screen-reader-text"><?php esc_html_e( 'Choose which post types to include in the sitemap.', 'livada-seo' ); ?></legend>
							<?php foreach ( $post_types as $pt ) : ?>
								<label>
									<input
										type="checkbox"
										name="livada_seo[sitemap_post_types][]"
										value="<?php echo esc_attr( $pt->name ); ?>"
										<?php checked( in_array( $pt->name, $sitemap_post_types, true ) ); ?>
									>
									<?php echo esc_html( $pt->labels->name ); ?>
									<span class="description">(<?php echo esc_html( $pt->name ); ?>)</span>
								</label><br>
							<?php endforeach; ?>
						</fieldset>
					</td>
				</tr>

				<tr>
					<th scope="row"><?php esc_html_e( 'Include Taxonomies', 'livada-seo' ); ?></th>
					<td>
						<fieldset>
							<legend class="screen-reader-text"><?php esc_html_e( 'Choose which taxonomies to include in the sitemap.', 'livada-seo' ); ?></legend>
							<?php foreach ( $taxonomies as $tax ) : ?>
								<label>
									<input
										type="checkbox"
										name="livada_seo[sitemap_taxonomies][]"
										value="<?php echo esc_attr( $tax->name ); ?>"
										<?php checked( in_array( $tax->name, $sitemap_taxonomies, true ) ); ?>
									>
									<?php echo esc_html( $tax->labels->name ); ?>
									<span class="description">(<?php echo esc_html( $tax->name ); ?>)</span>
								</label><br>
							<?php endforeach; ?>
						</fieldset>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="sitemap_max_urls"><?php esc_html_e( 'Max URLs per Sitemap', 'livada-seo' ); ?></label></th>
					<td>
						<input
							type="number"
							id="sitemap_max_urls"
							name="livada_seo[sitemap_max_urls]"
							value="<?php echo esc_attr( $sitemap_max_urls ); ?>"
							min="1"
							max="50000"
							class="small-text"
						>
						<p class="description"><?php esc_html_e( 'Maximum number of URLs per sitemap file. Recommended: 1000–5000.', 'livada-seo' ); ?></p>
					</td>
				</tr>

			</tbody>
		</table>

		<h2 style="margin-top:32px;"><?php esc_html_e( 'Google News Sitemap', 'livada-seo' ); ?></h2>
		<p class="description" style="max-width:780px;">
			<?php esc_html_e( 'Surfaces posts published in the last 48 hours under', 'livada-seo' ); ?>
			<a href="<?php echo esc_url( $sitemap_news_url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $sitemap_news_url ); ?></a>.
			<?php esc_html_e( 'Listed in the sitemap index only when at least one matching post exists, so you can leave it on without polluting the index on quiet days.', 'livada-seo' ); ?>
		</p>

		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'News post types', 'livada-seo' ); ?></th>
					<td>
						<fieldset>
							<legend class="screen-reader-text"><?php esc_html_e( 'Pick the post types eligible for the news sitemap.', 'livada-seo' ); ?></legend>
							<?php foreach ( $post_types as $pt ) : ?>
								<label>
									<input
										type="checkbox"
										name="livada_seo[sitemap_news_post_types][]"
										value="<?php echo esc_attr( $pt->name ); ?>"
										<?php checked( in_array( $pt->name, $news_post_types, true ) ); ?>
									>
									<?php echo esc_html( $pt->labels->name ); ?>
									<span class="description">(<?php echo esc_html( $pt->name ); ?>)</span>
								</label><br>
							<?php endforeach; ?>
						</fieldset>
						<p class="description"><?php esc_html_e( 'Leave empty to disable the news sub-sitemap entirely.', 'livada-seo' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="sitemap_news_publication_name"><?php esc_html_e( 'Publication name', 'livada-seo' ); ?></label></th>
					<td>
						<input
							type="text"
							id="sitemap_news_publication_name"
							name="livada_seo[sitemap_news_publication_name]"
							value="<?php echo esc_attr( $news_pub_name ); ?>"
							class="regular-text"
							placeholder="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"
						>
						<p class="description"><?php esc_html_e( 'Defaults to the WordPress site title.', 'livada-seo' ); ?></p>
					</td>
				</tr>

				<tr>
					<th scope="row"><label for="sitemap_news_language"><?php esc_html_e( 'Language', 'livada-seo' ); ?></label></th>
					<td>
						<input
							type="text"
							id="sitemap_news_language"
							name="livada_seo[sitemap_news_language]"
							value="<?php echo esc_attr( $news_language ); ?>"
							class="small-text"
							maxlength="5"
							placeholder="fr"
						>
						<p class="description"><?php esc_html_e( '2-letter ISO 639-1 code (or 5-character locale). Defaults to your WP locale prefix.', 'livada-seo' ); ?></p>
					</td>
				</tr>
			</tbody>
		</table>

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