#!/usr/bin/env herodo
// Pre-flight check: Ensure Kubernetes cluster is accessible
try {
    if !run("kubectl cluster-info").execute().success {
        print("SKIPPED: Kubernetes cluster is not accessible");
        exit(0);
    }
} catch(e) {
    print("SKIPPED: Kubernetes cluster is not accessible: " + e);
    exit(0);
}

//! Kubernetes namespace management example
//!
//! This script demonstrates namespace creation and management operations.
//! 
//! Prerequisites:
//! - A running Kubernetes cluster
//! - Valid kubeconfig file or in-cluster configuration
//! - Permissions to create and manage namespaces
//!
//! Usage:
//!   herodo examples/kubernetes/namespace_management.rhai

print("=== SAL Kubernetes Namespace Management Example ===");

// Create a KubernetesManager
let km = kubernetes("default");
print("Created KubernetesManager for namespace: " + namespace(km));

// Define test namespace names
let test_namespaces = [
    "sal-test-namespace-1",
    "sal-test-namespace-2", 
    "sal-example-app"
];

print("\n--- Creating Test Namespaces ---");
for ns in test_namespaces {
    print("Creating namespace: " + ns);
    try {
        namespace_create(km, ns);
        print("✓ Successfully created namespace: " + ns);
    } catch(e) {
        print("✗ Failed to create namespace " + ns + ": " + e);
    }
}

// Wait a moment for namespaces to be created
print("\nWaiting for namespaces to be ready...");

// Verify namespaces were created
print("\n--- Verifying Namespace Creation ---");
for ns in test_namespaces {
    let exists = namespace_exists(km, ns);
    if exists {
        print("✓ Namespace '" + ns + "' exists");
    } else {
        print("✗ Namespace '" + ns + "' was not found");
    }
}

// List all namespaces to see our new ones
print("\n--- Current Namespaces ---");
let all_namespaces = namespaces_list(km);
print("Total namespaces in cluster: " + all_namespaces.len());
for ns in all_namespaces {
    if ns.starts_with("sal-") {
        print("  🔹 " + ns + " (created by this example)");
    } else {
        print("  - " + ns);
    }
}

// Test idempotent creation (creating the same namespace again)
print("\n--- Testing Idempotent Creation ---");
let test_ns = test_namespaces[0];
print("Attempting to create existing namespace: " + test_ns);
try {
    namespace_create(km, test_ns);
    print("✓ Idempotent creation successful (no error for existing namespace)");
} catch(e) {
    print("✗ Unexpected error during idempotent creation: " + e);
}

// Create managers for the new namespaces and check their properties
print("\n--- Creating Managers for New Namespaces ---");
for ns in test_namespaces {
    try {
        let ns_km = kubernetes(ns);
        print("✓ Created manager for namespace: " + namespace(ns_km));
        
        // Get resource counts for the new namespace (should be mostly empty)
        let counts = resource_counts(ns_km);
        print("  Resource counts: " + counts);
    } catch(e) {
        print("✗ Failed to create manager for " + ns + ": " + e);
    }
}

print("\n--- Cleanup Instructions ---");
print("To clean up the test namespaces created by this example, run:");
for ns in test_namespaces {
    print("  kubectl delete namespace " + ns);
}

print("\n=== Namespace management example completed! ===");
