import SwiftUI
internal import CoreData

@main
struct LotteryTrackerApp: App {
    @StateObject private var persistenceController = PersistenceController.shared
    @StateObject private var appState = AppState()
    
    // 应用生命周期
    @Environment(\.scenePhase) private var scenePhase
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
                .environmentObject(appState)
                .onAppear {
                    setupAppearance()
                    // App启动时检查一次
//                    if appState.autoCheckDraw {
//                        checkDraws()
//                    }
                }
                .onChange(of: scenePhase) { oldPhase, newPhase in
                    // 只记录，不修改任何状态
                    switch newPhase {
                    case .active:
                        print("App变为活跃状态")
//                        if appState.autoCheckDraw {
//                            checkDraws()
//                        }
                    case .inactive:
                        print("App变为不活跃状态")
                    case .background:
                        print("App进入后台")
                    @unknown default:
                        break
                    }
                }
                .sheet(isPresented: $appState.showingOnboarding) {
                    OnboardingView()
                }
        }
    }
    
    // 设置外观
    private func setupAppearance() {
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.configureWithOpaqueBackground()
        UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        UITabBar.appearance().standardAppearance = tabBarAppearance
        
        let navigationBarAppearance = UINavigationBarAppearance()
        navigationBarAppearance.configureWithOpaqueBackground()
        UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
        UINavigationBar.appearance().standardAppearance = navigationBarAppearance
    }
    
    // 检查开奖
    private func checkDraws() {
        print("🔍 检查开奖...")
        let checker = LotteryChecker(context: persistenceController.container.viewContext)
        checker.checkDrawResults()
    }
}

// 主内容视图
struct ContentView: View {
    @EnvironmentObject private var appState: AppState
    
    var body: some View {
        TabView(selection: $appState.selectedTab) {
            // 首页界面
            HomeView()
                .tabItem {
                    Label("首页", systemImage: "house.fill")
                }
                .tag(Tab.home)
            // 统计界面
            StatisticsView()
                .tabItem {
                    Label("统计", systemImage: "chart.bar.fill")
                }
                .tag(Tab.statistics)
            // 设置界面
            SettingsView()
                .tabItem {
                    Label("设置", systemImage: "gearshape.fill")
                }
                .tag(Tab.settings)
            // 我的界面
            MyView()
                .tabItem {
                    Label("我的", systemImage: "person.fill")
                }
                .tag(Tab.my)
        }
        .accentColor(.blue)
    }
}
