#!/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 2: Pod Management Operations
// This test covers pod creation, listing, retrieval, and deletion

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

print("=== Kubernetes Pod Management Test ===");
print("");

// Setup test namespace
let test_namespace = "sal-test-pods-" + timestamp();
print("Setting up test namespace: " + test_namespace);

try {
    let setup_km = kubernetes("default");
    setup_km.create_namespace(test_namespace);
    print("✅ Test namespace created");
} catch (error) {
    print("❌ Failed to create test namespace: " + error);
    throw error;
}

// Create manager for test namespace
let km = kubernetes(test_namespace);

print("");

// Test pod listing (should be empty initially)
print("Test 1: Initial Pod Listing");
print("---------------------------");

try {
    let initial_pods = km.pods_list();
    print("Initial pod count: " + initial_pods.len());
    
    if initial_pods.len() == 0 {
        print("✅ Namespace is empty as expected");
    } else {
        print("⚠️  Found " + initial_pods.len() + " existing pods in test namespace");
    }
    
} catch (error) {
    print("❌ Initial pod listing failed: " + error);
    throw error;
}

print("");

// Test pod creation
print("Test 2: Pod Creation");
print("-------------------");

let test_pod_name = "test-pod-" + timestamp();
let test_image = "nginx:alpine";
let test_labels = #{
    "app": "test",
    "environment": "testing",
    "created-by": "sal-integration-test"
};

try {
    print("Creating pod: " + test_pod_name);
    print("Image: " + test_image);
    print("Labels: " + test_labels);
    
    let created_pod = km.create_pod(test_pod_name, test_image, test_labels);
    print("✅ Pod created successfully");
    
    // Verify pod name
    if created_pod.name == test_pod_name {
        print("✅ Pod name matches expected: " + created_pod.name);
    } else {
        print("❌ Pod name mismatch. Expected: " + test_pod_name + ", Got: " + created_pod.name);
        throw "Pod name verification failed";
    }
    
} catch (error) {
    print("❌ Pod creation failed: " + error);
    throw error;
}

print("");

// Test pod listing after creation
print("Test 3: Pod Listing After Creation");
print("----------------------------------");

try {
    let pods_after_creation = km.pods_list();
    print("Pod count after creation: " + pods_after_creation.len());
    
    if pods_after_creation.len() > 0 {
        print("✅ Pods found after creation");
        
        // Find our test pod
        let found_test_pod = false;
        for pod in pods_after_creation {
            if pod.name == test_pod_name {
                found_test_pod = true;
                print("✅ Test pod found in list: " + pod.name);
                print("  Status: " + pod.status);
                break;
            }
        }
        
        if !found_test_pod {
            print("❌ Test pod not found in pod list");
            throw "Test pod not found in listing";
        }
        
    } else {
        print("❌ No pods found after creation");
        throw "Pod listing verification failed";
    }
    
} catch (error) {
    print("❌ Pod listing after creation failed: " + error);
    throw error;
}

print("");

// Test pod retrieval
print("Test 4: Individual Pod Retrieval");
print("--------------------------------");

try {
    let retrieved_pod = km.get_pod(test_pod_name);
    print("✅ Pod retrieved successfully");
    print("Pod name: " + retrieved_pod.name);
    print("Pod status: " + retrieved_pod.status);
    
    if retrieved_pod.name == test_pod_name {
        print("✅ Retrieved pod name matches expected");
    } else {
        print("❌ Retrieved pod name mismatch");
        throw "Pod retrieval verification failed";
    }
    
} catch (error) {
    print("❌ Pod retrieval failed: " + error);
    throw error;
}

print("");

// Test resource counts
print("Test 5: Resource Counts");
print("-----------------------");

try {
    let counts = km.resource_counts();
    print("Resource counts: " + counts);
    
    if counts.pods >= 1 {
        print("✅ Pod count reflects created pod: " + counts.pods);
    } else {
        print("⚠️  Pod count doesn't reflect created pod: " + counts.pods);
    }
    
} catch (error) {
    print("❌ Resource counts failed: " + error);
    throw error;
}

print("");

// Test pod deletion
print("Test 6: Pod Deletion");
print("--------------------");

try {
    print("Deleting pod: " + test_pod_name);
    km.delete_pod(test_pod_name);
    print("✅ Pod deletion initiated");
    
    // Wait a moment for deletion to propagate
    print("Waiting for deletion to propagate...");
    
    // Check if pod is gone (may take time)
    try {
        let deleted_pod = km.get_pod(test_pod_name);
        print("⚠️  Pod still exists after deletion (may be terminating): " + deleted_pod.status);
    } catch (get_error) {
        print("✅ Pod no longer retrievable (deletion successful)");
    }
    
} catch (error) {
    print("❌ Pod deletion failed: " + error);
    throw error;
}

print("");

// Cleanup
print("Test 7: Cleanup");
print("---------------");

try {
    let cleanup_km = kubernetes("default");
    cleanup_km.delete_namespace(test_namespace);
    print("✅ Test namespace cleanup initiated");
    
} catch (error) {
    print("❌ Cleanup failed: " + error);
    // Don't throw here as this is cleanup
}

print("");
print("=== Pod Management Test Complete ===");
print("✅ All pod management tests passed");
