import 'package:hesabix_ui/core/api_client.dart'; import 'package:hesabix_ui/models/support_models.dart'; import 'package:dio/dio.dart'; class SupportService { final ApiClient _apiClient; SupportService(this._apiClient); // Categories Future> getCategories() async { try { final response = await _apiClient.get>('/api/v1/metadata/categories'); return (response.data!['data'] as List).map((json) => SupportCategory.fromJson(json)).toList(); } on DioException catch (e) { throw _handleError(e); } } // Priorities Future> getPriorities() async { try { final response = await _apiClient.get>('/api/v1/metadata/priorities'); return (response.data!['data'] as List).map((json) => SupportPriority.fromJson(json)).toList(); } on DioException catch (e) { throw _handleError(e); } } // Statuses Future> getStatuses() async { try { final response = await _apiClient.get>('/api/v1/metadata/statuses'); return (response.data!['data'] as List).map((json) => SupportStatus.fromJson(json)).toList(); } on DioException catch (e) { throw _handleError(e); } } // User tickets Future> searchUserTickets(Map queryInfo) async { try { final response = await _apiClient.post>( '/api/v1/support/search', data: queryInfo, ); return PaginatedResponse.fromJson( response.data!['data'], (json) => SupportTicket.fromJson(json), ); } on DioException catch (e) { throw _handleError(e); } } Future createTicket(CreateTicketRequest request) async { try { final response = await _apiClient.post>( '/api/v1/support', data: request.toJson(), ); return SupportTicket.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future getTicket(int ticketId) async { try { final response = await _apiClient.get>('/api/v1/support/$ticketId'); return SupportTicket.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future sendMessage(int ticketId, CreateMessageRequest request) async { try { final response = await _apiClient.post>( '/api/v1/support/$ticketId/messages', data: request.toJson(), ); return SupportMessage.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future> searchTicketMessages( int ticketId, Map queryInfo, ) async { try { final response = await _apiClient.post>( '/api/v1/support/$ticketId/messages/search', data: queryInfo, ); return PaginatedResponse.fromJson( response.data!['data'], (json) => SupportMessage.fromJson(json), ); } on DioException catch (e) { throw _handleError(e); } } // Operator tickets Future> searchOperatorTickets(Map queryInfo) async { try { final response = await _apiClient.post>( '/api/v1/support/operator/tickets/search', data: queryInfo, ); return PaginatedResponse.fromJson( response.data!['data'], (json) => SupportTicket.fromJson(json), ); } on DioException catch (e) { throw _handleError(e); } } Future getOperatorTicket(int ticketId) async { try { final response = await _apiClient.get>('/api/v1/support/operator/tickets/$ticketId'); return SupportTicket.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future updateTicketStatus(int ticketId, UpdateStatusRequest request) async { try { final response = await _apiClient.put>( '/api/v1/support/operator/tickets/$ticketId/status', data: request.toJson(), ); return SupportTicket.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future assignTicket(int ticketId, AssignTicketRequest request) async { try { final response = await _apiClient.post>( '/api/v1/support/operator/tickets/$ticketId/assign', data: request.toJson(), ); return SupportTicket.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future sendOperatorMessage(int ticketId, CreateMessageRequest request) async { try { final response = await _apiClient.post>( '/api/v1/support/operator/tickets/$ticketId/messages', data: request.toJson(), ); return SupportMessage.fromJson(response.data!['data']); } on DioException catch (e) { throw _handleError(e); } } Future> searchOperatorTicketMessages( int ticketId, Map queryInfo, ) async { try { final response = await _apiClient.post>( '/api/v1/support/operator/tickets/$ticketId/messages/search', data: queryInfo, ); return PaginatedResponse.fromJson( response.data!['data'], (json) => SupportMessage.fromJson(json), ); } on DioException catch (e) { throw _handleError(e); } } // Error handling Exception _handleError(DioException e) { if (e.response != null) { final data = e.response!.data; if (data is Map && data.containsKey('detail')) { return Exception(data['detail']); } } return Exception(e.message ?? 'خطای نامشخص'); } }