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

//! Legacy embeddings example
//!
//! This example demonstrates how to use the legacy embeddings API
//! which uses the /api/embeddings endpoint.
//!
//! Note: For modern batch embeddings, use the `embed()` method with
//! `EmbedInput::Multiple()` instead.

use oai_sdk::{EmbeddingsRequest, ModelClient};

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

    println!("Ollama Client Legacy Embeddings Example");
    println!("This example shows the legacy /api/embeddings endpoint.\n");

    let model = "llama3:8b"; // Make sure this model supports embeddings

    // Generate embedding for a single text using legacy API
    let request = EmbeddingsRequest {
        model: model.to_string(),
        prompt: "The cat sat on the mat".to_string(),
        truncate: Some(true),
        options: None,
        keep_alive: None,
    };

    println!("Generating embedding via legacy API...");
    match client.embeddings(request).await {
        Ok(response) => {
            println!("Embedding generated successfully!");
            println!("Embedding dimension: {}", response.embedding.len());
            println!(
                "First 5 values: {:?}",
                &response.embedding[..5.min(response.embedding.len())]
            );
        }
        Err(e) => println!("Error generating embedding: {}", e),
    }

    println!(
        "\nNote: For batch embeddings, use the modern `embed()` API with EmbedInput::Multiple."
    );

    Ok(())
}
