import SwiftUI
import AppIntents

struct ShowMovie: AppIntent {
   static let title: LocalizedStringResource = "Find Movie"
   static let description = IntentDescription("Finds a movie.")

   static let supportedModes: IntentModes = .background

   @Parameter(title: "Title", requestValueDialog: "What's the movie you want to see?")
   var search: String

   @MainActor
   func perform() async throws -> some IntentResult & ProvidesDialog {
      let appData = ApplicationData.shared

      let movie = appData.listMovies.first(where: {
         $0.title.localizedCaseInsensitiveContains(search)
      })
      var movieTitle = "Undefined"
      if let movie = movie {
         movieTitle = movie.title
      }
      return .result(
         dialog: IntentDialog("The movie is \(movieTitle)")
      )
   }
}
struct TestIntentsShortcuts: AppShortcutsProvider {
   static var appShortcuts: [AppShortcut] {
      AppShortcut(
         intent: ShowMovie(),
         phrases: [
            "Show me a movie in \(.applicationName)",
            "Find a movie in \(.applicationName)"
         ],
         shortTitle: LocalizedStringResource("Get Movie"),
         systemImageName: "magnifyingglass.circle"
      )
   }
}