use anyhow::Result;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tracing::{debug, error, warn};

use crate::core::config;

#[derive(Debug, Serialize)]
struct ChatRequest {
    model: String,
    messages: Vec<ChatMessage>,
    temperature: f32,
    max_tokens: u32,
    stream: bool,
}

#[derive(Debug, Serialize, Deserialize)]
struct ChatMessage {
    role: String,
    content: String,
}

#[derive(Debug, Deserialize)]
struct ChatResponse {
    choices: Vec<Choice>,
}

#[derive(Debug, Deserialize)]
struct Choice {
    message: ChatMessage,
}

/// Generates a 5W1H+ summary for a given scene context.
/// Context should include the combined text of all sentences in the scene.
pub async fn generate_5w1h_summary(scene_text: &str) -> Result<String> {
    if !*config::llm::SUMMARY_ENABLED {
        warn!("LLM Summary is disabled via config");
        return Ok("LLM Disabled".to_string());
    }

    let client = Client::builder()
        .timeout(Duration::from_secs(*config::llm::SUMMARY_TIMEOUT_SECS))
        .build()?;

    let prompt = format!(
        r#"Analyze the following video scene transcript and provide a concise 5W1H+ summary in JSON format.
        Focus on: Who, What, Where, When, Why, How, and Key Objects/Actions.
        
        Transcript:
        "{}"

        Output format:
        {{
          "who": "...",
          "what": "...",
          "where": "...",
          "when": "...",
          "why": "...",
          "how": "...",
          "summary": "..."
        }}"#,
        scene_text
    );

    let req = ChatRequest {
        model: (*config::llm::SUMMARY_MODEL).clone(),
        messages: vec![
            ChatMessage {
                role: "system".to_string(),
                content: "You are an expert video analyst assistant.".to_string(),
            },
            ChatMessage {
                role: "user".to_string(),
                content: prompt,
            },
        ],
        temperature: 0.1,
        max_tokens: 512,
        stream: false,
    };

    debug!("Calling LLM for summary: {}", *config::llm::SUMMARY_URL);

    let res = client
        .post(&*config::llm::SUMMARY_URL)
        .json(&req)
        .send()
        .await?;

    if !res.status().is_success() {
        error!("LLM API error: {}", res.status());
        let text = res.text().await.unwrap_or_default();
        anyhow::bail!("LLM API error: {}", text);
    }

    let chat_res: ChatResponse = res.json().await?;

    if let Some(choice) = chat_res.choices.into_iter().next() {
        Ok(choice.message.content.trim().to_string())
    } else {
        anyhow::bail!("Empty response from LLM");
    }
}
