import SwiftUI

@main
struct MCMSMobileApp: App {
    @State private var env = AppEnvironment()

    var body: some Scene {
        WindowGroup {
            RootView()
                .environment(env)
        }
    }
}

/// Routes the app: no config → Settings; configured but signed out → Login;
/// authenticated → main tabs.
struct RootView: View {
    @Environment(AppEnvironment.self) private var env

    var body: some View {
        Group {
            if !env.isConfigured {
                NavigationStack { SettingsView(isInitialSetup: true) }
            } else if !env.isAuthenticated {
                LoginView()
            } else if let connection = env.connection {
                MainTabView(connection: connection)
            } else {
                NavigationStack { SettingsView(isInitialSetup: true) }
            }
        }
    }
}

struct MainTabView: View {
    let connection: MCMSConnection

    var body: some View {
        TabView {
            NavigationStack { DashboardView(connection: connection) }
                .tabItem { Label("Dashboard", systemImage: "chart.pie") }

            NavigationStack { ONUListView(connection: connection) }
                .tabItem { Label("ONUs", systemImage: "point.3.connected.trianglepath.dotted") }

            NavigationStack { OLTListView(connection: connection) }
                .tabItem { Label("OLTs", systemImage: "antenna.radiowaves.left.and.right") }

            NavigationStack { SettingsView(isInitialSetup: false) }
                .tabItem { Label("Settings", systemImage: "gearshape") }
        }
    }
}
