<?php

declare(strict_types=1);

namespace Australcode\Rewards\Admin;

use Australcode\Rewards\Service\Licensing\LicenseGate;

defined( 'ABSPATH' ) || exit;

/**
 * Renders the "Activate Pro" upgrade call-to-action, gated by capability.
 *
 * The Australcode Rewards admin pages are visible to `manage_woocommerce`, but
 * the native Freemius upgrade/pricing/account screens require `manage_options`.
 * A shop manager (manage_woocommerce, no manage_options) who follows an upgrade
 * link therefore hits a wp_die. The CTA must degrade for those users: an admin
 * gets the button, everyone else gets a note pointing them to an administrator.
 */
final class UpgradeCta {

	/**
	 * Whether the current user can reach the Freemius upgrade / account screens.
	 */
	public static function can_manage_license(): bool {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Upgrade CTA markup: the button for users who can manage the license, or a
	 * muted note for those who cannot (e.g. shop managers). The return value is
	 * already escaped; wrap the echo in wp_kses_post() for the linter.
	 */
	public static function button( LicenseGate $gate, string $label ): string {
		if ( self::can_manage_license() ) {
			return '<a class="acds-btn acds-btn--primary" href="' . esc_url( $gate->upgrade_url() ) . '">'
				. esc_html( $label ) . '</a>';
		}

		return '<p class="acds-muted">' . esc_html__(
			'Ask a site administrator to activate a Pro license.',
			'australcode-points-rewards'
		) . '</p>';
	}
}
