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

//! Blob management example (requires `local` feature)
//!
//! This example demonstrates how to check for and push blob data
//! to the Ollama server for model management.
//!
//! Run with: cargo run --example blob_management --features local

#[cfg(not(feature = "local"))]
fn main() {
    println!("This example requires the 'local' feature.");
    println!("Run with: cargo run --example blob_management --features local");
}

#[cfg(feature = "local")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    use oai_sdk::ModelClient;

    // Create the client
    let client = ModelClient::builder()
        .base_url("http://localhost:11434".to_string())
        .build()?;

    println!("Ollama Client Blob Management Example");
    println!("This example shows how to check for and push blob data.\n");

    // Example digest (this is a placeholder - in real usage, this would be a real SHA256 digest)
    let digest = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

    // Check if blob exists
    println!("Checking if blob '{}' exists...", digest);
    match client.blob_exists(digest).await {
        Ok(exists) => {
            if exists {
                println!("Blob exists on the server.");
            } else {
                println!("Blob does not exist on the server.");
            }
        }
        Err(e) => println!("Error checking blob existence: {}", e),
    }

    // Example of pushing blob content
    // In real usage, this would be actual model blob data
    let blob_content = b"example model blob data";
    let new_digest = "sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";

    println!("\nPushing blob content for digest '{}'...", new_digest);
    match client.push_blob(new_digest, blob_content).await {
        Ok(()) => println!("Blob pushed successfully!"),
        Err(e) => println!("Error pushing blob: {}", e),
    }

    // Verify the blob now exists
    println!("\nVerifying blob exists after push...");
    match client.blob_exists(new_digest).await {
        Ok(exists) => {
            if exists {
                println!("Blob verified to exist on the server.");
            } else {
                println!("Blob still does not exist (this is expected for this example).");
            }
        }
        Err(e) => println!("Error checking blob existence: {}", e),
    }

    println!("\nBlob management example complete!");
    println!(
        "Note: In real usage, you would work with actual model blob data and valid SHA256 digests."
    );

    Ok(())
}
