//
// ApiService.swift
// IceGlass
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import Foundation

typealias ApiServiceCallback = (_ jsonData: Data, _ response: URLResponse) -> Void

class ApiService: @unchecked Sendable {
    private let logger = IceGlassLogger(
        subsystem: Bundle.main.bundleIdentifier ?? "dev.rzen.indie.IceGlass",
        category: "ApiService"
    )

    let url: URL
    let callback: ApiServiceCallback

    var previousFetchTime: Date = .distantPast

    init(url: URL?, callback: @escaping ApiServiceCallback) {
        guard let url = url else {
            preconditionFailure("ApiService initialised with nil URL — caller bug.")
        }
        self.url = url
        self.callback = callback
    }

    func fetch() async {
        do {
            let (data, response) = try await URLSession.shared.data(from: url)
            logger.info("Polling: \(self.url)")
            logger.debug("Previous: \(Date.now.timeIntervalSince(previousFetchTime).humanReadableTime())")
            previousFetchTime = Date.now
            callback(data, response)
        } catch {
            logger.error("Invalid response from \(self.url) \(error)")
        }
    }
}
