import SwiftUI

/// T-iOS-15 · App entry: assemble the production dependency graph once and
/// hand it to the coordinator (Pairing → SessionList → Terminal). All wiring
/// lives under `Wiring/`; this file stays a thin `@main`.
///
/// T-iOS-21（增量接线，任务允许的最小改动）：
/// - `@UIApplicationDelegateAdaptor` 挂 `PushAppDelegate`（remote-notification
///   回调只能走 UIApplicationDelegate）；SwiftUI 生命周期保证本 `init` 先于
///   `didFinishLaunching` 运行，coordinator 经一次性静态槽交接过去，在启动
///   完成前就把通知 delegate 设好（冷启动动作不丢）。
/// - scenePhase 转 `.active`（含每次可见启动）→ 幂等 `activate()`（授权 +
///   token 注册补账）；后台拉起（锁屏 Allow/Deny）不激活场景，正好不注册。
@main
struct WebTermApp: App {
    @UIApplicationDelegateAdaptor(PushAppDelegate.self) private var pushDelegate
    @Environment(\.scenePhase) private var scenePhase
    @State private var coordinator: AppCoordinator

    init() {
        let coordinator = AppCoordinator(environment: .production())
        _coordinator = State(initialValue: coordinator)
        PushAppDelegate.bootstrap = coordinator // 消费点：didFinishLaunching
    }

    var body: some Scene {
        WindowGroup {
            RootView(coordinator: coordinator)
                .onChange(of: scenePhase) { _, phase in
                    guard phase == .active else { return }
                    pushDelegate.activatePush()
                }
        }
    }
}
