<?php
/**
 * Premium feature gating for Show IDs.
 *
 * @package ShowIds
 */

namespace ShowIds;

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

class FeatureGate {

	/** @var list<string> */
	public const PREMIUM_KEYS = [
		'show_user_ids',
		'show_media_ids',
		'show_comment_ids',
		'show_id_in_action_row',
		'show_frontend_admin_bar',
	];

	/**
	 * @return array<string, mixed>
	 */
	public static function getDefaultSettings(): array {
		return [
			'enabled_post_types'      => [ 'post', 'page' ],
			'show_taxonomy_ids'       => true,
			'show_user_ids'           => false,
			'show_media_ids'          => false,
			'show_comment_ids'        => false,
			'show_id_in_action_row'   => false,
			'show_frontend_admin_bar' => false,
		];
	}

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

	/**
	 * @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;
		}

		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 ] );
		}

		return $settings;
	}
}
