import 'package:pshared/models/wallet/wallet.dart' as domain;

import 'package:pweb/models/currency.dart';
import 'package:pweb/models/wallet.dart';


extension WalletUiMapper on domain.WalletModel {
  Wallet toUi() {
    final amountStr = availableMoney?.amount ?? balance?.available?.amount ?? '0';
    final currencyStr = availableMoney?.currency ?? balance?.available?.currency ?? Currency.usd.toString().toUpperCase();
    final parsedAmount = double.tryParse(amountStr) ?? 0;
    final currency = Currency.values.firstWhere(
      (c) => c.name.toUpperCase() == currencyStr.toUpperCase(),
      orElse: () => Currency.usd,
    );
    return Wallet(
      id: walletRef,
      walletUserID: walletRef,
      name: metadata?['name'] ?? walletRef,
      balance: parsedAmount,
      currency: currency,
      isHidden: true,
      calculatedAt: balance?.calculatedAt ?? DateTime.now(),
    );
  }
}
