<?php

namespace MDCE;

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

class SitemapExclusion {

	private const CONFIG_META = '_mdce_hide_sitemap_on_expiry';
	private const ACTIVE_META = '_mdce_hide_sitemap_active';

	public function register(): void {
		add_action( 'mdce_meta_box_fields', [ $this, 'render_meta_box_field' ], 20, 2 );
		add_action( 'mdce_meta_box_save',   [ $this, 'save' ] );
		add_action( 'mdce_post_expired',    [ $this, 'handle_expired' ], 10, 2 );
		add_filter( 'wp_sitemaps_posts_query_args', [ $this, 'filter_query_args' ] );
		add_action( 'admin_init',          [ $this, 'handle_deactivate' ] );
	}

	public function render_meta_box_field( \WP_Post $post, string $current_action ): void {
		if ( get_post_meta( $post->ID, self::ACTIVE_META, true ) ) {
			$deactivate_url = wp_nonce_url(
				admin_url( 'post.php?post=' . $post->ID . '&action=edit&spe_deactivate=sitemap' ),
				'spe_deactivate_sitemap_' . $post->ID
			);
			?>
			<div class="spe-field-row">
				<div class="notice notice-info inline" style="margin:0; padding:6px 12px;">
					<p style="margin:0; font-size:12px;">
						<strong><?php esc_html_e( 'Hidden from XML sitemap', 'monkeydesign-content-expiry' ); ?></strong>
						— <a href="<?php echo esc_url( $deactivate_url ); ?>"><?php esc_html_e( 'Deactivate', 'monkeydesign-content-expiry' ); ?></a>
					</p>
				</div>
			</div>
			<?php
			return;
		}

		$checked = (bool) get_post_meta( $post->ID, self::CONFIG_META, true );
		?>
		<div class="spe-field-row">
			<label class="spe-check-item" style="font-size:12px; color:#50575e;">
				<input type="checkbox" name="spe_hide_sitemap" value="1" <?php checked( $checked ); ?>>
				<?php esc_html_e( 'Hide from XML sitemap when expired', 'monkeydesign-content-expiry' ); ?>
			</label>
		</div>
		<?php
	}

	public function save( int $post_id ): void {
		if ( ! isset( $_POST['spe_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['spe_nonce'] ) ), 'spe_save_expiration' ) ) {
			return;
		}
		if ( isset( $_POST['spe_hide_sitemap'] ) ) {
			update_post_meta( $post_id, self::CONFIG_META, '1' );
			delete_post_meta( $post_id, self::ACTIVE_META );
		} else {
			delete_post_meta( $post_id, self::CONFIG_META );
			delete_post_meta( $post_id, self::ACTIVE_META );
		}
	}

	public function handle_expired( int $post_id, string $action ): void {
		if ( get_post_meta( $post_id, self::CONFIG_META, true ) ) {
			update_post_meta( $post_id, self::ACTIVE_META, '1' );
		}
	}

	public function filter_query_args( array $args ): array {
		$args['meta_query'][] = [
			'relation' => 'OR',
			[ 'key' => self::ACTIVE_META, 'compare' => 'NOT EXISTS' ],
			[ 'key' => self::ACTIVE_META, 'value' => '1', 'compare' => '!=' ],
		];
		return $args;
	}

	public function handle_deactivate(): void {
		if ( ! isset( $_GET['spe_deactivate'], $_GET['post'] ) || 'sitemap' !== $_GET['spe_deactivate'] ) {
			return;
		}

		$post_id = (int) $_GET['post'];
		check_admin_referer( 'spe_deactivate_sitemap_' . $post_id );

		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			wp_die( esc_html__( 'You do not have permission to do this.', 'monkeydesign-content-expiry' ) );
		}

		delete_post_meta( $post_id, self::ACTIVE_META );

		wp_safe_redirect( admin_url( 'post.php?post=' . $post_id . '&action=edit' ) );
		exit;
	}
}
