import SwiftUI
import FoundationModels
import Observation

@Observable class ApplicationData {
   var response: AttributedString = ""
   var prompt: String = ""

   @ObservationIgnored var session: LanguageModelSession

   static let shared: ApplicationData = ApplicationData()
   private init() {
      let instructions = "Determine the city the user is talking about, and return information about it"
      session = LanguageModelSession(instructions: instructions)
   }
   func sendPrompt() async {
      do {
         let answer = try await session.respond(to: prompt, generating: CityResponse.self)
         let city = answer.content
         var message = "City: \(city.city)\n"
         message += "Country: \(city.country)\n"
         message += "Language: \(city.language)\n"
         message += "Currency: \(city.currency)\n"
         message += "Attractions: \(city.attractions.joined(separator: ", "))\n"

         var newResponse = AttributedString("\(message)\n\n")
         newResponse.font = .system(size: 16, weight: .regular)
         response.append(newResponse)
      } catch {
         response.append(AttributedString("Error accessing the model: \(error)\n\n"))
      }
      prompt = ""
   }
}