import 'package:injectable/injectable.dart';
import 'package:krow/core/application/clients/api/api_client.dart';
import 'package:krow/core/data/models/staff/bank_acc.dart';
import 'package:krow/features/profile/bank_account/data/gql.dart';

@injectable
class BankAccountApiProvider {
  final ApiClient _apiClient;

  BankAccountApiProvider(this._apiClient);

  Stream<BankAcc?> getStaffBankAcc() async* {
    await for (var response
        in _apiClient.queryWithCache(schema: staffBankAccount)) {
      if (response == null || response.data == null) {
        continue;
      }
      if (response.hasException) {
        throw Exception(response.exception.toString());
      }
      final bankAcc =
          BankAcc.fromJson(response.data?['me']?['bank_account'] ?? {});
      yield bankAcc;
    }
  }

  Future<void> putBunkAccount(BankAcc bankAcc) async {
    final Map<String, dynamic> variables = {
      'input': bankAcc.toJson(),
    };

    var result =
        await _apiClient.mutate(schema: updateBankAccount, body: variables);

    if (result.hasException) {
      throw Exception(result.exception.toString());
    }
  }
}
