<?php
/**
 * License Manager — Simplified freemium architecture.
 *
 * Core has NO license logic. "Is Pro active?" is determined solely
 * by whether the Pro plugin is installed and has registered its filter.
 *
 * Pro plugin hooks into:
 *   add_filter( 'capiflow_is_pro_active', '__return_true' );
 *   add_filter( 'capiflow_has_pro_feature', function( $val, $feature ) { ... }, 10, 2 );
 *
 * @package CapiFlow
 */

namespace CapiFlow\Core;

defined( 'ABSPATH' ) || exit;

class License_Manager {

	/**
	 * Check if CapiFlow Pro is active.
	 *
	 * Returns true only when Pro plugin is installed, active, and has
	 * registered the capiflow_is_pro_active filter.
	 *
	 * @return bool
	 */
	public static function is_pro_active(): bool {
		return (bool) apply_filters( 'capiflow_is_pro_active', false );
	}

	/**
	 * Check if a specific feature is available on the current plan.
	 *
	 * Core features are always available (no license required).
	 * Pro features require the Pro plugin to return true via filter.
	 *
	 * @param string $feature Feature slug.
	 * @return bool
	 */
	public static function has_feature( string $feature ): bool {
		// These features are always free — no Pro required.
		$core_features = array(
			'basic_tracking',
			'all_events',
			'purchase',
			'fraud_alert',
			'invoice',
			'debug_console',
			'single_pixel',
			'cart_monitor',
		);

		if ( in_array( $feature, $core_features, true ) ) {
			return true;
		}

		// Pro features — Pro plugin hooks in via filter.
		return (bool) apply_filters( 'capiflow_has_pro_feature', false, $feature );
	}

	/**
	 * Get the current plan label for display.
	 *
	 * @return string 'pro' or 'free'
	 */
	public static function get_plan(): string {
		return self::is_pro_active() ? 'pro' : 'free';
	}
}
