import SwiftUI

struct MainTabView: View {
    @Environment(ConnectivityMonitor.self) private var connectivity
    @State private var selectedTab = 0
    private let navState = AppNavigationState.shared

    var body: some View {
        TabView(selection: $selectedTab) {
            Tab(L("tab.library"), systemImage: "books.vertical", value: 0) {
                LibraryView()
            }

            Tab(L("tab.quicknote"), systemImage: "square.and.pencil", value: 1) {
                QuickNoteView()
            }

            Tab(L("tab.search"), systemImage: "magnifyingglass", value: 2) {
                SearchView()
            }

            Tab(L("tab.settings"), systemImage: "gear", value: 3) {
                SettingsView()
            }
        }
        .onChange(of: navState.pendingBookNavigation) { _, book in
            if book != nil { selectedTab = 0 }
        }
        .onChange(of: navState.navigateToSettings) { _, go in
            if go { selectedTab = 3; navState.navigateToSettings = false }
        }
    }
}

#Preview {
    MainTabView()
        .environment(ConnectivityMonitor.shared)
}
