import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../core/network/dio_client.dart';
import '../../data/datasources/approvals_datasource.dart';
import '../../data/repositories/approvals_repository_impl.dart';
import '../../domain/entities/approval.dart';
import '../../domain/repositories/approvals_repository.dart';
import '../../domain/usecases/get_pending_approvals.dart';
import '../../domain/usecases/approve_command.dart';
import '../../domain/usecases/reject_command.dart';

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

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

final approvalsRepositoryProvider = Provider<ApprovalsRepository>((ref) {
  final datasource = ref.watch(approvalsDatasourceProvider);
  return ApprovalsRepositoryImpl(datasource);
});

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

final getPendingApprovalsUseCaseProvider =
    Provider<GetPendingApprovals>((ref) {
  return GetPendingApprovals(ref.watch(approvalsRepositoryProvider));
});

final approveCommandUseCaseProvider = Provider<ApproveCommand>((ref) {
  return ApproveCommand(ref.watch(approvalsRepositoryProvider));
});

final rejectCommandUseCaseProvider = Provider<RejectCommand>((ref) {
  return RejectCommand(ref.watch(approvalsRepositoryProvider));
});

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

/// Provides the list of pending approvals.
final pendingApprovalsListProvider =
    FutureProvider<List<Approval>>((ref) async {
  final useCase = ref.watch(getPendingApprovalsUseCaseProvider);
  return useCase.execute();
});

/// Provides all approvals filtered by status.
final approvalsByStatusProvider =
    FutureProvider.family<List<Approval>, String>((ref, status) async {
  final repo = ref.watch(approvalsRepositoryProvider);
  return repo.getAll(status: status);
});
