// 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.

//! Model management example
//!
//! This example demonstrates how to use the ollama-api-rs to manage models,
//! including listing, showing information, and pulling models.

use oai_sdk::{ModelClient, ShowModelRequest};

#[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 Model Management Example");
    println!("This example shows how to manage models with the Ollama API.\n");

    // Get version information
    match client.get_version().await {
        Ok(version) => println!("Ollama version: {}", version.version),
        Err(e) => println!("Could not get version: {}", e),
    }

    // List all models
    println!("\nListing available models:");
    match client.list_models().await {
        Ok(models) => {
            if models.is_empty() {
                println!("No models found.");
            } else {
                for model in models {
                    println!("  - {} ({})", model.name, model.details.parameter_size);
                }
            }
        }
        Err(e) => println!("Error listing models: {}", e),
    }

    // Show detailed information about a model
    println!("\nShowing information about llama3 model:");
    let request = ShowModelRequest {
        model: "llama3.1:8b".to_string(),
        verbose: Some(true),
    };

    match client.show_model(request).await {
        Ok(info) => {
            println!(
                "Model file: {}",
                info.modelfile.lines().next().unwrap_or("")
            );
            println!(
                "Parameters: {}",
                info.parameters.lines().next().unwrap_or("")
            );
            println!("Template: {}", info.template.lines().next().unwrap_or(""));
            println!("Family: {}", info.details.family);
            println!("Parameter size: {}", info.details.parameter_size);
        }
        Err(e) => println!("Error showing model info: {}", e),
    }

    #[cfg(feature = "local")]
    {
        // List running models (local only)
        println!("\nListing running models:");
        match client.list_running_models().await {
            Ok(models) => {
                if models.is_empty() {
                    println!("No models currently running.");
                } else {
                    for model in models {
                        println!("  - {} ({})", model.name, model.details.parameter_size);
                    }
                }
            }
            Err(e) => println!("Error listing running models: {}", e),
        }
    }

    println!("\nThis example demonstrates basic model management operations.");
    println!("For pulling models, you would use:");
    println!(
        "  let request = PullModelRequest {{ model: \"llama3:8b\".to_string(), insecure: None, stream: None }};"
    );
    println!("  let mut stream = client.pull_model(request).await?;");

    Ok(())
}
