use serde::{Deserialize, Serialize};

pub fn continue_content(
    context: &str,
    instruction: &str,
    max_tokens: usize,
) -> Result<String, Box<dyn std::error::Error>> {
    let client = reqwest::blocking::Client::new();

    let messages = vec![
        Message {
            role: "system".to_string(),
            content: "
                Please generate content that is a direct continuation of the given text.
                Your response should be a logical next step in the content and should not repeat any of the text from the instruction or the content.
                Do not generate any text that is not a direct continuation of the content.
                if extra instructions are provided, follow them exactly, otherwise continue the text in a logical way.
            ".to_string(),
        },
        Message {
            role: "user".to_string(),
            content: context.to_string(),
        },
        Message {
            role: "user".to_string(),
            content: format!("Instructions: {instruction}"),
        },
    ];

    let request = ChatRequest {
        messages,
        temperature: 0.7,
    };

    let response = client
        .post("http://localhost:1234/v1/chat/completions")
        .json(&request)
        .send()?;

    if !response.status().is_success() {
        return Err(format!("Request failed: {}", response.text()?).into());
    }

    let response: ChatResponse = response.json()?;

    if let Some(choice) = response.choices.into_iter().next() {
        Ok(choice.message.content)
    } else {
        Err("No response from model".into())
    }
}

pub fn ai_enabled() -> bool {
    let client = reqwest::blocking::Client::new();
    client.get("http://localhost:1234/v1/models").send().is_ok()
}

// Simple request structure
#[derive(Serialize)]
struct ChatRequest {
    messages: Vec<Message>,
    temperature: f32,
}

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

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

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