import SwiftUI

struct ContentView: View {
    @Environment(AppState.self) private var app
    #if os(iOS)
    @State private var splashVisible = true
    #endif

    var body: some View {
        ZStack {
            mainContent
            #if os(iOS)
            if splashVisible {
                SplashView()
                    .zIndex(10)
                    .transition(.opacity.animation(.easeOut(duration: 0.55)))
            }
            #endif
        }
        .task { await boot() }
    }

    @ViewBuilder
    private var mainContent: some View {
        Group {
            if app.auth.isLoggedIn {
                MainView()
            } else {
                LoginView()
            }
        }
        #if os(iOS)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        #else
        .frame(minWidth: 900, minHeight: 600)
        #endif
    }

    private func boot() async {
        #if os(iOS)
        // Run bootstrap and minimum splash time in parallel;
        // dismiss splash only after BOTH complete.
        await withTaskGroup(of: Void.self) { group in
            group.addTask { await app.bootstrap() }
            group.addTask { try? await Task.sleep(for: .seconds(1.2)) }
            await group.waitForAll()
        }
        withAnimation { splashVisible = false }
        #else
        await app.bootstrap()
        #endif
    }
}
