<?php
/**
 * File Path: includes/admin/class-mbs-review-nudge.php
 *
 * My Book Showroom — WordPress.org Review Request Notices
 *
 * Displays dismissible wp-admin notices at tenure milestones (15 / 45 / 75 /
 * 105 / 285 days) asking authors to leave a review on WordPress.org.
 * Shown to all users (FREE and PRO) — reviews benefit everyone.
 * Dismissal is stored per-user in user_meta.
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

class MBS_Review_Nudge extends MBS_Nudge_Base {

	/** wp_options key written by GC2-1. */
	const INSTALL_KEY = 'mbs_install_timestamp';

	/** user_meta key prefix — milestone slug appended, e.g. 'mbs_review_nudge_dismissed_15d'. */
	const DISMISSED_META_PREFIX = 'mbs_review_nudge_dismissed_';

	/** WordPress.org review submission URL. */
	const REVIEW_URL = 'https://wordpress.org/support/plugin/my-book-showroom/reviews/#new-post';

	/**
	 * Nonce action for the dismiss AJAX request.
	 *
	 * @return string
	 * @since 1.1.0
	 */
	protected function get_nonce_action(): string {
		return 'mbs_dismiss_review_nudge';
	}

	public function __construct() {
		add_action( 'admin_notices',        array( $this, 'maybe_show_notice' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'wp_ajax_mbs_dismiss_review_nudge', array( $this, 'handle_ajax_dismiss' ) );
	}

	public function enqueue_assets(): void {
		wp_enqueue_script(
			'mbs-review-nudge',
			plugins_url( 'assets/js/adminjs/review-nudge.js', MBS_DIR . 'bookshowroom.php' ),
			array( 'jquery' ),
			MBS_VERSION,
			true
		);
	}

	/**
	 * Milestone definitions — slug ⇒ days + copy.
	 *
	 * @return array<string, array{days: int, heading: string, body: string}>
	 */
	protected static function milestones(): array {
		return array(
			'15d'  => array(
				'days'    => 15,
				'heading' => __( 'You know how much authors love getting reviews? We do too.', 'my-book-showroom' ),
				'body'    => __( 'If My Book Showroom has been worth your time, a quick review on WordPress.org goes a long way. We read every one.', 'my-book-showroom' ),
			),
			'45d'  => array(
				'days'    => 45,
				'heading' => __( "You're an author — you know what a good review means.", 'my-book-showroom' ),
				'body'    => __( "If My Book Showroom has earned one from you, we'd be grateful. It helps other authors find us and keeps us motivated to build great things.", 'my-book-showroom' ),
			),
			'75d'  => array(
				'days'    => 75,
				'heading' => __( 'Authors live and die by reviews. So do plugins.', 'my-book-showroom' ),
				'body'    => __( 'If My Book Showroom has been useful, a review on WordPress.org would mean the world. Takes less time than writing a dedication page.', 'my-book-showroom' ),
			),
			'105d' => array(
				'days'    => 105,
				'heading' => __( "Three and a half months — you know this plugin well by now.", 'my-book-showroom' ),
				'body'    => __( "A review from someone with your experience carries real weight. If it's earned one, we'd be grateful.", 'my-book-showroom' ),
			),
			'285d' => array(
				'days'    => 285,
				'heading' => __( "Nearly a year with My Book Showroom — you're practically family.", 'my-book-showroom' ),
				'body'    => __( "If the plugin has served you well, a review on WordPress.org would mean a lot. It's the best way to help other authors find us.", 'my-book-showroom' ),
			),
		);
	}

	/**
	 * Show the highest applicable milestone notice if all guards pass.
	 */
	public function maybe_show_notice(): void {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// Wait until onboarding is complete.
		if ( get_option( 'mbs_setup_wizard_dismissed' ) !== '1' ) {
			return;
		}

		$install_ts = (int) get_option( self::INSTALL_KEY );
		if ( $install_ts <= 0 ) {
			return;
		}

		$days_active = (int) floor( ( time() - $install_ts ) / DAY_IN_SECONDS );
		$milestones  = array_reverse( self::milestones(), true );

		foreach ( $milestones as $slug => $milestone ) {
			if ( $days_active < $milestone['days'] ) {
				continue;
			}
			if ( get_user_meta( get_current_user_id(), self::DISMISSED_META_PREFIX . $slug, true ) ) {
				continue;
			}
			$this->render_notice( $slug, $milestone );
			return;
		}
	}

	/**
	 * Render a single review request notice.
	 *
	 * @param string                                     $slug      Milestone slug, e.g. '15d'.
	 * @param array{days:int,heading:string,body:string} $milestone Milestone definition.
	 */
	private function render_notice( string $slug, array $milestone ): void {
		$nonce = wp_create_nonce( 'mbs_dismiss_review_nudge' );
		?>
		<div class="notice notice-success is-dismissible mbs-review-nudge"
		     id="mbs-review-nudge-<?php echo esc_attr( $slug ); ?>"
		     data-nonce="<?php echo esc_attr( $nonce ); ?>"
		     data-milestone="<?php echo esc_attr( $slug ); ?>"
		     style="padding: 12px 16px;">
			<p>
				<strong><?php echo esc_html( $milestone['heading'] ); ?></strong>
			</p>
			<p><?php echo esc_html( $milestone['body'] ); ?></p>
			<p>
				<a href="<?php echo esc_url( self::REVIEW_URL ); ?>"
				   class="button button-primary"
				   target="_blank"
				   rel="noopener noreferrer">
					<?php esc_html_e( 'Leave a Review', 'my-book-showroom' ); ?>
				</a>
				<button type="button"
				        class="button button-secondary mbs-nudge-dismiss-btn"
				        style="margin-left: 8px;">
					<?php esc_html_e( 'Maybe later', 'my-book-showroom' ); ?>
				</button>
			</p>
		</div>
		<?php
	}

}
