import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/features/report/transaction/domain/repositories/i_transaction_report_repository.dart';
import 'package:niogu_app/features/report/transaction/presentation/providers/transaction_report_provider.dart';

class TransactionReportController extends AutoDisposeAsyncNotifier<void> {
  late final ITransactionReportRepository _transactionReportRepository;
  @override
  FutureOr<void> build() {
    _transactionReportRepository = ref.read(
      transactionReportRepositoryProvider,
    );
  }

  Future<void> cancelOrder(String orderId) async {
    state = const AsyncValue.loading();

    final result = await AsyncValue.guard(() async {
      await _transactionReportRepository.cancelOrder(orderId);
    });

    state = result;

    if (result is AsyncError) throw result.error!;
  }

  Future<void> cancelPurchase(String purchaseId) async {
    state = const AsyncValue.loading();

    final result = await AsyncValue.guard(() async {
      await _transactionReportRepository.cancelPurchase(purchaseId);
    });

    state = result;

    if (result is AsyncError) throw result.error!;
  }
}
