import Foundation
import WireProtocol

/// T-iOS-15 · Production `HTTPTransport` (the WireProtocol seam's doc reserves
/// the URLSession wrapper for the production side; no package owns it, so the
/// assembly layer provides it). Deliberately logic-free: `APIClient` builds
/// every request — including the Origin-iff-G rule (plan §3.4 铁律) — and this
/// type only performs the exchange. Adding ANY header/URL logic here would
/// bypass that single audited point (review CRITICAL).
struct URLSessionHTTPTransport: HTTPTransport {
    private let session: URLSession

    /// Default is EPHEMERAL, not `.shared` (T-iOS-19 finding): RO GET bodies
    /// include `/live-sessions/:id/preview` — raw terminal ring-buffer bytes
    /// that may contain printed secrets — and `.shared`'s default URLCache
    /// writes responses to disk. Ephemeral keeps them memory-only, matching
    /// the WS transport and the privacy-shade posture.
    init(session: URLSession = URLSession(configuration: .ephemeral)) {
        self.session = session
    }

    func send(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) {
        let (data, response) = try await session.data(for: request)
        guard let httpResponse = response as? HTTPURLResponse else {
            // http(s)-only endpoints (HostEndpoint validates) always produce
            // an HTTPURLResponse; anything else is a transport-level anomaly.
            throw URLError(.badServerResponse)
        }
        return (data, httpResponse)
    }
}
