#!/usr/bin/env herodo
//! Nerdctl Basic Container Operations
//! Demonstrates fundamental container lifecycle operations with nerdctl/containerd

// Skip if required tools are not installed
if which("nerdctl") == "" || which("runc") == "" || which("buildah") == "" {
    print("SKIPPED: nerdctl/runc/buildah are required");
    exit(0);
}

// Example 1: Creating a simple container with builder pattern
let simple_container = nerdctl_container_from_image("webserver", "nginx:latest")
    .with_port("8080:80")
    .with_port("8443:443")
    .with_detach(true)
    .build();

simple_container.remove();

// Example 2: Container with environment variables
let env_vars = #{
    "APP_ENV": "production",
    "APP_DEBUG": "false",
    "DATABASE_HOST": "localhost",
    "DATABASE_PORT": "5432"
};

let app_container = nerdctl_container_from_image("app", "node:18-alpine")
    .with_envs(env_vars)
    .with_port("3000:3000")
    .with_detach(true)
    .build();

app_container.remove();

// Example 3: Container with volume mounts
let db_container = nerdctl_container_from_image("database", "postgres:15-alpine")
    .with_volume("/var/lib/postgresql/data", "/data/postgres")
    .with_volume("/etc/postgresql", "/config/postgres")
    .with_env("POSTGRES_PASSWORD", "secret123")
    .with_detach(true)
    .build();

db_container.remove();

// Example 4: Container with health check
let healthy_container = nerdctl_container_from_image("healthy-app", "alpine:latest")
    .with_health_check("ping -c 1 localhost || exit 1")
    .with_detach(true)
    .build();

healthy_container.remove();

// Example 5: Container with resource limits
let limited_container = nerdctl_container_from_image("limited-app", "ubuntu:22.04")
    .with_memory_limit("512m")
    .with_memory_swap_limit("1g")
    .with_cpu_limit("1.0")
    .with_cpu_shares("1024")
    .with_detach(true)
    .build();

limited_container.remove();

// Example 6: Container with network configuration
let network_container = nerdctl_container_from_image("net-app", "nginx:latest")
    .with_network("bridge")
    .with_network_alias("web")
    .with_network_aliases(["api", "webserver"])
    .with_port("80:80")
    .with_detach(true)
    .build();

network_container.remove();

// Example 7: Container with restart policy
let restart_container = nerdctl_container_from_image("persistent", "alpine:latest")
    .with_restart_policy("unless-stopped")
    .with_detach(true)
    .build();

restart_container.remove();

// Example 8: Container with snapshotter
let snap_container = nerdctl_container_from_image("snapped", "alpine:latest")
    .with_snapshotter("overlayfs")
    .with_detach(true)
    .build();

snap_container.remove();

// Example 9: Simple run operations (legacy API)
nerdctl_run("hello-world");
nerdctl_run_with_name("alpine:latest", "test-container");
nerdctl_run_with_port("nginx:latest", "web", "9090:80");

// Example 10: Container lifecycle operations
let lifecycle = nerdctl_container_from_image("lifecycle-test", "alpine:latest")
    .with_detach(true)
    .build();

lifecycle.start();

let exec_result = lifecycle.exec("echo 'Hello from container'");

let logs = lifecycle.logs();

lifecycle.stop();

lifecycle.remove();

// Example 11: Multiple containers with different configurations
let db = nerdctl_container_from_image("app-db", "postgres:15-alpine")
    .with_env("POSTGRES_PASSWORD", "apppass")
    .with_port("5432:5432")
    .with_detach(true)
    .build();

let cache = nerdctl_container_from_image("app-cache", "redis:7-alpine")
    .with_port("6379:6379")
    .with_detach(true)
    .build();

let web = nerdctl_container_from_image("app-web", "nginx:alpine")
    .with_port("80:80")
    .with_env("BACKEND", "localhost:8000")
    .with_detach(true)
    .build();

db.remove();
cache.remove();
web.remove();

// Example 12: Container with all features
let full_featured = nerdctl_container_from_image("full-app", "ubuntu:22.04")
    .with_ports(["8080:8080", "8081:8081"])
    .with_envs(#{
        "ENV": "prod",
        "DEBUG": "false",
        "LOG_LEVEL": "info"
    })
    .with_volumes(["/app:/app", "/logs:/var/log"])
    .with_memory_limit("1g")
    .with_cpu_limit("2.0")
    .with_health_check("curl -f http://localhost:8080 || exit 1")
    .with_restart_policy("always")
    .with_detach(true)
    .build();

full_featured.remove();
