// // FluxReaderAPI.swift // FluxRead // // Created by RSA on 13/01/2026. // import Foundation import Alamofire import UIKit class MedMapAPI { private let apiClient: APIClient = DIContainer.shared.resolve() private let baseURL: String init(baseURL: String = "https://medmap.desynced.net") { self.baseURL = baseURL } // MARK: - Auth func login(email: String, password: String) async throws -> LoginResponse { let url = "\(baseURL)/api/auth/login" return try await apiClient.request(url, method: .post, parameters: LoginRequest(email: email, password: password)) } func signup(email: String, password: String, role: Role) async throws -> LoginResponse { let url = "\(baseURL)/api/auth/signup" return try await apiClient.request(url, method: .post, parameters: SignupRequest(email: email, password: password, role: role)) } func logout() async throws { let url = "\(baseURL)/api/auth/logout" try await apiClient.requestVoid(url, method: .post, parameters: LogoutRequest()) } // MARK: - Companies func createCompany(request: CreateCompanyRequest) async throws -> CompanyDTO { let url = "\(baseURL)/api/companies" return try await apiClient.request(url, method: .post, parameters: request) } // MARK: - Professionals func createProfessional(request: CreateProfessionalRequest) async throws -> ProfessionalResponse { let url = "\(baseURL)/api/professionals" return try await apiClient.request(url, method: .post, parameters: request) } func listProfessionals(companyId: String) async throws -> [ProfessionalResponse] { let url = "\(baseURL)/api/professionals/list" return try await apiClient.request(url, method: .post, parameters: ListProfessionalsRequest(companyId: companyId)) } // MARK: - Patients func createPatient(request: CreatePatientRequest) async throws -> PatientDTO { let url = "\(baseURL)/api/patients" return try await apiClient.request(url, method: .post, parameters: request) } func getPatient(id: String) async throws -> PatientDTO { let url = "\(baseURL)/api/patients/\(id)" return try await apiClient.request(url, method: .get) } func getMyPatientProfile() async throws -> PatientDTO { let url = "\(baseURL)/api/patients/me" return try await apiClient.request(url, method: .get) } func getMyMonthlyHistory(date: String? = nil) async throws -> PatientMonthlyHistoryResponse { var components = URLComponents(string: "\(baseURL)/api/patients/me/history") if let date { components?.queryItems = [URLQueryItem(name: "date", value: date)] } guard let url = components?.url else { throw URLError(.badURL) } return try await apiClient.request(url, method: .get) } func listPatients(professionalId: String) async throws -> PatientsByProfessionalResponse { let url = "\(baseURL)/api/patients/list" return try await apiClient.request(url, method: .post, parameters: ListPatientsRequest(professionalId: professionalId)) } // MARK: - Prescriptions func createPrescription(request: CreatePrescriptionRequest) async throws -> PrescriptionResponse { let url = "\(baseURL)/api/prescriptions/create" return try await apiClient.request(url, method: .post, parameters: request) } func changePrescriptionSchedule(request: ChangePrescriptionScheduleRequest) async throws -> PrescriptionScheduleResponse { let url = "\(baseURL)/api/prescriptions/schedule" return try await apiClient.request(url, method: .put, parameters: request) } func listPrescriptions(patientId: String, activeOnly: Bool = false, scheduleFilter: ScheduleFilter = .activeOnly) async throws -> [PrescriptionWithSchedulesResponse] { let url = "\(baseURL)/api/prescriptions/list" let request = ListPrescriptionsRequest(patientId: patientId, activeOnly: activeOnly, scheduleFilter: scheduleFilter) return try await apiClient.request(url, method: .post, parameters: request) } func listMyActivePrescriptions() async throws -> [PrescriptionWithSchedulesResponse] { let url = "\(baseURL)/api/prescriptions/my-active" return try await apiClient.request(url, method: .get) } // MARK: - Medications func listMedications() async throws -> ListOfMedicationsResponse { let url = "\(baseURL)/api/medication/list" return try await apiClient.request(url, method: .get) } } private struct LogoutRequest: Encodable {}