<?php
/**
 * Answyr — Pro capability seam.
 *
 * Single source of truth for "is this a licensed Pro install?". Kept as a
 * filter/constant so Pro features can be built and tested before the Freemius
 * SDK lands. Once Freemius is wired in, its bootstrap hooks `answyr_is_pro` to
 * `answyr_fs()->is_plan( 'pro', true )` — no feature code changes.
 *
 * Dev/test: define `ANSWYR_PRO` truthy in wp-config, or
 * `add_filter( 'answyr_is_pro', '__return_true' )`.
 *
 * @package Answyr
 */

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

if ( ! function_exists( 'answyr_is_pro' ) ) {
	/** Whether Pro features are unlocked on this install. */
	function answyr_is_pro(): bool {
		if ( defined( 'ANSWYR_PRO' ) && ANSWYR_PRO ) {
			return true;
		}
		return (bool) apply_filters( 'answyr_is_pro', false );
	}
}

if ( ! function_exists( 'answyr_upgrade_url' ) ) {
	/**
	 * Where "Upgrade to Pro" should point. Uses the Freemius in-plugin checkout
	 * when the SDK is active, else a filterable default (the marketing pricing page).
	 *
	 * The in-plugin pricing page (`?page=answyr-pricing`) only exists once the user
	 * has left Freemius "activation mode" (connected or skipped the opt-in). Linking
	 * to it before then hits an unregistered page and WordPress 403s, so we fall back
	 * to the marketing URL until it's registered.
	 */
	function answyr_upgrade_url(): string {
		if ( function_exists( 'answyr_fs' ) && answyr_fs() && ! answyr_fs()->is_activation_mode() ) {
			return answyr_fs()->get_upgrade_url();
		}
		return (string) apply_filters( 'answyr_upgrade_url', 'https://answyr.io/pricing/' );
	}
}

if ( ! function_exists( 'answyr_pro_upgrade_available' ) ) {
	/**
	 * Whether to surface an in-plugin "Upgrade to Pro" CTA right now.
	 *
	 * False when already Pro, or when the Freemius SDK is active but still in
	 * activation mode — its pricing page isn't registered yet, so an Upgrade link
	 * would 403. Once the opt-in is completed or skipped, the page exists and this
	 * returns true. With the SDK dormant (no Freemius config) the URL falls back to
	 * the marketing page, which is always safe to link.
	 */
	function answyr_pro_upgrade_available(): bool {
		if ( answyr_is_pro() ) {
			return false;
		}
		if ( function_exists( 'answyr_fs' ) && answyr_fs() ) {
			return ! answyr_fs()->is_activation_mode();
		}
		return true;
	}
}

if ( ! function_exists( 'answyr_license_activation_url' ) ) {
	/**
	 * URL that lets an existing key-holder activate their license from the free
	 * build. Opens the Freemius connect screen pre-set to license-key mode
	 * (`require_license=true`); on a valid key Freemius downloads + installs the
	 * premium package automatically (the Pro code is stripped from the free build).
	 *
	 * If the user skipped the opt-in (anonymous), we use the reconnect URL so the
	 * connect screen is shown again instead of the normal plugin page. Returns ''
	 * when the SDK is dormant (no Freemius config) or the install is already Pro.
	 */
	function answyr_license_activation_url(): string {
		if ( answyr_is_pro() || ! function_exists( 'answyr_fs' ) || ! answyr_fs() ) {
			return '';
		}
		$fs     = answyr_fs();
		$params = array( 'require_license' => 'true' );
		return (string) ( $fs->is_activation_mode()
			? $fs->get_activation_url( $params )
			: $fs->get_reconnect_url( $params ) );
	}
}
