<?php
/**
 * Freemius Pro gate - centralized helpers for premium checks.
 *
 * @package EasySearchReplace
 */

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

/**
 * Thin wrapper around esrn_fs() so the rest of the plugin never has to know
 * whether Freemius is loaded or how to construct upgrade URLs.
 *
 * Every other class asks ESRN_Freemius_Gate::is_pro() - NEVER calls esrn_fs()
 * directly. That keeps Freemius swappable and the Pro logic testable.
 */
class ESRN_Freemius_Gate {

	/**
	 * Option key for the CSS-selector grandfather flag.
	 *
	 * Set once during the 1.x → 1.2 migration for sites that already had rules.
	 * Lets those existing users keep CSS-selector targeting without a Pro license.
	 */
	const GRANDFATHER_OPTION = 'esrn_css_selector_grandfathered';

	/**
	 * Is this a licensed Pro site right now?
	 *
	 * Uses can_use_premium_code() which handles trial users correctly too.
	 *
	 * @return bool
	 */
	public static function is_pro() {
		if ( ! function_exists( 'esrn_fs' ) ) {
			return false;
		}
		$fs = esrn_fs();
		if ( ! $fs ) {
			return false;
		}
		return (bool) $fs->can_use_premium_code();
	}

	/**
	 * Can the current site use the CSS selector feature?
	 *
	 * Returns true if either:
	 *  1. The site has an active Pro license, OR
	 *  2. The site was grandfathered (upgraded from a pre-1.2 install that
	 *     already had the feature for free).
	 *
	 * Self-healing: if the migration missed setting the flag but existing rules
	 * already contain a CSS selector value, the flag is set automatically.
	 *
	 * @return bool
	 */
	public static function is_css_selector_allowed() {
		if ( self::is_pro() ) {
			return true;
		}

		// Fast path: flag already set.
		if ( get_option( self::GRANDFATHER_OPTION, false ) ) {
			return true;
		}

		// Self-healing: check if any saved rule already uses a CSS selector.
		// If so, this is an existing user whose flag was missed — set it now.
		$options = get_option( ESRN_OPTION_KEY );
		if ( is_array( $options ) && ! empty( $options['fields'] ) && is_array( $options['fields'] ) ) {
			foreach ( $options['fields'] as $rule ) {
				if ( ! empty( $rule['selector'] ) ) {
					update_option( self::GRANDFATHER_OPTION, 1, 'yes' );
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * Build the upgrade / checkout URL, optionally tagged with the feature
	 * that triggered the click so you can see conversion source in Freemius.
	 *
	 * @param string $feature_tag Optional feature slug, e.g. 'regex', 'schedule'.
	 * @return string
	 */
	public static function upgrade_url( $feature_tag = '' ) {
		if ( ! function_exists( 'esrn_fs' ) ) {
			return '#';
		}
		$fs = esrn_fs();
		if ( ! $fs ) {
			return '#';
		}

		$url = $fs->get_upgrade_url();

		if ( '' !== $feature_tag ) {
			$url = add_query_arg( 'esrn_feature', sanitize_key( $feature_tag ), $url );
		}

		return $url;
	}

	/**
	 * URL to the Freemius account page (license, invoices, billing).
	 *
	 * @return string
	 */
	public static function account_url() {
		if ( ! function_exists( 'esrn_fs' ) ) {
			return '#';
		}
		$fs = esrn_fs();
		return $fs ? $fs->get_account_url() : '#';
	}

	/**
	 * Markup for a small "Pro" badge - used next to locked fields.
	 *
	 * @return string Safe HTML.
	 */
	public static function pro_badge() {
		return '<span class="esr-pro-badge" aria-label="' . esc_attr__( 'Pro feature', 'easy-search-replace' ) . '">' . esc_html__( 'Pro', 'easy-search-replace' ) . '</span>';
	}

	/**
	 * Full "Upgrade to unlock" button markup for locked fields.
	 *
	 * @param string $feature_tag Feature slug for attribution.
	 * @return string Safe HTML.
	 */
	public static function upgrade_button( $feature_tag = '' ) {
		return sprintf(
			'<a href="%s" class="esr-upgrade-btn">%s</a>',
			esc_url( self::upgrade_url( $feature_tag ) ),
			esc_html__( 'Upgrade to unlock', 'easy-search-replace' )
		);
	}
}
