#!/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);
}

// Test 1: Namespace Operations
// This test covers namespace creation, existence checking, and listing

// Helper function to generate timestamp for unique names
fn timestamp() {
    let now = 1640995200; // Base timestamp
    let random = (now % 1000000).to_string();
    random
}

print("=== Kubernetes Namespace Operations Test ===");
print("");

// Test namespace creation and existence checking
print("Test 1: Namespace Creation and Existence");
print("----------------------------------------");

// Create a test namespace
let test_namespace = "sal-test-ns-" + timestamp();
print("Creating test namespace: " + test_namespace);

try {
    let km = kubernetes("default");
    
    // Check if namespace exists before creation
    let exists_before = km.namespace_exists(test_namespace);
    print("Namespace exists before creation: " + exists_before);
    
    if exists_before {
        print("⚠️  Namespace already exists, this is unexpected");
    } else {
        print("✅ Namespace doesn't exist yet (expected)");
    }
    
    // Create the namespace
    print("Creating namespace...");
    km.create_namespace(test_namespace);
    print("✅ Namespace created successfully");
    
    // Check if namespace exists after creation
    let exists_after = km.namespace_exists(test_namespace);
    print("Namespace exists after creation: " + exists_after);
    
    if exists_after {
        print("✅ Namespace exists after creation (expected)");
    } else {
        print("❌ Namespace doesn't exist after creation (unexpected)");
        throw "Namespace creation verification failed";
    }
    
    // Test idempotent creation (should not error)
    print("Testing idempotent creation...");
    km.create_namespace(test_namespace);
    print("✅ Idempotent creation successful");
    
} catch (error) {
    print("❌ Namespace creation test failed: " + error);
    throw error;
}

print("");

// Test namespace listing
print("Test 2: Namespace Listing");
print("-------------------------");

try {
    let km = kubernetes("default");
    
    // List all namespaces
    let namespaces = km.namespaces_list();
    print("Found " + namespaces.len() + " namespaces");
    
    if namespaces.len() == 0 {
        print("⚠️  No namespaces found, this might indicate a connection issue");
    } else {
        print("✅ Successfully retrieved namespace list");
        
        // Check if our test namespace is in the list
        let found_test_ns = false;
        for ns in namespaces {
            if ns.name == test_namespace {
                found_test_ns = true;
                break;
            }
        }
        
        if found_test_ns {
            print("✅ Test namespace found in namespace list");
        } else {
            print("⚠️  Test namespace not found in list (might be propagation delay)");
        }
    }
    
} catch (error) {
    print("❌ Namespace listing test failed: " + error);
    throw error;
}

print("");

// Test namespace manager creation
print("Test 3: Namespace Manager Creation");
print("----------------------------------");

try {
    // Create manager for our test namespace
    let test_km = kubernetes(test_namespace);
    
    // Verify the manager's namespace
    let manager_namespace = namespace(test_km);
    print("Manager namespace: " + manager_namespace);
    
    if manager_namespace == test_namespace {
        print("✅ Manager created for correct namespace");
    } else {
        print("❌ Manager namespace mismatch");
        throw "Manager namespace verification failed";
    }
    
} catch (error) {
    print("❌ Namespace manager creation test failed: " + error);
    throw error;
}

print("");

// Cleanup
print("Test 4: Namespace Cleanup");
print("-------------------------");

try {
    let km = kubernetes("default");
    
    // Delete the test namespace
    print("Deleting test namespace: " + test_namespace);
    km.delete_namespace(test_namespace);
    print("✅ Namespace deletion initiated");
    
    // Note: Namespace deletion is asynchronous, so we don't immediately check existence
    print("ℹ️  Namespace deletion is asynchronous and may take time to complete");
    
} catch (error) {
    print("❌ Namespace cleanup failed: " + error);
    // Don't throw here as this is cleanup
}

print("");
print("=== Namespace Operations Test Complete ===");
print("✅ All namespace operation tests passed");
