import SwiftUI
import CoreSpotlight

@main
struct HeygoApp: App {
    let persistenceController = PersistenceController.shared
    @StateObject private var linkViewModel = LinkViewModel()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
                .environmentObject(linkViewModel)
                .onContinueUserActivity(CSSearchableItemActionType) { userActivity in
                    handleSpotlightSearch(userActivity: userActivity)
                }
        }
    }

    private func handleSpotlightSearch(userActivity: NSUserActivity) {
        guard let alias = SpotlightSearchManager.handleSpotlightSearchSelection(userActivity: userActivity) else {
            return
        }

        // Open the link from Spotlight search
        SpotlightSearchManager.openLinkFromSpotlight(alias: alias, linkViewModel: linkViewModel)
    }
}
