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

//! OpenAI compatibility example
//!
//! This example demonstrates how to use the OpenAI-compatible endpoints
//! that work with standard OpenAI client libraries.

use oai_sdk::{
    ChatCompletionsRequest, ChatMessage, ModelClient, OpenAIEmbeddingsInput,
    OpenAIEmbeddingsRequest, ResponsesRequest,
};

#[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 OpenAI Compatibility Example");
    println!("This example shows OpenAI-compatible endpoints.\n");

    // Chat completions
    println!("1. Chat Completions:");
    let chat_request = ChatCompletionsRequest {
        model: "llama3.1:8b".to_string(),
        messages: vec![ChatMessage::user("Why is the sky blue?")],
        stream: Some(false),
        ..Default::default()
    };

    match client.chat_completions(chat_request).await {
        Ok(response) => {
            println!("   Response: {}", response.choices[0].message.content);
        }
        Err(e) => println!("   Error: {}", e),
    }

    // Embeddings
    println!("\n2. Embeddings:");
    let embeddings_request = OpenAIEmbeddingsRequest {
        model: "llama3.1:8b".to_string(),
        input: OpenAIEmbeddingsInput::Single("Why is the sky blue?".to_string()),
        encoding_format: Some("float".to_string()),
        ..Default::default()
    };

    match client.openai_embeddings(embeddings_request).await {
        Ok(response) => {
            println!("   Generated {} embeddings", response.data.len());
            println!("   First 3 values: {:?}", &response.data[0].embedding[..3]);
        }
        Err(e) => println!("   Error: {}", e),
    }

    // Responses
    println!("\n3. Responses:");
    let responses_request = ResponsesRequest {
        model: "llama3.1:8b".to_string(),
        input: Some("Why is the sky blue?".to_string()),
        stream: Some(false),
        ..Default::default()
    };

    match client.responses(responses_request).await {
        Ok(response) => {
            println!("   Response: {}", response.output);
        }
        Err(e) => println!("   Error: {}", e),
    }

    println!("\nOpenAI compatibility example complete!");
    println!("You can also use this with standard OpenAI client libraries:");
    println!("  - Set base_url to 'http://localhost:11434/v1/'");
    println!("  - Use any API key (it will be ignored)");

    Ok(())
}
