// Copyright 2026 Cloudflavor GmbH

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Basic chat example
//!
//! This example demonstrates how to use the ollama-api-rs to have a simple
//! conversation with a model.

use oai_sdk::{ChatRequest, Message, ModelClient};
use std::io::{self, Write};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create the client
    let client = ModelClient::builder()
        .base_url("http://localhost:11434")
        .build()?;

    println!("Ollama Client Chat Example");
    println!("Enter 'quit' to exit\n");

    // Check if we can connect
    match client.get_version().await {
        Ok(version) => println!("Connected to Ollama version: {}\n", version.version),
        Err(e) => {
            println!("Warning: Could not connect to Ollama: {}", e);
            println!("Make sure Ollama is running at http://localhost:11434\n");
        }
    }

    // Get available models
    match client.list_models().await {
        Ok(models) => {
            println!("Available models:");
            for model in models.iter().take(3) {
                println!("  - {} ({})", model.name, model.details.parameter_size);
            }
            if models.len() > 3 {
                println!("  ... and {} more", models.len() - 3);
            }
            println!();
        }
        Err(e) => {
            println!("Warning: Could not list models: {}\n", e);
        }
    }

    // Start the conversation
    let mut messages = Vec::new();

    loop {
        print!("You: ");
        io::stdout().flush()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input = input.trim();

        if input.to_lowercase() == "quit" {
            println!("Goodbye!");
            break;
        }

        if input.is_empty() {
            continue;
        }

        // Add user message
        messages.push(Message::user(input));

        // Create the chat request
        let request = ChatRequest {
            model: "llama3.1:8b".to_string(), // You can change this to any available model
            messages: messages.clone(),
            stream: false,
            format: None,
            options: None,
            keep_alive: None,
            tools: None,
            think: None,
        };

        // Get the response
        match client.chat(request).await {
            Ok(response) => {
                println!("Assistant: {}", response.message.content);
                println!();

                // Add assistant message to conversation history
                messages.push(Message::assistant(response.message.content));
            }
            Err(e) => {
                println!("Error: {}", e);
            }
        }
    }

    Ok(())
}
