<?php

namespace VendorTasksDokanClickUp\Licensing;

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

/**
 * Plan Manager
 *
 * Determines whether the site is running under the Free or Pro plan.
 * A stored, non-empty license_key in plugin settings activates Pro.
 * Developers may force Pro via the filter:
 *   add_filter( 'vendor_tasks_dokan_clickup_is_pro', '__return_true' );
 *
 * @since 1.4.0
 * @package VendorTasksDokanClickUp
 * @subpackage Licensing
 */
class PlanManager {

	/**
	 * Option name where settings are stored.
	 */
	private const OPTION = 'vendor_tasks_dokan_clickup_settings';

	/**
	 * Cached pro status (null = not yet resolved).
	 *
	 * @var bool|null
	 */
	private ?bool $is_pro_cache = null;

	/**
	 * Whether the current site is on the Pro plan.
	 *
	 * @since 1.4.0
	 *
	 * @return bool
	 */
	public function is_pro(): bool {
		if ( null === $this->is_pro_cache ) {
			// Prefer Freemius SDK check when available.
			$fs     = function_exists( 'vendor_tasks_dokan_clickup_freemius' ) ? vendor_tasks_dokan_clickup_freemius() : null;
			$is_pro = false;

			if ( null !== $fs ) {
				$is_pro = (bool) $fs->is_paying() || (bool) $fs->is_trial();
			} else {
				// Fallback: non-empty license_key stored in settings.
				$settings = get_option( self::OPTION, array() );
				$is_pro   = ! empty( $settings['license_key'] );
			}

			/**
			 * Filter: override Pro status.
			 * The pro add-on registers this filter to signal it is active.
			 *
			 * @param bool $is_pro True when Freemius says paying/trial, or license_key set.
			 */
			$this->is_pro_cache = (bool) apply_filters( 'vendor_tasks_dokan_clickup_is_pro', $is_pro );
		}

		return $this->is_pro_cache;
	}

	/**
	 * Return the Freemius upgrade URL or empty string when SDK not loaded.
	 *
	 * @since 1.5.0
	 * @return string
	 */
	public function freemius_upgrade_url(): string {
		$fs = function_exists( 'vendor_tasks_dokan_clickup_freemius' ) ? vendor_tasks_dokan_clickup_freemius() : null;
		if ( null !== $fs && method_exists( $fs, 'get_upgrade_url' ) ) {
			return (string) $fs->get_upgrade_url();
		}
		return '';
	}

	/**
	 * Return the Freemius account/license management URL or empty string.
	 *
	 * @since 1.5.0
	 * @return string
	 */
	public function freemius_account_url(): string {
		$fs = function_exists( 'vendor_tasks_dokan_clickup_freemius' ) ? vendor_tasks_dokan_clickup_freemius() : null;
		if ( null !== $fs && method_exists( $fs, 'get_account_url' ) ) {
			return (string) $fs->get_account_url();
		}
		return '';
	}

	/**
	 * Return the current plan slug.
	 *
	 * @since 1.4.0
	 *
	 * @return string 'pro' or 'free'
	 */
	public function plan(): string {
		return $this->is_pro() ? 'pro' : 'free';
	}

	/**
	 * Canonical list of Pro-only feature keys.
	 *
	 * These keys are shared between PHP enforcement and the JS frontend
	 * (exposed via vendorTasksAdmin.proFeatures).
	 *
	 * @since 1.4.0
	 *
	 * @return string[]
	 */
	public function pro_features(): array {
		return array(
			'routing',
			'webhook',
			'slack_notifications',
			'payment_method_check',
			'trust_status_tag',
			'reverse_withdrawal_alert',
			'vendor_verification_approved',
			'failure_email',
			'backfill',
			'export_settings',
			'dokan_pro',
		);
	}

	/**
	 * Assert that a specific Pro feature is available on the current plan.
	 *
	 * @since 1.4.0
	 *
	 * @param string $feature Feature key from pro_features().
	 * @return bool True if feature is available (Pro plan or feature not in pro_features list).
	 */
	public function assert_pro( string $feature ): bool {
		if ( $this->is_pro() ) {
			return true;
		}
		return ! in_array( $feature, $this->pro_features(), true );
	}

	/**
	 * Invalidate the cached pro status (call when license_key is changed).
	 *
	 * @since 1.4.0
	 *
	 * @return void
	 */
	public function invalidate_cache(): void {
		$this->is_pro_cache = null;
	}
}
