import CloudKit
import Foundation
import os.log

private let logger = Logger(subsystem: "ro.xdev.healthprobe", category: "CloudKitSyncService")

// CloudKit is treated as storage only — no ordering, no conflict resolution.
// Snapshots are identified by deviceID + snapshotID, not by chain position.
// On read, deduplicate by id BEFORE any chain traversal.
// Never assume chronological order from CloudKit fetch results.
final class CloudKitSyncService {
    static let shared = CloudKitSyncService()

    private let containerID = "iCloud.ro.xdev.healthprobe"

    func checkAvailability() async -> Bool {
        do {
            let status = try await CKContainer(identifier: containerID).accountStatus()
            return status == .available
        } catch {
            logger.error("CloudKit availability check failed: \(error)")
            return false
        }
    }
}
