<?php

declare(strict_types=1);

namespace Australcode\Rewards\Service\Licensing;

use Australcode\Rewards\Domain\License\LicenseTier;

defined( 'ABSPATH' ) || exit;

/**
 * The single point of feature gating. is_pro_active() for the binary Pro flag.
 * DI, not a singleton — trivially mockable in tests via the FreemiusGateway port.
 *
 * Migrating billing provider (Lemon Squeezy → Freemius, 2026-06-09) changed only
 * the adapter behind {@see FreemiusGateway}, not this contract nor its consumers.
 */
final class LicenseGate {

	public function __construct( private readonly FreemiusGateway $freemius ) {
	}

	/**
	 * True if Pro features should be available now.
	 *
	 * Delegates to the SDK's can_use_premium_code() — covers active paid licenses
	 * and trials.
	 */
	public function is_pro_active(): bool {
		return $this->freemius->can_use_premium_code();
	}

	/**
	 * Active tier. null if there is no usable Pro license (Free state).
	 *
	 * Free/Pro only: premium active ⇒ Pro; otherwise null. Never returns Free
	 * (that is the null case), consistent with the gate's historical contract.
	 */
	public function tier(): ?LicenseTier {
		return $this->freemius->can_use_premium_code() ? LicenseTier::Pro : null;
	}

	/**
	 * URL of the native Freemius pricing/upgrade page. For in-admin upsell CTAs —
	 * the custom License page was removed in the Freemius migration.
	 */
	public function upgrade_url(): string {
		return $this->freemius->upgrade_url();
	}
}
