using System.Net.Http.Json; using HealthcareManagerUiWebAssem.Entities; using HealthcareManagerUiWebAssem.Models; using HealthcareManagerUiWebAssem.Services.RequestHttp; namespace HealthcareManagerUiWebAssem.Services.PatientManagement; public class PatientManagementService(IRequestHttpService requestHttpService) : IPatientManagementService { public async Task GetPatientProfileAsync(Guid patientId) { var response = await requestHttpService.GetAsync($"/api/Patients/{patientId}"); if (!response.IsSuccessStatusCode) return null; var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse?.Data; } public async Task UpdatePatientProfileAsync(Patient patient) { var response = await requestHttpService.PutAsync("/api/Patients", patient); return response.IsSuccessStatusCode; } public async Task DeletePatientProfileAsync(Guid patientId) { var response = await requestHttpService.DeleteAsync($"/api/Patients/{patientId}"); return response.IsSuccessStatusCode; } public async Task> GetAppointmentsAsync(Guid patientId) { var response = await requestHttpService.GetAsync($"/api/Appointments/patient/{patientId}"); if (!response.IsSuccessStatusCode) return new List(); var apiResponse = await response.Content.ReadFromJsonAsync>>(); return apiResponse?.Data ?? []; } public async Task> BookAppointmentAsync(NewAppointmentModel newAppointment) { var response = await requestHttpService.PostAsync("/api/Appointments", newAppointment); var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse; } public async Task CancelAppointmentAsync(Guid doctorId, Guid patientId, DateTime appointmentDate) { var cancellationRequest = new AppointmentCancellationRequest { DoctorId = doctorId, PatientId = patientId, Appointment = appointmentDate }; var response = await requestHttpService.PutAsync("/api/Appointments", cancellationRequest); return response.IsSuccessStatusCode; } // Medical history methods public async Task GetMedicalHistoryAsync(Guid userId) { var response = await requestHttpService.GetAsync($"/api/MedicalHistory/user/{userId}"); if (!response.IsSuccessStatusCode) return null; var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse.Data; } public async Task> UploadMedicalHistoryAsync(Guid userId, byte[] content) { var requestBody = new { UserId = userId, Content = content }; var response = await requestHttpService.PostAsync("/api/MedicalHistory", requestBody); var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse; } public async Task DeleteMedicalHistoryAsync(Guid fileId) { var response = await requestHttpService.DeleteAsync($"/api/MedicalHistory/{fileId}"); return response.IsSuccessStatusCode; } public async Task DownloadMedicalHistoryAsync(Guid fileId) { var response = await requestHttpService.GetAsync($"/api/MedicalHistory/{fileId}"); if (response.IsSuccessStatusCode) return await response.Content.ReadAsByteArrayAsync(); return null; } public async Task> GetAllDoctorsAsync() { var response = await requestHttpService.GetAsync("/api/Doctors"); if (!response.IsSuccessStatusCode) return []; var apiResponse = await response.Content.ReadFromJsonAsync>>(); return apiResponse?.Data ?? []; } public async Task CheckForAccessAsync(Guid medicalRecordId, Guid doctorId) { var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId }; var response = await requestHttpService.PostAsync("/api/MedicalHistory/check_access", requestBody); return response.IsSuccessStatusCode; } public async Task GrantAccessAsync(Guid medicalRecordId, Guid doctorId) { var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId }; var response = await requestHttpService.PutAsync("/api/MedicalHistory/grant_access", requestBody); if (!response.IsSuccessStatusCode) return false; var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse.StatusCode == HttpStatusCodes.OK; } public async Task RevokeAccessAsync(Guid medicalRecordId, Guid doctorId) { var requestBody = new { MedicalRecordId = medicalRecordId, DoctorId = doctorId }; var response = await requestHttpService.PutAsync("/api/MedicalHistory/revoke_access", requestBody); if (!response.IsSuccessStatusCode) return false; var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse.StatusCode == HttpStatusCodes.OK; } public async Task SendMessageAsync(Guid senderId, Guid receiverId, string message) { var requestBody = new { sender = senderId, receiver = receiverId, message }; var response = await requestHttpService.PostAsync("/api/Chat/send_message", requestBody); return response.IsSuccessStatusCode; } public async Task GetConversationAsync(Guid userId1, Guid userId2) { var requestBody = new { idUser1 = userId1, idUser2 = userId2 }; var response = await requestHttpService.PostAsync("/api/Chat/get_conversation", requestBody); if (!response.IsSuccessStatusCode) return null; var apiResponse = await response.Content.ReadFromJsonAsync>(); return apiResponse?.Data; } }