//
//  AtTableApp.swift
//  AtTable
//
//  Created by Jared Evans on 12/24/25.
//

import SwiftUI

@main
struct AtTableApp: App {
    @AppStorage("isOnboardingComplete") var isOnboardingComplete: Bool = false
    @AppStorage("userName") var userName: String = ""
    @AppStorage("userRole") var userRole: UserRole = .hearing
    @AppStorage("userColorHex") var userColorHex: String = "#00008B"
    
    // APP-LEVEL SESSION: Ensures stable identity (Instance ID) across view reloads
    @StateObject var multipeerSession = MultipeerSession()
    
    init() {
        // Reset navigation state on launch so users always see the "Login" screen first.
        // This prevents getting "stuck" in a connecting loop if the app crashed.
        UserDefaults.standard.set(false, forKey: "isOnboardingComplete")
    }
    
    var body: some Scene {
        WindowGroup {
            Group {
                if isOnboardingComplete {
                    ChatView(
                        userName: userName,
                        userRole: userRole,
                        userColorHex: userColorHex
                    )
                } else {
                    OnboardingView(
                        isOnboardingComplete: $isOnboardingComplete,
                        userName: $userName,
                        userRole: $userRole,
                        userColorHex: $userColorHex
                    )
                }
            }
            .environmentObject(multipeerSession)
        }
    }
}
