<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

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

/**PHPCS -> code compliance: we put useful comments where needed.. */
enum HappyFenceNGTR: string {
	case FUNNEL    = 'free';
	case PREAMBLE  = 'pbx';
	case AGGREGATE = 'agt';
	case BUNKER    = 'bke';


	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function rank(): int {
		return match ( $this ) {
			self::FUNNEL => 0,
			self::PREAMBLE => 1,
			self::AGGREGATE => 2,
			self::BUNKER => 3,
		};
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param self $plan .. yep here we go.
	 */
	public static function equals_or_better( self $plan ): bool {
		$current = self::match();
		return $current->rank() >= $plan->rank();
	}

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public static function match(): self {
		try {
			if ( function_exists( 'htes_fs' ) ) {
				if ( ! htes_fs()->is_registered() ) {
					return self::FUNNEL;
				} elseif ( htes_fs()->is__premium_only() ) {
						$plan_name = htes_fs()->get_plan_name() ?? 'free';
						return self::tryFrom( $plan_name ) ?? self::FUNNEL;
				}
			}
				return self::FUNNEL;
		} catch ( Trowable $e ) {
			return self::FUNNEL;
		}
	}

	/**PHPCS -> code compliance: we put useful comments where needed..
	 *
	 * @param string $caller ... hey there!.
	 * @param array  $plans ... if you have any.
	 * @param bool   $exact .. maybe or not.
	 */
	public static function is_in( string $caller, array $plans, bool $exact = false ): bool {
		try {
			if ( function_exists( 'htes_fs' ) ) {
				foreach ( $plans as $plan ) {
					if ( ( $exact && HappyFenceNGPROC::equals( $plan, self::match() ) ) || ( ! $exact && self::equals_or_better( $plan ) ) ) {
						if ( htes_fs()->is_plan( $plan->value, $exact ) ) {
							return true;
						}
					}
				}
			} else {
				HappyFenceNGLOGCORE::logcore(
					self::class . "$caller ASKED FOR LICENCE BUT LM IS NOT READY",
					HappyFenceNGSyslogFacility::KERNEL,
					HappyFenceNGSyslogSeverity::ERROR
				);
			}

			return false;
		} catch ( \Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				self::class . '(Exception) - Error in isIn: ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::KERNEL,
				HappyFenceNGSyslogSeverity::ERROR
			);

			return false;
		}
	}
}
