<?php
/**
 * Truebe review ask: a one-time dismissible bar inviting a wp.org review,
 * shown once the plugin has proven value. Triggers after 14 days installed
 * OR 50 answered visitor questions, whichever comes first. Dismissal is
 * permanent.
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( class_exists( 'Truebe_Review_Ask' ) ) { return; }

class Truebe_Review_Ask {

	const OPT_INSTALLED = 'truebe_installed_at';
	const OPT_ANSWERED  = 'truebe_answered_count';
	const OPT_DISMISSED = 'truebe_review_dismissed';

	const DAYS_THRESHOLD    = 14;
	const ANSWERS_THRESHOLD = 50;

	const REVIEW_URL = 'https://wordpress.org/support/plugin/truebe-fact-based-ai-chatbot/reviews/#new-post';

	public static function init() {
		if ( false === get_option( self::OPT_INSTALLED, false ) ) {
			add_option( self::OPT_INSTALLED, time(), '', false );
		}
		add_action( 'admin_notices', array( __CLASS__, 'maybe_show_notice' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
		add_action( 'wp_ajax_truebe_dismiss_review', array( __CLASS__, 'ajax_dismiss' ) );
	}

	/** Call when the public chatbot serves an answered question. */
	public static function count_answer() {
		$count = (int) get_option( self::OPT_ANSWERED, 0 );
		update_option( self::OPT_ANSWERED, $count + 1, false );
	}

	private static function should_show() {
		if ( get_option( self::OPT_DISMISSED, false ) ) { return false; }
		$installed = (int) get_option( self::OPT_INSTALLED, time() );
		$answered  = (int) get_option( self::OPT_ANSWERED, 0 );
		$days_in   = ( time() - $installed ) / DAY_IN_SECONDS;
		return ( $days_in >= self::DAYS_THRESHOLD || $answered >= self::ANSWERS_THRESHOLD );
	}

	private static function on_plugin_screen() {
		if ( ! function_exists( 'get_current_screen' ) ) { return false; }
		$screen = get_current_screen();
		return ( $screen && false !== strpos( (string) $screen->id, 'truebe' ) );
	}

	public static function enqueue_assets() {
		if ( ! current_user_can( 'manage_options' ) ) { return; }
		if ( ! self::on_plugin_screen() ) { return; }
		if ( ! self::should_show() ) { return; }

		wp_enqueue_script(
			'truebe-review-ask',
			plugins_url( '../assets/review-ask.js', __FILE__ ),
			array(),
			(string) filemtime( plugin_dir_path( __FILE__ ) . '../assets/review-ask.js' ),
			true
		);
		wp_localize_script(
			'truebe-review-ask',
			'truebeReviewAsk',
			array(
				'ajax'  => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
				'nonce' => wp_create_nonce( 'truebe_dismiss_review' ),
			)
		);
	}

	public static function maybe_show_notice() {
		if ( ! current_user_can( 'manage_options' ) ) { return; }
		if ( ! self::on_plugin_screen() ) { return; }
		if ( ! self::should_show() ) { return; }

		$answered = (int) get_option( self::OPT_ANSWERED, 0 );
		if ( $answered >= self::ANSWERS_THRESHOLD ) {
			/* translators: %s: number of answered questions */
			$lead = sprintf( __( 'Truebe has answered %s questions for your visitors.', 'truebe-fact-based-ai-chatbot' ), number_format_i18n( $answered ) );
		} else {
			$lead = __( 'You\'ve been running Truebe for a couple of weeks.', 'truebe-fact-based-ai-chatbot' );
		}
		?>
		<div id="truebe-review-notice" style="background:#0f6e56;color:#fff;border-radius:8px;padding:12px 16px;margin:12px 20px 0 2px;display:flex;align-items:center;justify-content:space-between;gap:16px;font-size:14px;">
			<span>
				<?php echo esc_html( $lead ); ?>
				<a href="<?php echo esc_url( self::REVIEW_URL ); ?>" target="_blank" rel="noopener" style="color:#fff;font-weight:600;text-decoration:underline;"><?php esc_html_e( 'Mind leaving a review?', 'truebe-fact-based-ai-chatbot' ); ?></a>
			</span>
			<button type="button" id="truebe-review-dismiss" aria-label="<?php esc_attr_e( 'Dismiss', 'truebe-fact-based-ai-chatbot' ); ?>" style="border:none;background:none;color:#fff;font-size:18px;line-height:1;cursor:pointer;padding:0;">&times;</button>
		</div>
		<?php
	}

	public static function ajax_dismiss() {
		if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'forbidden', 403 ); }
		check_ajax_referer( 'truebe_dismiss_review' );
		update_option( self::OPT_DISMISSED, 1, '', false );
		wp_send_json_success();
	}
}