//! Single-turn query agent: takes parsed build data + user question,
//! streams an LLM response with the build as context.

use futures_core::Stream;

use crate::llm::{ChatGptClient, LlmError, Message};

const SYSTEM_PROMPT: &str = "\
You are a Path of Exile 2 build analysis assistant. The user has uploaded \
their Path of Building export and you have access to their parsed build stats.\n\
\n\
Answer the user's questions about their build. Be specific and reference \
actual numbers from the build data when relevant. If the data doesn't contain \
enough information to answer, say so.\n\
\n\
Here is the parsed build data:\n";

/// Answers questions about a PoB build using an LLM with injected context.
pub struct QueryAgent {
    llm: ChatGptClient,
}

impl QueryAgent {
    pub fn new(llm: ChatGptClient) -> Self {
        Self { llm }
    }

    /// Stream a response to a user question about the given build.
    ///
    /// `build_json` is the JSON output from `PobParser::parse`.
    pub fn respond(
        &self,
        build_json: &[u8],
        message: &str,
    ) -> impl Stream<Item = Result<String, LlmError>> + Send {
        let system_content = format!(
            "{SYSTEM_PROMPT}```json\n{}\n```",
            String::from_utf8_lossy(build_json),
        );

        let messages = vec![
            Message {
                role: "system".to_owned(),
                content: system_content,
            },
            Message {
                role: "user".to_owned(),
                content: message.to_owned(),
            },
        ];

        self.llm.chat_stream(messages)
    }
}
