// src/llm.rs
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
struct LlmRequest {
    model: String,
    messages: Vec<Message>,
}

#[derive(Serialize, Deserialize)]
struct Message {
    role: String, // "user" or "assistant"
    content: String,
}

pub async fn query_llm(text: &str) -> Result<String, String> {
    let client = reqwest::Client::new();

    // Build the request body
    let payload = LlmRequest {
        model: "gemma2-9b-it".into(), // whatever model you run locally
        messages: vec![Message {
            role: "user".into(),
            content: text.into(),
        }],
    };

    // POST to lm‑studio (default 127.0.0.1:1234)
    let resp = client
        .post("http://127.0.0.1:1234/v1/chat/completions")
        .json(&payload)
        .send()
        .await
        .unwrap();

    // The API returns a JSON with `choices[].message.content`
    #[derive(Deserialize)]
    struct LlmResponse {
        choices: Vec<Choice>,
    }
    #[derive(Deserialize)]
    struct Choice {
        message: Message,
    }

    let llm_resp: LlmResponse = resp.json().await.unwrap();
    Ok(llm_resp.choices[0].message.content.clone())
}
