<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Payment Gateway Currency Support Functions
 *
 * Functions to check if currencies are supported by Stripe and PayPal payment gateways
 * Based on official documentation as of 2025
 */

/**
 * Get list of Stripe-supported currencies.
 *
 * Stripe supports 135+ currencies for card payments.
 * Source: https://docs.stripe.com/currencies
 *
 * @return array Array of currency codes supported by Stripe
 */
function get_rtrb_stripe_supported_currencies() {
	return array(
		'USD',
		'AED',
		'AFN',
		'ALL',
		'AMD',
		'ANG',
		'AOA',
		'ARS',
		'AUD',
		'AWG',
		'AZN',
		'BAM',
		'BBD',
		'BDT',
		'BGN',
		'BIF',
		'BMD',
		'BND',
		'BOB',
		'BRL',
		'BSD',
		'BWP',
		'BYN',
		'BZD',
		'CAD',
		'CDF',
		'CHF',
		'CLP',
		'CNY',
		'COP',
		'CRC',
		'CVE',
		'CZK',
		'DJF',
		'DKK',
		'DOP',
		'DZD',
		'EGP',
		'ETB',
		'EUR',
		'FJD',
		'FKP',
		'GBP',
		'GEL',
		'GIP',
		'GMD',
		'GNF',
		'GTQ',
		'GYD',
		'HKD',
		'HNL',
		'HTG',
		'HUF',
		'IDR',
		'ILS',
		'INR',
		'ISK',
		'JMD',
		'JPY',
		'KES',
		'KGS',
		'KHR',
		'KMF',
		'KRW',
		'KYD',
		'KZT',
		'LAK',
		'LBP',
		'LKR',
		'LRD',
		'LSL',
		'MAD',
		'MDL',
		'MGA',
		'MKD',
		'MMK',
		'MNT',
		'MOP',
		'MUR',
		'MVR',
		'MWK',
		'MXN',
		'MYR',
		'MZN',
		'NAD',
		'NGN',
		'NIO',
		'NOK',
		'NPR',
		'NZD',
		'PAB',
		'PEN',
		'PGK',
		'PHP',
		'PKR',
		'PLN',
		'PYG',
		'QAR',
		'RON',
		'RSD',
		'RUB',
		'RWF',
		'SAR',
		'SBD',
		'SCR',
		'SEK',
		'SGD',
		'SHP',
		'SLE',
		'SOS',
		'SRD',
		'STD',
		'SZL',
		'THB',
		'TJS',
		'TOP',
		'TRY',
		'TTD',
		'TWD',
		'TZS',
		'UAH',
		'UGX',
		'UYU',
		'UZS',
		'VND',
		'VUV',
		'WST',
		'XAF',
		'XCD',
		'XCG',
		'XOF',
		'XPF',
		'YER',
		'ZAR',
		'ZMW',
	);
}

/**
 * Get list of PayPal-supported currencies.
 *
 * PayPal supports 25 currencies.
 * Source: https://developer.paypal.com/docs/reports/reference/paypal-supported-currencies/
 *
 * @return array Array of currency codes supported by PayPal
 */
function get_rtrb_paypal_supported_currencies() {
	return array(
		'AUD', // Australian Dollar
		'BRL', // Brazilian Real (in-country only)
		'CAD', // Canadian Dollar
		'CNY', // Chinese Renminbi (in-country only)
		'CZK', // Czech Koruna
		'DKK', // Danish Krone
		'EUR', // Euro
		'HKD', // Hong Kong Dollar
		'HUF', // Hungarian Forint (zero-decimal)
		'ILS', // Israeli New Shekel
		'JPY', // Japanese Yen (zero-decimal)
		'MYR', // Malaysian Ringgit (in-country only)
		'MXN', // Mexican Peso
		'TWD', // New Taiwan Dollar (zero-decimal)
		'NZD', // New Zealand Dollar
		'NOK', // Norwegian Krone
		'PHP', // Philippine Peso
		'PLN', // Polish Złoty
		'GBP', // Pound Sterling
		'SGD', // Singapore Dollar
		'SEK', // Swedish Krona
		'CHF', // Swiss Franc
		'THB', // Thai Baht
		'USD', // United States Dollar
	);
}

/**
 * Check if a currency is supported by Stripe.
 *
 * @param string $currency_code Three-letter ISO currency code (e.g., 'USD', 'EUR')
 *
 * @return bool True if supported, false otherwise
 */
function is_rtrb_currency_supported_by_stripe( $currency_code ) {
	$supported_currencies = get_rtrb_stripe_supported_currencies();

	return in_array( strtoupper( $currency_code ), $supported_currencies, true );
}

/**
 * Check if a currency is supported by PayPal.
 *
 * @param string $currency_code Three-letter ISO currency code (e.g., 'USD', 'EUR')
 *
 * @return bool True if supported, false otherwise
 */
function is_rtrb_currency_supported_by_paypal( $currency_code ) {
	$supported_currencies = get_rtrb_paypal_supported_currencies();

	return in_array( strtoupper( $currency_code ), $supported_currencies, true );
}

/**
 * Get available payment gateways for a specific currency.
 *
 * @param string $currency_code Three-letter ISO currency code (e.g., 'USD', 'EUR')
 *
 * @return array Array of available payment gateways ('stripe', 'paypal', or both)
 */
function get_rtrb_available_payment_gateways_for_currency( $currency_code ) {
	$gateways = array();

	// Stripe is a Pro feature.
	if ( rtrb_pro_active() && is_rtrb_currency_supported_by_stripe( $currency_code ) ) {
		$gateways[] = 'stripe';
	}

	if ( is_rtrb_currency_supported_by_paypal( $currency_code ) ) {
		$gateways[] = 'paypal';
	}

	return $gateways;
}

/**
 * Check if any payment gateway supports the given currency.
 *
 * @param string $currency_code Three-letter ISO currency code (e.g., 'USD', 'EUR')
 *
 * @return bool True if at least one gateway supports it, false otherwise
 */
function is_rtrb_currency_supported_by_any_gateway( $currency_code ) {
	return is_rtrb_currency_supported_by_stripe( $currency_code )
			|| is_rtrb_currency_supported_by_paypal( $currency_code );
}

/**
 * Get payment gateway support info for a currency.
 *
 * @param string $currency_code Three-letter ISO currency code (e.g., 'USD', 'EUR')
 *
 * @return array Array with keys 'stripe', 'paypal', and 'gateways'
 */
function get_rtrb_currency_payment_gateway_support( $currency_code ) {
	return array(
		'currency'  => strtoupper( $currency_code ),
		'stripe'    => is_rtrb_currency_supported_by_stripe( $currency_code ),
		'paypal'    => is_rtrb_currency_supported_by_paypal( $currency_code ),
		'gateways'  => get_rtrb_available_payment_gateways_for_currency( $currency_code ),
		'supported' => is_rtrb_currency_supported_by_any_gateway( $currency_code ),
	);
}

/**
 * Filter booking currencies to show only those supported by payment gateways.
 *
 * @param array $currencies Array of currencies from get_rtrb_currencies()
 * @param array $required_gateways Array of required gateways (e.g., ['stripe', 'paypal'])
 * @param string $match_type 'any' (at least one gateway) or 'all' (all gateways)
 *
 * @return array Filtered array of currencies
 */
function filter_rtrb_currencies_by_gateway_support(
	$currencies,
	$required_gateways = array(
		'stripe',
		'paypal',
	),
	$match_type = 'any'
) {
	$filtered = array();

	foreach ( $currencies as $code => $name ) {
		$support            = get_rtrb_currency_payment_gateway_support( $code );
		$supported_gateways = $support['gateways'];

		if ( $match_type === 'all' ) {
			// Currency must be supported by ALL required gateways
			$is_supported = count( array_intersect( $required_gateways, $supported_gateways ) ) === count( $required_gateways );
		} else {
			// Currency must be supported by ANY of the required gateways (default)
			$is_supported = count( array_intersect( $required_gateways, $supported_gateways ) ) > 0;
		}

		if ( $is_supported ) {
			$filtered[ $code ] = $name;
		}
	}

	return $filtered;
}

/**
 * Get currencies supported by both Stripe and PayPal.
 *
 * @return array Array of currency codes supported by both gateways
 */
function get_rtrb_currencies_supported_by_both_gateways() {
	$stripe_currencies = get_rtrb_stripe_supported_currencies();
	$paypal_currencies = get_rtrb_paypal_supported_currencies();

	return array_intersect( $stripe_currencies, $paypal_currencies );
}

/**
 * Display payment gateway availability message for admin.
 *
 * @param string $currency_code Three-letter ISO currency code
 *
 * @return string HTML message about gateway availability
 */
function get_rtrb_payment_gateway_availability_message( $currency_code ) {
	$support = get_rtrb_currency_payment_gateway_support( $currency_code );

	if ( ! $support['supported'] ) {
		return sprintf(
			'<div class="notice notice-error"><p><strong>%s</strong> is not supported by Stripe or PayPal. Please choose a different currency or use an alternative payment gateway.</p></div>',
			$currency_code
		);
	}

	$gateways_list = implode( ' and ', array_map( 'ucfirst', $support['gateways'] ) );

	return sprintf(
		'<div class="notice notice-success"><p><strong>%s</strong> is supported by: %s</p></div>',
		$currency_code,
		$gateways_list
	);
}

// ==========================================
// Example Usage Functions
// ==========================================

/**
 * Example: Get filtered currencies for your booking system
 * This integrates with your existing get_rtrb_currencies() function
 *
 * @return array Filtered currencies that work with at least one payment gateway
 */
function get_rtrb_currencies_with_gateway_support() {
	$all_currencies = get_rtrb_currencies(); // Your existing function

	// Filter to show only currencies supported by Stripe OR PayPal
	return filter_rtrb_currencies_by_gateway_support(
		$all_currencies,
		array( 'stripe', 'paypal' ),
		'any' // 'any' means at least one gateway must support it
	);
}

/**
 * Example: Get currencies supported by BOTH gateways only
 *
 * @return array Currencies supported by both Stripe and PayPal
 */
function get_rtrb_currencies_supported_by_both() {
	$all_currencies = get_rtrb_currencies(); // Your existing function

	// Filter to show only currencies supported by BOTH Stripe AND PayPal
	return filter_rtrb_currencies_by_gateway_support(
		$all_currencies,
		array( 'stripe', 'paypal' ),
		'all' // 'all' means all gateways must support it
	);
}

/**
 * Example: Check if current booking currency is supported
 *
 * @param string $currency_code Current booking currency code
 *
 * @return array Status of each gateway
 */
function check_rtrb_booking_currency_support( $currency_code ) {
	$support = get_rtrb_currency_payment_gateway_support( $currency_code );

	return array(
		'currency'           => $support['currency'],
		'stripe_available'   => $support['stripe'],
		'paypal_available'   => $support['paypal'],
		'has_any_gateway'    => $support['supported'],
		'available_gateways' => $support['gateways'],
		'message'            => get_rtrb_payment_gateway_availability_message( $currency_code ),
	);
}
