import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../core/network/dio_client.dart';
import '../../data/datasources/standing_orders_datasource.dart';
import '../../data/repositories/standing_orders_repository_impl.dart';
import '../../domain/entities/standing_order.dart';
import '../../domain/repositories/standing_orders_repository.dart';
import '../../domain/usecases/get_standing_orders.dart';
import '../../domain/usecases/toggle_standing_order.dart';

// ---------------------------------------------------------------------------
// Dependency providers
// ---------------------------------------------------------------------------

final standingOrdersDatasourceProvider =
    Provider<StandingOrdersDatasource>((ref) {
  final dio = ref.watch(dioClientProvider);
  return StandingOrdersDatasource(dio);
});

final standingOrdersRepositoryProvider =
    Provider<StandingOrdersRepository>((ref) {
  final datasource = ref.watch(standingOrdersDatasourceProvider);
  return StandingOrdersRepositoryImpl(datasource);
});

// ---------------------------------------------------------------------------
// Use case providers
// ---------------------------------------------------------------------------

final getStandingOrdersUseCaseProvider = Provider<GetStandingOrders>((ref) {
  return GetStandingOrders(ref.watch(standingOrdersRepositoryProvider));
});

final toggleStandingOrderUseCaseProvider =
    Provider<ToggleStandingOrder>((ref) {
  return ToggleStandingOrder(ref.watch(standingOrdersRepositoryProvider));
});

// ---------------------------------------------------------------------------
// Data providers
// ---------------------------------------------------------------------------

/// Provides the full list of standing orders.
final standingOrdersListProvider =
    FutureProvider<List<StandingOrder>>((ref) async {
  final useCase = ref.watch(getStandingOrdersUseCaseProvider);
  return useCase.execute();
});

/// Provides a single standing order by ID.
final standingOrderByIdProvider =
    FutureProvider.family<StandingOrder, String>((ref, id) async {
  final repo = ref.watch(standingOrdersRepositoryProvider);
  return repo.getById(id);
});
