<?php
/**
 * Flipa license helper — single source of truth for free/Pro gating.
 * The rest of the plugin asks Flipa_License::can_use_pro() or
 * Flipa_License::allowed( 'feature' ); it never talks to Freemius directly.
 *
 * Free vs Pro split (decided 2026-06-13):
 *   FREE : core viewer, seek + thumbnails, zoom, all 7 languages + RTL,
 *          background colour, inline/iframe embed, lightbox, unlimited books,
 *          shortcode + Gutenberg + Classic. An optional "Powered by flipa" credit is
 *          OFF by default for every tier and shown only when the site owner opts in.
 *   PRO  : table of contents, page-flip sounds, password,
 *          custom background image, clickable PDF links, QR in share,
 *          page-stack depth, and the WooCommerce / Elementor / WPBakery /
 *          bookshelf surfaces.
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

class Flipa_License {

	/** Features locked to the Pro tier. */
	const PRO_FEATURES = array();

	/**
	 * Is the Pro tier available right now (paying customer, or dev stub)?
	 * Define FLIPA_FORCE_FREE (true) in wp-config.php to preview the free
	 * experience on a dev/stub install.
	 */
	public static function can_use_pro() {
		// Dev-only override (never honored in production, so a leftover define can't downgrade a paying user).
		if ( defined( 'FLIPA_FORCE_FREE' ) && FLIPA_FORCE_FREE && defined( 'WP_FS__DEV_MODE' ) && WP_FS__DEV_MODE ) { return false; }
		if ( function_exists( 'flipa_fs' ) ) {
			$fs = flipa_fs();
			if ( is_object( $fs ) ) {
				// dev stub (SDK absent) → behave as licensed so dev sees everything
				if ( $fs instanceof Flipa_FS_Stub ) { return true; }
				// real Freemius: Pro = an active paid license OR an in-progress trial
				if ( method_exists( $fs, 'is_paying_or_trial' ) ) { return (bool) $fs->is_paying_or_trial(); }
				$paying = method_exists( $fs, 'is_paying' ) ? $fs->is_paying() : false;
				$trial  = method_exists( $fs, 'is_trial' )  ? $fs->is_trial()  : false;
				return (bool) ( $paying || $trial );
			}
		}
		return true; // fail open in dev (no SDK present)
	}

	/** Alias kept for readability at call sites. */
	public static function is_pro() {
		return self::can_use_pro();
	}

	/** Is this feature key a Pro-only feature? */
	public static function is_pro_feature( $feature ) {
		return in_array( $feature, self::PRO_FEATURES, true );
	}

	/** Can this specific feature be used right now? Free features → always true. */
	public static function allowed( $feature ) {
		if ( ! self::is_pro_feature( $feature ) ) { return true; }
		return self::can_use_pro();
	}

	/** URL to send free users to upgrade (Freemius checkout if wired, else the site). */
	public static function upgrade_url() {
		if ( function_exists( 'flipa_fs' ) ) {
			$fs = flipa_fs();
			if ( is_object( $fs ) && method_exists( $fs, 'get_upgrade_url' ) ) {
				$u = @$fs->get_upgrade_url();
				if ( $u ) { return $u; }
			}
		}
		return 'https://flipa.pro/pro';
	}

	/** Small inline "PRO" badge for the editor UI. */
	public static function pro_badge() {
		return '<span class="flipa-pro-badge" title="' . esc_attr__( 'Pro feature', 'flipa-pdf-flipbook' ) . '">PRO</span>';
	}
}
