<?php
/**
 * Free/Pro feature gating.
 *
 * Mirrors SwiftQueue's pattern: everything is UNLOCKED until license
 * enforcement is explicitly enabled (Freemius wiring defines
 * DATASTREAM_ENFORCE_LICENSE). Premium status is always derived — never
 * hardcode it — so the free build and the pro build share this file.
 *
 * @package DataStream
 */

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

/**
 * Class DataStream_Feature_Gate
 */
class DataStream_Feature_Gate {

	/**
	 * Whether premium features are available on this install.
	 *
	 * @return bool
	 */
	public static function is_premium() {
		// Unlocked unless something is actively enforcing. An install with no
		// store behind it must never lose features it already had.
		$premium = self::is_enforcing() ? self::is_pro() : true;

		/**
		 * Filter premium availability (used by tests; not a licensing bypass —
		 * a shipped build enforces via DataStream_License).
		 *
		 * @param bool $premium Whether premium code may run.
		 */
		return (bool) apply_filters( 'datastream_is_premium', $premium );
	}

	/**
	 * Whether licensing gates are active at all.
	 *
	 * Off by default. DataStream_License::initialize() turns this on by
	 * filter, but only once real store credentials AND the SDK are present —
	 * so a missing vendor/freemius or a blank config leaves the plugin fully
	 * usable instead of half-crippled.
	 *
	 * @return bool
	 */
	public static function is_enforcing() {
		if ( defined( 'DATASTREAM_ENFORCE_LICENSE' ) && DATASTREAM_ENFORCE_LICENSE ) {
			return true;
		}

		/**
		 * Filter whether license enforcement runs.
		 *
		 * @param bool $enforce Whether to enforce licensing.
		 */
		return (bool) apply_filters( 'datastream_enforce_license', false );
	}

	/**
	 * Whether this site holds an active paid (or trial) license.
	 *
	 * @return bool
	 */
	public static function is_pro() {
		/**
		 * Lets Freemius (or a custom license manager) flip Pro on.
		 *
		 * @param bool $is_pro Whether this site has an active Pro license.
		 */
		return (bool) apply_filters( 'datastream_is_pro', false );
	}

	/**
	 * How many streams may be enabled.
	 *
	 * @return int
	 */
	public static function max_enabled_streams() {
		return self::is_premium() ? 100 : 1;
	}

	/**
	 * Whether API-to-Posts (CPT mapping) may run.
	 *
	 * @return bool
	 */
	public static function can_map_posts() {
		return self::is_premium();
	}

	/**
	 * Allowed sync schedules for this tier. The 15-minute interval is
	 * deliberately FREE and stays free; 5-minute is the pro addition.
	 *
	 * @return array<string, string>
	 */
	public static function allowed_schedules() {
		$schedules = array(
			'datastream_15min' => __( 'Every 15 Minutes', 'datastream-api-sync' ),
			'hourly'           => __( 'Hourly', 'datastream-api-sync' ),
			'twicedaily'       => __( 'Twice Daily', 'datastream-api-sync' ),
			'daily'            => __( 'Daily', 'datastream-api-sync' ),
		);

		if ( self::is_premium() ) {
			$schedules = array( 'datastream_5min' => __( 'Every 5 Minutes', 'datastream-api-sync' ) ) + $schedules;
		}

		return $schedules;
	}

	/**
	 * Maximum pagination pages fetched per stream per sync.
	 *
	 * @return int
	 */
	public static function max_pages() {
		return self::is_premium() ? 50 : 5;
	}
}
