import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:real_estate_mobile/features/agents/data/agents_repository.dart'; class AgentNetworkState { final List> pendingRequests; final List> connections; final bool isLoading; final String? error; final Set processingIds; const AgentNetworkState({ this.pendingRequests = const [], this.connections = const [], this.isLoading = true, this.error, this.processingIds = const {}, }); AgentNetworkState copyWith({ List>? pendingRequests, List>? connections, bool? isLoading, String? error, Set? processingIds, bool clearError = false, }) { return AgentNetworkState( pendingRequests: pendingRequests ?? this.pendingRequests, connections: connections ?? this.connections, isLoading: isLoading ?? this.isLoading, error: clearError ? null : (error ?? this.error), processingIds: processingIds ?? this.processingIds, ); } } class AgentNetworkNotifier extends StateNotifier { final AgentsRepository _repo; AgentNetworkNotifier(this._repo) : super(const AgentNetworkState()) { loadData(); } Future loadData() async { state = state.copyWith(isLoading: true, clearError: true); try { final results = await Future.wait([ _repo.getReceivedRequests('PENDING'), _repo.getReceivedRequests('ACCEPTED'), ]); state = state.copyWith( pendingRequests: results[0], connections: results[1], isLoading: false, ); } catch (e) { state = state.copyWith( isLoading: false, error: 'Failed to load network data', ); } } Future acceptRequest(String requestId) async { if (state.processingIds.contains(requestId)) return; state = state.copyWith( processingIds: {...state.processingIds, requestId}, ); final success = await _repo.respondToRequest(requestId, 'ACCEPTED'); if (success) { // Move from pending to connections final request = state.pendingRequests.firstWhere( (r) => r['id'] == requestId, orElse: () => {}, ); state = state.copyWith( pendingRequests: state.pendingRequests.where((r) => r['id'] != requestId).toList(), connections: request.isNotEmpty ? [request, ...state.connections] : state.connections, processingIds: state.processingIds .where((id) => id != requestId) .toSet(), ); } else { state = state.copyWith( processingIds: state.processingIds .where((id) => id != requestId) .toSet(), ); } } Future rejectRequest(String requestId) async { if (state.processingIds.contains(requestId)) return; state = state.copyWith( processingIds: {...state.processingIds, requestId}, ); final success = await _repo.respondToRequest(requestId, 'REJECTED'); if (success) { state = state.copyWith( pendingRequests: state.pendingRequests.where((r) => r['id'] != requestId).toList(), processingIds: state.processingIds .where((id) => id != requestId) .toSet(), ); } else { state = state.copyWith( processingIds: state.processingIds .where((id) => id != requestId) .toSet(), ); } } } final agentNetworkProvider = StateNotifierProvider.autoDispose( (ref) => AgentNetworkNotifier(AgentsRepository()), );