import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @EnvironmentObject var linkViewModel: LinkViewModel

    var body: some View {
        TabView {
            LinkListView()
                .tabItem {
                    Image(systemName: "link")
                    Text("links")
                }

            SearchView()
                .tabItem {
                    Image(systemName: "magnifyingglass")
                    Text("search")
                }

            AnalyticsView()
                .tabItem {
                    Image(systemName: "chart.bar")
                    Text("analytics")
                }

            SettingsView()
                .tabItem {
                    Image(systemName: "gear")
                    Text("settings")
                }

            #if DEBUG
            SpotlightDebugView()
                .tabItem {
                    Image(systemName: "eye")
                    Text("spotlight")
                }
            #endif
        }
        .environmentObject(linkViewModel)
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("ShowLinkContent"))) { notification in
            // Handle showing link content when opened from Spotlight
            if let link = notification.userInfo?["link"] as? Link {
                handleShowLinkContent(link: link)
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("ShowTemplateParameterInput"))) { notification in
            // Handle showing template parameter input when opened from Spotlight
            if let link = notification.userInfo?["link"] as? Link {
                handleShowTemplateParameterInput(link: link)
            }
        }
    }

    private func handleShowLinkContent(link: Link) {
        // For custom/template links, you might want to show a modal or navigate to a detail view
        // For now, this is a placeholder - you can implement the UI as needed
        print("Showing content for link: \(link.alias)")
    }

    private func handleShowTemplateParameterInput(link: Link) {
        // For template links with parameters, show parameter input interface
        // For now, this is a placeholder - you can implement the UI as needed
        print("Showing template parameter input for link: \(link.alias)")
    }
}

#Preview {
    ContentView()
        .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
        .environmentObject(LinkViewModel())
}
