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

 .template(|pod| {
      pod
        .label("app", "default_app")
        .image("nginx:latest")
        .port(80)
        .cpu("500", "1000")
        .memory("256", "512")
        .readiness_http("/health", 80)
  })



 // Example 10: Multi-namespace deployment
 let deployment_envs = ["dev", "staging", "prod"];

 for env in deployment_envs {
     let env_ns = k.namespace("app-" + env); // create/get namespace

     env_ns.deployment("app-" + env)
         .image("nginx:latest")
         .replicas(1)
         .apply();

     env_ns.service("svc-" + env)
         .target_deployment("app-" + env)
         .port(80)
         .apply();
 }

 // Example 11: Full application deployment workflow
 let app_ns = k.namespace("myapp");

 // Deploy web tier
 app_ns.deployment("web")
     .image("nginx:latest")
     .replicas(3)
     .apply();

 app_ns.service("web-svc")
     .target_deployment("web")
     .port(80)
     .apply();

 // Deploy API tier
 app_ns.deployment("api")
     .image("node:18-alpine")
     .replicas(3)
     .apply();

 app_ns.service("api-svc")
     .target_deployment("api")
     .port(3000)
     .apply();

 // Deploy database tier
 app_ns.deployment("database")
     .image("postgres:15-alpine")
     .replicas(1)
     .apply();

 app_ns.service("db-svc")
     .target_deployment("database")
     .port(5432)
     .apply();

 // Create configuration for application connectivity
 app_ns.configmap("app-config")
     .set("database-host", "db-svc")
     .set("database-port", "5432")
     .set("api-port", "3000")
     .apply();

 // Create secrets for application
 app_ns.secret("app-secrets")
     .set("database-password", "secure_pass")
     .set("jwt-secret", "jwt_secret_key")
     .apply();

 // Example 12: List all resources in application namespace
 print(app_ns.pods_list());
 print(app_ns.deployments_list());
 print(app_ns.services_list());

 // Example 13: Microservices deployment with environment injection
 let microservices = [
     #{ name: "user-service", image: "node:18-alpine", port: 8001, replicas: 2 },
     #{ name: "order-service", image: "node:18-alpine", port: 8002, replicas: 2 },
     #{ name: "payment-service", image: "python:3.11-slim", port: 8003, replicas: 1 },
     #{ name: "notification-service", image: "node:18-alpine", port: 8004, replicas: 2 }
 ];

 for ms in microservices {
     let svc_ns = k.namespace("svc-" + ms.name);

     // Deploy service
     svc_ns.deployment(ms.name)
         .image(ms.image)
         .replicas(ms.replicas)
         .apply();

     // Create service endpoint
     svc_ns.service(ms.name + "-svc")
         .target_deployment(ms.name)
         .port(ms.port)
         .apply();
 }

 // Example 14: Advanced deployment with environment variables from multiple sources
 let advanced_ns = k.namespace("advanced-app");

 advanced_ns.configmap("database-config")
     .set("host", "postgres.default.svc.cluster.local")
     .set("port", "5432")
     .set("pool-size", "20")
     .apply();

 advanced_ns.secret("database-credentials")
     .set("username", "dbuser")
     .set("password", "dbpass123")
     .apply();

 advanced_ns.pod("advanced-pod")
     .image("python:3.11-slim")
     .env(#{ "LOG_LEVEL": "debug", "APP_ENV": "production" })
     .env_from_configmap("database-config")
     .env_from_secret("database-credentials")
     .apply();

 // Example 15: Display Kubernetes state
 print(k.md()); // show complete markdown representation of all namespaces
