import {log, logError} from "./util.js"

export interface GrokMessage {
  role: "system" | "user" | "assistant"
  content: string
}

export class GrokApiClient {
  private readonly apiKey: string | null
  private readonly baseUrl: string
  private readonly model: string
  private readonly initialContext: readonly GrokMessage[]

  constructor(apiKey: string | null, baseUrl: string, model: string, initialContext: readonly GrokMessage[]) {
    this.apiKey = apiKey
    this.baseUrl = baseUrl.replace(/\/$/, "")
    this.model = model
    this.initialContext = initialContext
  }

  async chatRaw(messages: GrokMessage[]): Promise<string> {
    const headers: Record<string, string> = {"Content-Type": "application/json"}
    if (this.apiKey) headers["Authorization"] = `Bearer ${this.apiKey}`
    const response = await fetch(`${this.baseUrl}/chat/completions`, {
      method: "POST",
      headers,
      body: JSON.stringify({
        model: this.model,
        messages,
        temperature: 0.3,
        max_tokens: 1024,
      }),
      signal: AbortSignal.timeout(60_000),
    })

    if (!response.ok) {
      const body = await response.text()
      logError(`Grok API HTTP ${response.status}`, body)
      throw new Error(`Grok API error: HTTP ${response.status}`)
    }

    const data = await response.json() as {choices: {message: {content: string}}[]}
    const content = data.choices?.[0]?.message?.content
    if (!content) throw new Error("Grok API returned empty response")

    log(`Grok API response: ${content.length} chars`)
    return content
  }

  async chat(history: GrokMessage[], userMessage: string): Promise<string> {
    log(`Grok API call: ${this.initialContext.length} context msgs, ${history.length} history msgs, user msg ${userMessage.length} chars`)
    return this.chatRaw([
      ...this.initialContext,
      ...history,
      {role: "user", content: userMessage},
    ])
  }
}
