import '../../entities/profile/bank_account.dart';

/// Adapter for [BankAccount] to map data layer values to domain entity.
class BankAccountAdapter {
  /// Maps primitive values to [BankAccount].
  static BankAccount fromPrimitives({
    required String id,
    required String userId,
    required String bankName,
    required String? type,
    String? accountNumber,
    String? last4,
    String? sortCode,
    bool? isPrimary,
  }) {
    return BankAccount(
      id: id,
      userId: userId,
      bankName: bankName,
      accountNumber: accountNumber ?? '',
      accountName: '', // Not provided by backend
      last4: last4,
      sortCode: sortCode,
      type: _stringToType(type),
      isPrimary: isPrimary ?? false,
    );
  }

  static BankAccountType _stringToType(String? value) {
    if (value == null) return BankAccountType.checking;
    try {
      // Assuming backend enum names match or are uppercase
      return BankAccountType.values.firstWhere(
        (e) => e.name.toLowerCase() == value.toLowerCase(),
        orElse: () => BankAccountType.other,
      );
    } catch (_) {
      return BankAccountType.other;
    }
  }

  /// Converts domain type to string for backend.
  static String typeToString(BankAccountType type) {
    switch (type) {
      case BankAccountType.checking:
        return 'CHECKING';
      case BankAccountType.savings:
        return 'SAVINGS';
      default:
        return 'CHECKING';
    }
  }
}
