<?php
/**
 * Utility class for managing the PeachPay plugin capabilities.
 *
 * Note: Server dependency removed. This class now only manages local storage.
 * Payment gateway credentials are managed directly by each gateway module.
 *
 * @package PeachPay
 */

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

require_once PEACHPAY_ABSPATH . 'core/traits/trait-peachpay-singleton.php';

/**
 * Utility class for PeachPay plugin capabilities.
 * Local storage only - no server calls.
 */
final class PeachPay_Capabilities {

	use PeachPay_Singleton;

	/**
	 * The plugin capabilities data.
	 *
	 * @var array
	 */
	private $capabilities_data = array();

	/**
	 * Constructs the capabilities utility class.
	 */
	public function __construct() {
		$cached = get_option( 'peachpay_plugin_capabilities', array() );
		$this->capabilities_data = is_array( $cached ) ? $cached : array();
	}

	/**
	 * Checks if the capability is connected.
	 *
	 * @param string $capability_key The capability key to check.
	 * @return bool
	 */
	public static function connected( $capability_key ) {
		$plugin_capabilities = self::instance()->capabilities_data;

		if ( ! isset( $plugin_capabilities[ $capability_key ]['connected'] ) ) {
			return false;
		}

		return filter_var( $plugin_capabilities[ $capability_key ]['connected'], FILTER_VALIDATE_BOOLEAN );
	}

	/**
	 * Checks if the current merchant has the given capability.
	 *
	 * @param string $capability_key The capability key.
	 * @param string $data_type The data type ('config' or 'account').
	 * @return bool
	 */
	public static function has( $capability_key, $data_type = 'config' ) {
		$plugin_capabilities = self::instance()->capabilities_data;

		if ( ! isset( $plugin_capabilities[ $capability_key ][ $data_type ] ) ) {
			return false;
		}

		return is_array( $plugin_capabilities[ $capability_key ][ $data_type ] );
	}

	/**
	 * Gets the data for a given capability.
	 *
	 * @param string $capability_key The capability key.
	 * @param string $data_type The data type ('config' or 'account').
	 * @return array|null
	 */
	public static function get( $capability_key, $data_type = 'config' ) {
		$plugin_capabilities = self::instance()->capabilities_data;

		if ( ! isset( $plugin_capabilities[ $capability_key ][ $data_type ] ) ) {
			return null;
		}

		$data = $plugin_capabilities[ $capability_key ][ $data_type ];
		return is_array( $data ) ? $data : null;
	}

	/**
	 * No-op. Server dependency removed.
	 * Kept for backward compatibility so existing callers don't error.
	 */
	public static function refresh() {
		// Server dependency removed.
	}

	/**
	 * Sets capability data locally.
	 *
	 * @param string $capability_key The capability key.
	 * @param array  $data The capability data.
	 */
	public static function set_capability( $capability_key, $data ) {
		$instance = self::instance();
		$instance->capabilities_data[ $capability_key ] = $data;
		update_option( 'peachpay_plugin_capabilities', $instance->capabilities_data );
	}

	/**
	 * Removes a capability.
	 *
	 * @param string $capability_key The capability key to remove.
	 */
	public static function remove_capability( $capability_key ) {
		$instance = self::instance();
		unset( $instance->capabilities_data[ $capability_key ] );
		update_option( 'peachpay_plugin_capabilities', $instance->capabilities_data );
	}
}

return PeachPay_Capabilities::instance();
