import 'package:dio/dio.dart'; import 'package:real_estate_mobile/core/constants/api_constants.dart'; import 'package:real_estate_mobile/core/network/api_client.dart'; import 'package:real_estate_mobile/core/network/api_exceptions.dart'; import 'package:real_estate_mobile/features/agents/data/models/agent_profile.dart'; import 'package:real_estate_mobile/features/agents/data/models/agent_type.dart'; class SearchAgentsResponse { final List data; final int total; final int page; final int limit; final int totalPages; const SearchAgentsResponse({ required this.data, required this.total, required this.page, required this.limit, required this.totalPages, }); factory SearchAgentsResponse.fromJson(Map json) { return SearchAgentsResponse( data: (json['data'] as List) .map((e) => AgentProfile.fromJson(e as Map)) .toList(), total: json['total'] as int? ?? 0, page: json['page'] as int? ?? 1, limit: json['limit'] as int? ?? 10, totalPages: json['totalPages'] as int? ?? 1, ); } } class AgentsRepository { final Dio _dio = ApiClient.instance.dio; Future searchAgents({ String? agentTypeId, int page = 1, int limit = 10, String? sortBy, String? sortOrder, }) async { try { final queryParams = { 'page': page, 'limit': limit, }; if (agentTypeId != null) queryParams['agentTypeId'] = agentTypeId; if (sortBy != null) queryParams['sortBy'] = sortBy; if (sortOrder != null) queryParams['sortOrder'] = sortOrder; final response = await _dio.get( ApiConstants.agents, queryParameters: queryParams, ); final data = response.data['data'] as Map; return SearchAgentsResponse.fromJson(data); } on DioException catch (e) { if (e.error is ApiException) { throw e.error as ApiException; } throw ApiException( message: e.message ?? 'Failed to fetch agents', statusCode: e.response?.statusCode, ); } } Future> getAgentTypes() async { try { final response = await _dio.get(ApiConstants.agentTypes); final data = response.data['data'] as List; return data .map((e) => AgentType.fromJson(e as Map)) .toList(); } on DioException catch (e) { if (e.error is ApiException) { throw e.error as ApiException; } throw ApiException( message: e.message ?? 'Failed to fetch agent types', statusCode: e.response?.statusCode, ); } } }