<?php
/**
 * Upsell prompts shown in Free admin to promote Donatico Pro.
 *
 * Hosts contextual upgrade prompts that appear on specific Donatico admin
 * pages when the Pro add-on is inactive. Per Phase 6B placements in
 * docs/WEEK6-UPSELL-SPEC.md.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

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

/**
 * Renders and manages Pro upgrade prompts in the Free admin.
 */
class UpsellPrompts {

	/**
	 * User meta key for the Donations-list dismissal state.
	 *
	 * @var string
	 */
	private const DONATIONS_DISMISS_META_KEY = 'donatico_dismissed_donations_upgrade_notice';

	/**
	 * Nonce action used by the dismiss AJAX endpoint.
	 *
	 * @var string
	 */
	private const DISMISS_NONCE_ACTION = 'donatico_upsell_dismiss';

	/**
	 * Render the Donations-list upgrade notice.
	 *
	 * Hooked to admin_notices. Gates on:
	 *  - Pro add-on inactive
	 *  - Current screen is the Donations admin page
	 *  - Current user has not dismissed the notice
	 *  - Current user can manage donations
	 *
	 * @return void
	 */
	public function renderDonationsNotice(): void {
		if ( donatico_can_use_premium() ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || 'donatico_page_donatico-donations' !== $screen->id ) {
			return;
		}

		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			return;
		}

		if ( get_user_meta( $user_id, self::DONATIONS_DISMISS_META_KEY, true ) ) {
			return;
		}

		$upgrade_url = admin_url( 'admin.php?page=donatico-upgrade' );
		?>
		<div
			class="notice notice-info is-dismissible donatico-upsell-notice"
			data-donatico-upsell-key="donations"
		>
			<p>
				<strong><?php esc_html_e( 'Track lifetime donor value with Donatico Pro', 'donatico' ); ?></strong>
				—
				<?php esc_html_e( 'Manage subscriptions, see donor history, and accept recurring donations.', 'donatico' ); ?>
				<a href="<?php echo esc_url( $upgrade_url ); ?>" style="font-weight: 600; color: #0F4640;">
					<?php esc_html_e( 'Learn more →', 'donatico' ); ?>
				</a>
			</p>
		</div>
		<?php
	}

	/**
	 * AJAX handler: dismiss an upsell notice for the current user.
	 *
	 * Hooked to wp_ajax_donatico_dismiss_upsell.
	 *
	 * Expects POST body:
	 *  - nonce: the nonce from the donaticoUpsell.nonce localized object
	 *  - key:   the upsell key (currently only 'donations' is supported)
	 *
	 * Returns JSON success on dismissal, JSON error otherwise.
	 *
	 * @return void
	 */
	public function ajaxDismiss(): void {
		if ( ! is_user_logged_in() ) {
			wp_send_json_error( array( 'message' => 'not_logged_in' ), 403 );
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
		}

		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, self::DISMISS_NONCE_ACTION ) ) {
			wp_send_json_error( array( 'message' => 'invalid_nonce' ), 403 );
		}

		$key = isset( $_POST['key'] ) ? sanitize_key( wp_unslash( (string) $_POST['key'] ) ) : '';
		if ( 'donations' !== $key ) {
			wp_send_json_error( array( 'message' => 'unknown_key' ), 400 );
		}

		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			wp_send_json_error( array( 'message' => 'no_user' ), 403 );
		}

		update_user_meta( $user_id, self::DONATIONS_DISMISS_META_KEY, 1 );

		wp_send_json_success( array( 'dismissed' => true ) );
	}

	/**
	 * Render the Form Designer contextual upgrade prompt.
	 *
	 * Called directly from the Form Designer template — no screen-ID gate
	 * needed since we're already in the right context. Gates only on Pro
	 * presence and capability.
	 *
	 * @return void
	 */
	public function renderFormDesignerPrompt(): void {
		if ( donatico_can_use_premium() ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$upgrade_url = admin_url( 'admin.php?page=donatico-upgrade' );
		?>
		<div class="donatico-form-designer-upsell">
			<span class="donatico-upsell-icon" aria-hidden="true">ℹ</span>
			<div class="donatico-upsell-content">
				<strong><?php esc_html_e( 'Want recurring donations?', 'donatico' ); ?></strong>
				<?php esc_html_e( 'Donatico Pro adds Monthly and Yearly giving with Stripe Customer Portal for donor self-management.', 'donatico' ); ?>
				<a href="<?php echo esc_url( $upgrade_url ); ?>" class="donatico-upsell-link">
					<?php esc_html_e( 'Learn more →', 'donatico' ); ?>
				</a>
			</div>
		</div>
		<?php
	}
}
