import SwiftUI
import AppFeature

/// Thin @main entry: window + DI only. 기본 = 본 서버(GPU DS) 라이브 결선(AppModel.live —
/// LiveDSClient + 실 AIFabric 라우터, base 기본 publicTLS = https://document.hyungi.net/api).
/// env 스위치: DSAPP_FIXTURE=1 → 오프라인 스캐폴드(Fixture+Mock) / DSAPP_DS_URL → base 오버라이드
/// (예: http://100.110.63.63:8000/api). Feature logic lives in AppFeature, keeping the seam to a
/// future iPhone/Watch target trivial.
@main
struct DSApp: App {
    @State private var model: AppModel

    @MainActor
    init() {
        let env = ProcessInfo.processInfo.environment
        let initial: AppModel
        if env["DSAPP_FIXTURE"] == "1" {
            initial = .preview
        } else if let raw = env["DSAPP_DS_URL"] {
            // dev 전용 오버라이드 — 파싱 실패 시 prod(publicTLS)로 silent fallback 금지, 즉사.
            let trimmed = raw.hasSuffix("/") ? String(raw.dropLast()) : raw
            guard let url = URL(string: trimmed), url.scheme != nil, url.host() != nil else {
                fatalError("DSAPP_DS_URL 파싱 실패: \(raw)")
            }
            initial = .live(base: .custom(url))
        } else {
            initial = .live()
        }
        _model = State(initialValue: initial)
    }

    var body: some Scene {
        WindowGroup {
            RootView()
                .environment(model)
                .frame(minWidth: 980, minHeight: 640)
        }
        .windowStyle(.automatic)
    }
}
