import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/crypto_address.dart';
import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/methods/iban.dart';
import 'package:pshared/models/payment/methods/russian_bank.dart';
import 'package:pshared/models/payment/quote/quote.dart';


class PaymentQuotationCurrencyResolver {
  static String? resolveQuoteCurrency({
    PaymentQuote? quote,
    PaymentMethodData? paymentData,
  }) {
    final quoteCurrency = _normalizeCurrency(
      quote?.amounts?.destinationSettlement?.currency.isoCode,
    );
    if (quoteCurrency != null) return quoteCurrency;

    if (paymentData == null) return null;

    final metadataCurrency = _normalizeCurrency(
      paymentData.metadata?['currency'],
    );
    if (metadataCurrency != null) return metadataCurrency;

    if (paymentData is CryptoAddressPaymentMethod) {
      return _normalizeCurrency(paymentData.asset?.tokenSymbol);
    }

    if (paymentData is RussianBankAccountPaymentMethod ||
        paymentData is CardPaymentMethod) {
      return 'RUB';
    }

    if (paymentData is IbanPaymentMethod) {
      return 'EUR';
    }

    return null;
  }

  static bool isFxEnabled({
    required String? sourceCurrency,
    required String? quoteCurrency,
  }) {
    final normalizedSource = _normalizeCurrency(sourceCurrency);
    final normalizedQuote = _normalizeCurrency(quoteCurrency);
    if (normalizedSource == null || normalizedQuote == null) return false;
    return normalizedSource != normalizedQuote;
  }

  static String? _normalizeCurrency(String? value) {
    final normalized = value?.trim().toUpperCase();
    if (normalized == null || normalized.isEmpty) return null;
    return normalized;
  }
}
