import Foundation
import Observation
import SwiftData

/// Composition root for the Mac app. Owns the SwiftData cache container and the
/// iCloud sync engine only — no watch bridge, HealthKit, ActivityKit, backup, or
/// reminders/speech. The Mac's MVP is browsing and managing the library
/// (routines/schedules/exercises); it never runs a workout. Injected into the
/// view tree via `.environment(...)`.
@Observable
@MainActor
final class MacAppServices {
    let container: ModelContainer
    let syncEngine: SyncEngine

    private var bootstrapTask: Task<Void, Never>?

    init() {
        let container = WorkoutsModelContainer.make()
        self.container = container
        self.syncEngine = SyncEngine(container: container)
    }

    /// Launch step: resolve iCloud and reconcile the cache. Idempotent — repeated
    /// callers await the same one-shot task.
    func bootstrap() async {
        if let bootstrapTask { await bootstrapTask.value; return }
        let task = Task { @MainActor [weak self] in
            guard let self else { return }
            await self.syncEngine.connect()
        }
        bootstrapTask = task
        await task.value
    }
}
