<?php
/**
 * Freshness Reminder — nudge developers to update readme.txt regularly.
 *
 * Self-registering: instantiate once and all hooks are set up automatically.
 * Does NOT modify any existing functionality — purely additive.
 *
 * Uses a lightweight check: if wahrb_last_svn_update was set more than 90 days
 * ago (or was never set), show an admin-only notice reminding to bump
 * "Stable tag" and "Tested up to" in readme.txt so the WP.org freshness
 * decay multiplier stays close to 1.0.
 *
 * @package WahraRentalsBookings
 * @since   2.0.6
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class WAHRB_Freshness_Reminder
 */
class WAHRB_Freshness_Reminder {

	/**
	 * Number of days before the reminder fires.
	 *
	 * @var int
	 */
	private int $days_threshold = 90;

	/**
	 * Constructor — register hooks.
	 */
	public function __construct() {
		// Record initial timestamp on first load.
		if ( false === get_option( 'wahrb_last_svn_update' ) ) {
			update_option( 'wahrb_last_svn_update', time(), true );
		}

		add_action( 'admin_notices', array( $this, 'maybe_show_freshness_notice' ) );
		add_action( 'wp_ajax_wahrb_dismiss_freshness', array( $this, 'ajax_dismiss_freshness' ) );
		add_action( 'admin_footer', array( $this, 'dismiss_script' ) );
	}

	/**
	 * Show the freshness reminder if 90+ days have elapsed.
	 */
	public function maybe_show_freshness_notice(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$last_update = (int) get_option( 'wahrb_last_svn_update', 0 );
		if ( $last_update <= 0 ) {
			return;
		}

		$elapsed_days = (int) floor( ( time() - $last_update ) / DAY_IN_SECONDS );
		if ( $elapsed_days < $this->days_threshold ) {
			return;
		}

		printf(
			'<div class="notice notice-warning is-dismissible wahrb-freshness-notice" data-nonce="%1$s">
				<p><strong>%2$s</strong></p>
				<p>%3$s</p>
				<p>
					<a href="#" class="button wahrb-dismiss-freshness">%4$s</a>
				</p>
			</div>',
			esc_attr( wp_create_nonce( 'wahrb_dismiss_freshness' ) ),
			esc_html__( 'Rentals & Bookings — Freshness Reminder', 'wahra-rentals-bookings' ),
			sprintf(
				/* translators: %d: Number of days since last SVN update */
				esc_html__( 'It has been %d days since the last readme.txt update. Update the "Stable tag" and "Tested up to" fields in readme.txt, then commit to SVN to maintain your WordPress.org search ranking.', 'wahra-rentals-bookings' ),
				$elapsed_days
			),
			esc_html__( 'Done — I just updated SVN (reset timer)', 'wahra-rentals-bookings' )
		);
	}

	/**
	 * AJAX handler — reset the freshness timer.
	 */
	public function ajax_dismiss_freshness(): void {
		check_ajax_referer( 'wahrb_dismiss_freshness', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( 'Unauthorized', 403 );
		}

		update_option( 'wahrb_last_svn_update', time(), true );
		wp_send_json_success();
	}

	/**
	 * Print inline JS to handle dismiss clicks.
	 */
	public function dismiss_script(): void {
		?>
		<script>
		(function(){
			document.addEventListener('click', function(e) {
				if (!e.target.classList.contains('wahrb-dismiss-freshness')) return;
				e.preventDefault();
				var wrap = e.target.closest('.wahrb-freshness-notice');
				if (!wrap) return;
				var nonce = wrap.getAttribute('data-nonce');
				var xhr = new XMLHttpRequest();
				xhr.open('POST', ajaxurl);
				xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xhr.send('action=wahrb_dismiss_freshness&nonce=' + encodeURIComponent(nonce));
				wrap.style.display = 'none';
			});
		})();
		</script>
		<?php
	}
}
