<?php
/**
 * FeatureGate
 *
 * Single source of truth for premium feature gating and free-tier limits.
 * Wraps the Freemius SDK license check so the rest of the plugin does not
 * need to know about SDK internals.
 *
 * @package Relatedly\Premium
 * @since   2.1.0
 */

namespace Relatedly\Premium;

defined( 'ABSPATH' ) || exit;

/**
 * Provides tier checks and free-tier limits.
 *
 * @since 2.1.0
 */
class FeatureGate {

	/** Free inline theme keys. */
	public const FREE_INLINE_THEMES = [ 'theme1', 'theme2', 'theme3' ];

	/** Free end-content theme keys. */
	public const FREE_END_CONTENT_THEMES = [ 'theme1', 'theme2', 'theme6' ];

	/** Maximum inline posts for free users. */
	public const FREE_MAX_INLINE_POSTS = 5;

	/** Maximum end-content posts for free users. */
	public const FREE_MAX_END_CONTENT_POSTS = 5;

	/** Maximum RSS end-content posts for free users. */
	public const FREE_MAX_RSS_POSTS = 3;

	/** Maximum end-content posts for premium users. */
	public const PREMIUM_MAX_END_CONTENT_POSTS = 20;

	/** Maximum RSS end-content posts for premium users. */
	public const PREMIUM_MAX_RSS_POSTS = 10;

	/** Post types available in the free tier. */
	public const FREE_POST_TYPES = [ 'post', 'page' ];

	/**
	 * Check whether premium features should be enabled.
	 *
	 * @since 2.1.0
	 */
	public static function is_premium(): bool {
		return LicenseManager::is_valid();
	}

	/**
	 * Check whether an inline theme is available in the free tier.
	 *
	 * @since 2.1.0
	 * @param string $theme Theme key.
	 * @return bool
	 */
	public static function is_inline_theme_available( string $theme ): bool {
		if ( self::is_premium() ) {
			return true;
		}
		return in_array( $theme, self::FREE_INLINE_THEMES, true );
	}

	/**
	 * Check whether an end-content theme is available in the free tier.
	 *
	 * @since 2.1.0
	 * @param string $theme Theme key.
	 * @return bool
	 */
	public static function is_end_content_theme_available( string $theme ): bool {
		if ( self::is_premium() ) {
			return true;
		}
		return in_array( $theme, self::FREE_END_CONTENT_THEMES, true );
	}

	/**
	 * Get the effective maximum number of inline posts.
	 *
	 * @since 2.1.0
	 */
	public static function max_inline_posts(): int {
		return self::is_premium() ? PHP_INT_MAX : self::FREE_MAX_INLINE_POSTS;
	}

	/**
	 * Get the effective maximum number of end-content posts.
	 *
	 * @since 2.1.0
	 */
	public static function max_end_content_posts(): int {
		return self::is_premium() ? self::PREMIUM_MAX_END_CONTENT_POSTS : self::FREE_MAX_END_CONTENT_POSTS;
	}

	/**
	 * Get the effective maximum number of RSS end-content posts.
	 *
	 * @since 2.1.0
	 */
	public static function max_rss_posts(): int {
		return self::is_premium() ? self::PREMIUM_MAX_RSS_POSTS : self::FREE_MAX_RSS_POSTS;
	}

	/**
	 * Filter a list of enabled post types down to what the current tier allows.
	 *
	 * @since 2.1.0
	 * @param string[] $post_types Requested post types.
	 * @return string[]
	 */
	public static function allowed_post_types( array $post_types ): array {
		if ( self::is_premium() ) {
			return $post_types;
		}
		return array_values( array_intersect( $post_types, self::FREE_POST_TYPES ) );
	}

	/**
	 * Check whether the floating sidebar is available.
	 *
	 * @since 2.1.0
	 */
	public static function can_use_floating_sidebar(): bool {
		return self::is_premium();
	}

	/**
	 * Check whether the advanced statistics dashboard is available.
	 *
	 * @since 2.1.0
	 */
	public static function can_use_statistics_dashboard(): bool {
		return self::is_premium();
	}

	/**
	 * Check whether statistics CSV export is available.
	 *
	 * @since 2.1.0
	 */
	public static function can_export_statistics(): bool {
		return self::is_premium();
	}

	/**
	 * Check whether scheduled email reports are available.
	 *
	 * @since 2.1.0
	 */
	public static function can_use_email_reports(): bool {
		return self::is_premium();
	}
}
