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 = Instructions {
         "You are a skilled writer of horror fiction"
         "You craft chilling stories filled with psychological fear"
         "You speak as if you were obsessed with the genre"
      }
      session = LanguageModelSession(instructions: instructions)
   }
   func sendPrompt() async {
      do {
         let answer = try await session.respond(to: prompt)

         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 = ""
   }
}