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 = "Respond only with a number that represents the answer to the question"
      session = LanguageModelSession(instructions: instructions)
   }
   func sendPrompt() async {
      do {
         let answer = try await session.respond(to: prompt, generating: Int.self)

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