<?php
/**
 * Review request notice.
 *
 * @package NaveenCodesImageOptimizer
 */

namespace NaveenCodesImageOptimizer;

defined( 'ABSPATH' ) || exit;

/**
 * Shows a friendly review request after 7 days of use.
 */
class Review_Notice {

	const OPTION = 'ncio_review_notice';
	const NONCE  = 'ncio_review_nonce';
	const AJAX   = 'ncio_dismiss_review';
	const URL    = 'https://wordpress.org/support/plugin/naveencodes-image-optimizer/reviews/#new-post';
	const NAME   = 'Image Optimizer';
	const DAYS   = 7;

	public function __construct() {
		add_action( 'admin_notices', array( $this, 'maybe_show' ) );
		add_action( 'wp_ajax_' . self::AJAX, array( $this, 'ajax_dismiss' ) );
	}

	public function maybe_show() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		if ( get_transient( 'naveencodes_review_shown' ) ) {
			return;
		}

		$data = get_option( self::OPTION, array() );

		if ( empty( $data ) ) {
			update_option( self::OPTION, array( 'status' => 'pending', 'since' => time() ), false );
			return;
		}
		if ( ! empty( $data['status'] ) && 'done' === $data['status'] ) {
			return;
		}
		if ( ! empty( $data['status'] ) && 'snoozed' === $data['status'] ) {
			if ( ! empty( $data['until'] ) && time() < (int) $data['until'] ) {
				return;
			}
		}
		if ( empty( $data['since'] ) || ( time() - (int) $data['since'] ) < ( self::DAYS * DAY_IN_SECONDS ) ) {
			return;
		}

		set_transient( 'naveencodes_review_shown', 1, DAY_IN_SECONDS );
		$this->render();
	}

	public function ajax_dismiss() {
		check_ajax_referer( self::NONCE, 'nonce' );
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error();
		}
		$action = sanitize_key( isset( $_POST['review_action'] ) ? wp_unslash( $_POST['review_action'] ) : 'snoozed' );
		$old    = get_option( self::OPTION, array() );
		if ( 'done' === $action ) {
			update_option( self::OPTION, array( 'status' => 'done' ), false );
		} else {
			update_option(
				self::OPTION,
				array(
					'status' => 'snoozed',
					'since'  => isset( $old['since'] ) ? $old['since'] : time(),
					'until'  => time() + ( 14 * DAY_IN_SECONDS ),
				),
				false
			);
		}
		wp_send_json_success();
	}

	private function render() {
		$nonce    = esc_js( wp_create_nonce( self::NONCE ) );
		$ajax_url = esc_js( admin_url( 'admin-ajax.php' ) );
		$ajax_act = esc_js( self::AJAX );
		?>
		<div id="ncio-review-notice" class="notice notice-info" style="border-left:4px solid #b8922c;padding:14px 20px">
			<div style="display:flex;align-items:flex-start;gap:14px">
				<div style="font-size:26px;line-height:1.3;flex-shrink:0">&#x2B50;</div>
				<div style="flex:1">
					<p style="margin:0 0 6px;font-size:14px;font-weight:600">
						<?php
						printf(
							/* translators: %s: plugin name */
							esc_html__( 'Enjoying NaveenCodes %s?', 'naveencodes-image-optimizer' ),
							esc_html( self::NAME )
						);
						?>
					</p>
					<p style="margin:0 0 12px;color:#646970;font-size:13px">
						<?php esc_html_e( 'A quick review takes less than a minute and helps this free plugin reach more people who need it.', 'naveencodes-image-optimizer' ); ?>
						&#x1F49B;
					</p>
					<p style="margin:0;display:flex;align-items:center;gap:10px;flex-wrap:wrap">
						<a href="<?php echo esc_url( self::URL ); ?>" target="_blank" rel="noopener noreferrer"
						   class="button button-primary" data-rv="done">
							&#x2B50; <?php esc_html_e( "Yes, I'd love to!", 'naveencodes-image-optimizer' ); ?>
						</a>
						<button type="button" class="button" data-rv="done">
							<?php esc_html_e( 'I already did ✓', 'naveencodes-image-optimizer' ); ?>
						</button>
						<button type="button" class="button-link" data-rv="snoozed" style="color:#646970">
							<?php esc_html_e( 'Remind me later', 'naveencodes-image-optimizer' ); ?>
						</button>
					</p>
				</div>
				<button type="button" class="notice-dismiss" data-rv="snoozed" style="position:static;flex-shrink:0"></button>
			</div>
		</div>
		<script>
		(function($){$(function(){
			$('#ncio-review-notice [data-rv]').on('click',function(){
				var a=$(this).data('rv')||'snoozed';
				$.post('<?php echo $ajax_url; ?>',{action:'<?php echo $ajax_act; ?>',nonce:'<?php echo $nonce; ?>',review_action:a});
				$('#ncio-review-notice').slideUp(200);
			});
		});}(jQuery));
		</script>
		<?php
	}
}
