import Foundation // MARK: - Service Protocol protocol TrainingPlanServiceProtocol: Sendable { func listPlans(type: PlanType?, difficulty: Difficulty?) async throws -> [TrainingPlan] func getPlan(id: String) async throws -> TrainingPlan func generatePlan(request: GeneratePlanRequest) async throws -> TrainingPlan func followPlan(id: String) async throws func unfollowPlan(id: String) async throws func updateSessionStatus(sessionId: String, status: SessionStatus) async throws } // MARK: - Default Service class TrainingPlanService: TrainingPlanServiceProtocol { private let baseURL: URL private let session: URLSession private let authToken: String? init( baseURL: URL = URL(string: "http://localhost:3000")!, session: URLSession = .shared, authToken: String? = nil ) { self.baseURL = baseURL self.session = session self.authToken = authToken } func listPlans(type: PlanType? = nil, difficulty: Difficulty? = nil) async throws -> [TrainingPlan] { var components = URLComponents(url: baseURL.appendingPathComponent("/api/training-plans"), resolvingAgainstBaseURL: true)! var queryItems: [URLQueryItem] = [] if let type = type { queryItems.append(URLQueryItem(name: "type", value: type.rawValue)) } if let difficulty = difficulty { queryItems.append(URLQueryItem(name: "difficulty", value: difficulty.rawValue)) } components.queryItems = queryItems let request = try buildRequest(url: components.url!) let (data, response) = try await session.data(for: request) try validateResponse(response) let decoded = try JSONDecoder().decode(TrainingPlanListResponse.self, from: data) return decoded.plans } func getPlan(id: String) async throws -> TrainingPlan { let url = baseURL.appendingPathComponent("/api/training-plans/\(id)") let request = try buildRequest(url: url) let (data, response) = try await session.data(for: request) try validateResponse(response) let decoded = try JSONDecoder().decode(TrainingPlanDetailResponse.self, from: data) return decoded.plan } func generatePlan(request: GeneratePlanRequest) async throws -> TrainingPlan { let url = baseURL.appendingPathComponent("/api/training-plans/generate") var request = try buildRequest(url: url, method: .post) request.httpBody = try JSONEncoder().encode(request) let (data, response) = try await session.data(for: request) try validateResponse(response) let decoded = try JSONDecoder().decode(GeneratePlanResponse.self, from: data) return decoded.plan } func followPlan(id: String) async throws { let url = baseURL.appendingPathComponent("/api/training-plans/\(id)/follow") let request = try buildRequest(url: url, method: .post) let (_, response) = try await session.data(for: request) try validateResponse(response) } func unfollowPlan(id: String) async throws { let url = baseURL.appendingPathComponent("/api/training-plans/\(id)/follow") let request = try buildRequest(url: url, method: .delete) let (_, response) = try await session.data(for: request) try validateResponse(response) } func updateSessionStatus(sessionId: String, status: SessionStatus) async throws { let url = baseURL.appendingPathComponent("/api/training-plans/sessions/\(sessionId)/status") var request = try buildRequest(url: url, method: .patch) let body = try JSONEncoder().encode(["status": status.rawValue]) request.httpBody = body let (_, response) = try await session.data(for: request) try validateResponse(response) } // MARK: - Helpers private func buildRequest(url: URL, method: HTTPMethod = .get, body: Data? = nil) throws -> URLRequest { var request = URLRequest(url: url) request.httpMethod = method.rawValue request.setValue("application/json", forHTTPHeaderField: "Content-Type") if let token = authToken { request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") } if let body = body { request.httpBody = body } return request } private func validateResponse(_ response: URLResponse) throws { guard let httpResponse = response as? HTTPURLResponse else { throw TrainingPlanError.invalidResponse } guard (200...299).contains(httpResponse.statusCode) else { switch httpResponse.statusCode { case 401: throw TrainingPlanError.unauthorized case 403: throw TrainingPlanError.forbidden case 404: throw TrainingPlanError.notFound case 429: throw TrainingPlanError.rateLimited case 500...599: throw TrainingPlanError.serverError(httpResponse.statusCode) default: throw TrainingPlanError.httpError(httpResponse.statusCode) } } } } // MARK: - Error Types enum TrainingPlanError: LocalizedError { case invalidResponse case unauthorized case forbidden case notFound case rateLimited case serverError(Int) case httpError(Int) case decodingError(Error) var errorDescription: String { switch self { case .invalidResponse: return "Invalid server response" case .unauthorized: return "Unauthorized — please log in again" case .forbidden: return "Forbidden — check permissions" case .notFound: return "Training plan not found" case .rateLimited: return "Too many requests — try again shortly" case .serverError(let code): return "Server error (\(code))" case .httpError(let code): return "HTTP error (\(code))" case .decodingError(let error): return "Decoding error: \(error.localizedDescription)" } } }