import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../core/network/dio_client.dart';
import '../../data/datasources/servers_datasource.dart';
import '../../data/repositories/servers_repository_impl.dart';
import '../../domain/entities/server.dart';
import '../../domain/repositories/servers_repository.dart';

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

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

final serversRepositoryProvider = Provider<ServersRepository>((ref) {
  final datasource = ref.watch(serversDatasourceProvider);
  return ServersRepositoryImpl(datasource);
});

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

/// Provides the full list of servers.
final serversListProvider = FutureProvider<List<Server>>((ref) async {
  final repo = ref.watch(serversRepositoryProvider);
  return repo.getAll();
});

/// Provides servers filtered by environment.
final serversByEnvironmentProvider =
    FutureProvider.family<List<Server>, String>((ref, environment) async {
  final repo = ref.watch(serversRepositoryProvider);
  return repo.getAll(environment: environment);
});

/// Provides a single server by ID.
final serverByIdProvider =
    FutureProvider.family<Server, String>((ref, id) async {
  final repo = ref.watch(serversRepositoryProvider);
  return repo.getById(id);
});
