//! Deployment with Docker registry secret example
//!
//! This example shows how to create a Docker registry secret and use it in a deployment.
//!
//! Run with: cargo run --example with_secrets

use lmrc_kubernetes::config::DeploymentOptions;
use lmrc_kubernetes::deployment::{ContainerSpec, DeploymentSpec};
use lmrc_kubernetes::{Client, ClientConfig};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing
    tracing_subscriber::fmt::init();

    // Create client
    let config = ClientConfig::infer().await?;
    let client = Client::new(config, "default").await?;

    // Get credentials from environment
    let registry = env::var("REGISTRY_URL").unwrap_or_else(|_| "docker.io".to_string());
    let username = env::var("REGISTRY_USERNAME").expect("REGISTRY_USERNAME not set");
    let password = env::var("REGISTRY_PASSWORD").expect("REGISTRY_PASSWORD not set");

    // Create Docker registry secret
    println!("Creating registry secret...");
    client
        .secrets()
        .apply_docker_registry_secret("my-registry-secret", &registry, &username, &password)
        .await?;

    // Create deployment that uses the secret
    let container = ContainerSpec::new("app", "private-registry/myapp:v1.0.0")
        .with_port(8080)
        .with_env("DATABASE_URL", "postgres://localhost/mydb");

    let deployment = DeploymentSpec::new("private-app")
        .with_replicas(3)
        .with_container(container);

    // Apply deployment
    let options = DeploymentOptions::new().wait(true);
    println!("Deploying application...");
    client.deployments().apply(&deployment, &options).await?;

    println!("Deployment successful!");

    Ok(())
}
