import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/entities/share_account.dart';
import '../../domain/entities/global_state.dart';
import '../../domain/usecases/mining/get_share_account.dart';
import '../../domain/usecases/mining/get_global_state.dart';
import '../../core/di/injection.dart';

// Use Cases Providers
final getShareAccountUseCaseProvider = Provider<GetShareAccount>((ref) {
  return getIt<GetShareAccount>();
});

final getGlobalStateUseCaseProvider = Provider<GetGlobalState>((ref) {
  return getIt<GetGlobalState>();
});

// State Providers
final shareAccountProvider = FutureProvider.family<ShareAccount?, String>(
  (ref, accountSequence) async {
    final useCase = ref.watch(getShareAccountUseCaseProvider);
    final result = await useCase(accountSequence);
    return result.fold(
      (failure) => throw Exception(failure.message),
      (account) => account,
    );
  },
);

final globalStateProvider = FutureProvider<GlobalState?>((ref) async {
  final useCase = ref.watch(getGlobalStateUseCaseProvider);
  final result = await useCase();
  return result.fold(
    (failure) => throw Exception(failure.message),
    (state) => state,
  );
});

final currentPriceProvider = Provider<String>((ref) {
  final globalState = ref.watch(globalStateProvider);
  return globalState.when(
    data: (state) => state?.currentPrice ?? '0',
    loading: () => '0',
    error: (_, __) => '0',
  );
});
