import 'package:pshared/data/mapper/wallet/ui.dart';
import 'package:pshared/models/currency.dart';
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/payment/chain_network.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/service/wallet.dart' as shared_wallet_service;


abstract class WalletsService {
  Future<List<Wallet>> getWallets(String organizationRef);
  Future<double> getBalance(String organizationRef, String walletRef);
  Future<void> create({
    required String organizationRef,
    required Describable describable,
    required ChainNetwork chain,
    required CurrencyCode currency,
    required String? ownerRef,
  });
}

class ApiWalletsService implements WalletsService {
  @override
  Future<List<Wallet>> getWallets(String organizationRef) async {
    final models = await shared_wallet_service.WalletService.list(organizationRef);
    return models.map((m) => m.toUi()).toList();
  }

  @override
  Future<double> getBalance(String organizationRef, String walletRef) async {
    final balance = await shared_wallet_service.WalletService.getBalance(
      organizationRef: organizationRef,
      walletRef: walletRef,
    );
    return balance.available?.toDouble() ?? 0;
  }

  @override
  Future<void> create({
    required String organizationRef,
    required Describable describable,
    required ChainNetwork chain,
    required CurrencyCode currency,
    required String? ownerRef,
  }) => shared_wallet_service.WalletService.create(
    organizationRef: organizationRef,
    describable: describable,
    chain: chain,
    currency: currency,
    ownerRef: ownerRef,
  );
}
