import SwiftUI
import SwiftData

@main
struct NahbarApp: App {
    @StateObject private var callWindowManager = CallWindowManager.shared
    @StateObject private var appLockManager = AppLockManager.shared
    @AppStorage("activeThemeID") private var activeThemeIDRaw: String = ThemeID.linen.rawValue
    @Environment(\.scenePhase) private var scenePhase
    @State private var showSplash = true

    private var activeTheme: NahbarTheme {
        NahbarTheme.theme(for: ThemeID(rawValue: activeThemeIDRaw) ?? .linen)
    }

    var body: some Scene {
        WindowGroup {
            ZStack {
                ContentView()
                    .environmentObject(callWindowManager)
                    .environmentObject(appLockManager)

                if appLockManager.isLocked && !showSplash {
                    AppLockView()
                        .environmentObject(appLockManager)
                        .transition(.opacity)
                        .zIndex(1)
                }

                if showSplash {
                    SplashView(onFinished: {
                            appLockManager.lockIfEnabled()
                            showSplash = false
                        })
                        .transition(.opacity)
                        .zIndex(2)
                }
            }
            .animation(.easeInOut(duration: 0.25), value: appLockManager.isLocked)
            .animation(.easeInOut(duration: 0.4), value: showSplash)
            .environment(\.nahbarTheme, activeTheme)
            .tint(activeTheme.accent)
            .onAppear { applyTabBarAppearance(activeTheme) }
            .onChange(of: activeThemeIDRaw) { _, _ in applyTabBarAppearance(activeTheme) }
        }
        .modelContainer(AppGroup.makeMainContainerWithMigration())
        .onChange(of: scenePhase) { _, phase in
            if phase == .background {
                appLockManager.lockIfEnabled()
            }
        }
    }

    private func applyTabBarAppearance(_ theme: NahbarTheme) {
        let bg       = UIColor(theme.backgroundPrimary).withAlphaComponent(0.88)
        let normal   = UIColor(theme.contentTertiary)
        let selected = UIColor(theme.accent)
        let border   = UIColor(theme.borderSubtle).withAlphaComponent(0.6)

        // Tab bar
        let item = UITabBarItemAppearance()
        item.normal.iconColor = normal
        item.normal.titleTextAttributes   = [.foregroundColor: normal]
        item.selected.iconColor = selected
        item.selected.titleTextAttributes = [.foregroundColor: selected]

        let tabAppearance = UITabBarAppearance()
        tabAppearance.configureWithTransparentBackground()
        tabAppearance.backgroundColor = bg
        tabAppearance.shadowColor = border
        tabAppearance.stackedLayoutAppearance       = item
        tabAppearance.inlineLayoutAppearance        = item
        tabAppearance.compactInlineLayoutAppearance = item

        UITabBar.appearance().standardAppearance   = tabAppearance
        UITabBar.appearance().scrollEdgeAppearance = tabAppearance

        // Navigation bar
        let navBg = UIColor(theme.backgroundPrimary).withAlphaComponent(0.92)
        let titleColor = UIColor(theme.contentPrimary)

        let navAppearance = UINavigationBarAppearance()
        navAppearance.configureWithTransparentBackground()
        navAppearance.backgroundColor = navBg
        navAppearance.shadowColor = border
        navAppearance.titleTextAttributes      = [.foregroundColor: titleColor]
        navAppearance.largeTitleTextAttributes = [.foregroundColor: titleColor]

        UINavigationBar.appearance().standardAppearance   = navAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navAppearance
        UINavigationBar.appearance().compactAppearance    = navAppearance
        UINavigationBar.appearance().tintColor = selected
    }
}
