<?php
/**
 * Rating Notice handler for SureCookie plugin.
 *
 * Registers a WordPress admin notice asking for a wp.org review after the
 * user completes their first successful cookie scan that discovered cookies.
 *
 * @package    SureCookie
 * @subpackage SureCookie\Admin
 * @since      1.0.0
 */

namespace SureCookie\Admin;

use SureCookie\Inc\Traits\GetInstance;

defined( 'ABSPATH' ) || exit;

/**
 * Rating Notice.
 *
 * Displays a review-request notice via the BSF_Admin_Notices library once the
 * first successful cookie scan has completed. Mirrors the SureForms pattern
 * for voice/tone consistency across the Brainstorm Force suite.
 *
 * @since 1.0.0
 */
class Rating_Notice {
	use GetInstance;

	/**
	 * Unique notice ID used by BSF_Admin_Notices for dismissal tracking.
	 *
	 * @since 1.0.0
	 */
	private const NOTICE_ID = 'surecookie-rating-notice';

	/**
	 * WordPress.org review URL (opens the "leave a 5-star review" form).
	 *
	 * @since 1.0.0
	 */
	private const REVIEW_URL = 'https://wordpress.org/support/plugin/surecookie/reviews/?filter=5#new-post';

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'admin_notices', [ $this, 'display_notice' ] );
	}

	/**
	 * Register the rating notice with BSF_Admin_Notices when the milestone is met.
	 *
	 * Short-circuits (in order) when:
	 *  - the current user lacks {@see SURECOOKIE_CAPABILITY},
	 *  - the `surecookie_show_rating_notice` filter returns false,
	 *  - the first-successful-scan flag hasn't been set yet.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function display_notice(): void {
		if ( ! current_user_can( SURECOOKIE_CAPABILITY ) ) {
			return;
		}

		/**
		 * Allow third parties (agencies, managed hosts) to suppress the
		 * review-request notice entirely.
		 *
		 * @since 1.0.0
		 * @param bool $show Whether to register the notice. Default true.
		 */
		if ( ! apply_filters( 'surecookie_show_rating_notice', true ) ) {
			return;
		}

		$first_scan_ts = get_option( SURECOOKIE_FIRST_SUCCESSFUL_SCAN_OPTION, false );
		if ( empty( $first_scan_ts ) ) {
			return;
		}

		if ( ! class_exists( 'BSF_Admin_Notices' ) ) {
			require_once SURECOOKIE_DIR . 'inc/lib/astra-notices/class-bsf-admin-notices.php';
		}

		\BSF_Admin_Notices::add_notice(
			[
				'id'                         => self::NOTICE_ID,
				'type'                       => '',
				'message'                    => $this->build_message(),
				'repeat-notice-after'        => 2 * WEEK_IN_SECONDS,
				'display-with-other-notices' => true,
				'capability'                 => SURECOOKIE_CAPABILITY,
			]
		);
	}

	/**
	 * Build the HTML markup for the notice body.
	 *
	 * Mirrors SureForms' `build_notice_markup` structure so styling inherits
	 * from the shared BSF_Admin_Notices stylesheet already enqueued by the library.
	 *
	 * The "Rate SureCookie" button carries both `button-primary` and
	 * `astra-notice-close` classes plus `target="_blank"`. The library's
	 * `_dismissNoticeNew` handler detects this combination and fires the
	 * dismiss AJAX + `window.open( …, '_blank' )` in a single action.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	private function build_message(): string {
		$logo_url = SURECOOKIE_URL . 'assets/images/surecookie--brand-colored.svg';
		$snooze   = (string) ( 2 * WEEK_IN_SECONDS );

		ob_start();
		?>
		<div class="notice-image">
			<img
				src="<?php echo esc_url( $logo_url ); ?>"
				class="custom-logo"
				alt="SureCookie"
			/>
		</div>
		<div class="notice-content">
			<div class="notice-heading">
				<?php esc_html_e( 'Amazing! SureCookie discovered cookies on your site — let\'s keep growing together!', 'surecookie' ); ?>
			</div>
			<?php esc_html_e( 'If SureCookie has been helpful, would you mind taking a moment to leave a 5-star review on WordPress.org?', 'surecookie' ); ?>
			<br />
			<div class="astra-review-notice-container">
				<a
					href="<?php echo esc_url( self::REVIEW_URL ); ?>"
					class="button-primary astra-notice-close"
					target="_blank"
					rel="noopener noreferrer"
				>
					<?php esc_html_e( 'Rate SureCookie', 'surecookie' ); ?>
				</a>
				<span class="dashicons dashicons-clock" aria-hidden="true"></span>
				<a
					href="#"
					data-repeat-notice-after="<?php echo esc_attr( $snooze ); ?>"
					class="astra-notice-close"
				>
					<?php esc_html_e( 'Maybe later', 'surecookie' ); ?>
				</a>
				<span class="dashicons dashicons-smiley" aria-hidden="true"></span>
				<a href="#" class="astra-notice-close">
					<?php esc_html_e( 'I already did', 'surecookie' ); ?>
				</a>
			</div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
}
