<?php
/**
 * Premium feature gating for Custom Post Order.
 *
 * @package CustomPostOrder
 */

namespace CustomPostOrder;

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

class FeatureGate {

	/** @var list<string> */
	public const FREE_POST_TYPES = [ 'post', 'page' ];

	/** @var list<string> */
	public const PREMIUM_KEYS = [
		'apply_frontend_archive',
		'apply_frontend_search',
		'apply_admin',
	];

	public static function isPremiumLicensed(): bool {
		return function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid();
	}

	public static function isFreePostType( string $post_type ): bool {
		return in_array( sanitize_key( $post_type ), self::FREE_POST_TYPES, true );
	}

	/**
	 * License page when Pro is installed, otherwise the public purchase URL.
	 */
	public static function upsellUrl(): string {
		if ( function_exists( 'wp_powersuite_premium_upsell_url' ) ) {
			return wp_powersuite_premium_upsell_url();
		}

		return defined( 'WP_POWERSUITE_PRODUCT_URL' ) ? WP_POWERSUITE_PRODUCT_URL : 'https://wppowersuite.com/';
	}

	/**
	 * @param array<string, mixed> $settings
	 * @return array<string, mixed>
	 */
	public static function applyPremiumTierLimits( array $settings ): array {
		if ( self::isPremiumLicensed() ) {
			return $settings;
		}

		foreach ( self::PREMIUM_KEYS as $key ) {
			$settings[ $key ] = false;
		}

		$types = isset( $settings['post_types'] ) && is_array( $settings['post_types'] )
			? $settings['post_types']
			: [];
		$settings['post_types'] = array_values(
			array_filter(
				$types,
				static fn( $type ): bool => self::isFreePostType( (string) $type )
			)
		);

		return $settings;
	}

	/**
	 * @param array<string, mixed> $settings Sanitized settings about to be saved.
	 * @param array<string, mixed> $existing Previously stored settings.
	 * @return array<string, mixed>
	 */
	public static function preservePremiumSettingsOnSave( array $settings, array $existing ): array {
		if ( self::isPremiumLicensed() ) {
			return $settings;
		}

		foreach ( self::PREMIUM_KEYS as $key ) {
			$settings[ $key ] = ! empty( $existing[ $key ] );
		}

		$submitted = isset( $settings['post_types'] ) && is_array( $settings['post_types'] )
			? $settings['post_types']
			: [];
		$existing_types = isset( $existing['post_types'] ) && is_array( $existing['post_types'] )
			? $existing['post_types']
			: [];

		$free_submitted = array_values(
			array_filter(
				$submitted,
				static fn( $type ): bool => self::isFreePostType( (string) $type )
			)
		);
		$premium_existing = array_values(
			array_filter(
				$existing_types,
				static fn( $type ): bool => ! self::isFreePostType( (string) $type )
			)
		);

		$settings['post_types'] = array_values( array_unique( array_merge( $free_submitted, $premium_existing ) ) );

		return $settings;
	}
}

if ( function_exists( 'add_filter' ) ) {
	add_filter(
		'wp_powersuite_preserve_pro_settings_on_save',
		static function ( $settings, string $module_id, $existing ) {
			if ( $module_id !== 'custom-post-order' || ! is_array( $settings ) ) {
				return $settings;
			}

			return FeatureGate::preservePremiumSettingsOnSave( $settings, is_array( $existing ) ? $existing : [] );
		},
		10,
		3
	);
}
