#!/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 Registry and MQTT
//! Demonstrates container registry and MQTT broker deployment
//! All resource names are automatically normalized using name_fix

// Example 1: Initialize Kubernetes and create services namespace
let k = kubernetes();
let ns = k.namespace("container-services");

// Example 2: Deploy container registry
ns.registry("my-registry")
    .host("registry.example.com")
    .port(5000)
    .apply();

// Example 3: Deploy secure registry with authentication
ns.registry("docker-registry")
    .host("docker.example.com")
    .port(5000)
    .auth("admin", "secret-password")
    .apply();

// Example 4: Deploy multiple registry mirrors
let registries = [
    #{ name: "docker-hub-mirror", host: "mirror.docker.io", port: 5000 },
    #{ name: "quay-mirror", host: "mirror.quay.io", port: 5000 },
    #{ name: "gcr-mirror", host: "mirror.gcr.io", port: 5000 }
];

for reg in registries {
    ns.registry(reg.name)
        .host(reg.host)
        .port(reg.port)
        .apply();
}

// Example 5: Deploy MQTT broker
ns.mqtt("mqtt-broker")
    .image("mosquitto:latest")
    .port(1883)
    .apply();

// Example 6: Deploy secure MQTT broker
ns.mqtt("mqtt-secure")
    .image("mosquitto:latest")
    .port(8883)
    .auth("user", "password")
    .apply();

// Example 7: Deploy multiple MQTT brokers with different ports
let mqtt_brokers = [
    #{ name: "mqtt-primary", port: 1883 },
    #{ name: "mqtt-secondary", port: 1884 },
    #{ name: "mqtt-tertiary", port: 1885 }
];

for mqtt_cfg in mqtt_brokers {
    ns.mqtt(mqtt_cfg.name)
        .image("mosquitto:latest")
        .port(mqtt_cfg.port)
        .apply();
}

// Example 8: Get registry and MQTT connection info
print(ns.service_info("my-registry"));
print(ns.service_info("mqtt-broker"));

// Example 9: Complete IoT application with registry and messaging
let iot_ns = k.namespace("iot-app");

// Deploy internal registry for IoT app
iot_ns.registry("app-registry")
    .host("registry.local")
    .port(5000)
    .apply();

// Deploy MQTT broker for IoT messaging
iot_ns.mqtt("app-mqtt")
    .image("mosquitto:latest")
    .port(1883)
    .apply();

// Deploy IoT application services
iot_ns.deployment("iot-app")
    .image("node:18-alpine")
    .replicas(3)
    .apply();

iot_ns.service("iot-app-svc")
    .target_deployment("iot-app")
    .port(8080)
    .apply();

// Create configuration for app connectivity
iot_ns.configmap("app-config")
    .set("registry-host", "app-registry:5000")
    .set("mqtt-host", "app-mqtt")
    .set("mqtt-port", "1883")
    .apply();

// Example 10: Microservices with integrated registry and messaging
let infra_ns = k.namespace("iot-infrastructure");

// Deploy primary registry
infra_ns.registry("primary-registry")
    .host("registry.internal")
    .port(5000)
    .apply();

// Deploy MQTT brokers for redundancy
infra_ns.mqtt("primary-mqtt")
    .image("mosquitto:latest")
    .port(1883)
    .apply();

infra_ns.mqtt("backup-mqtt")
    .image("mosquitto:latest")
    .port(1884)
    .apply();

// Deploy sensor and actuator microservices
let services_cfg = [
    #{ name: "sensor-service", replicas: 3 },
    #{ name: "actuator-service", replicas: 2 },
    #{ name: "gateway-service", replicas: 2 }
];

for svc in services_cfg {
    infra_ns.deployment(svc.name)
        .image("node:18-alpine")
        .replicas(svc.replicas)
        .apply();

    infra_ns.service(svc.name + "-svc")
        .target_deployment(svc.name)
        .port(8080)
        .apply();
}

// Example 11: Create service interconnection configuration
infra_ns.configmap("infrastructure-config")
    .set("registry-host", "primary-registry:5000")
    .set("mqtt-primary", "primary-mqtt:1883")
    .set("mqtt-backup", "backup-mqtt:1884")
    .set("gateway-host", "gateway-service-svc")
    .apply();

// Example 12: Create secrets for registry and MQTT auth
infra_ns.secret("registry-credentials")
    .set("username", "registry-user")
    .set("password", "registry-pass")
    .apply();

infra_ns.secret("mqtt-credentials")
    .set("mqtt-user", "iot-user")
    .set("mqtt-password", "mqtt-pass")
    .apply();

// Example 13: Advanced IoT edge setup
let edge_ns = k.namespace("iot-edge");

// Edge-local registry
edge_ns.registry("edge-registry")
    .host("edge-registry.local")
    .port(5000)
    .apply();

// Edge MQTT broker
edge_ns.mqtt("edge-mqtt")
    .image("mosquitto:latest")
    .port(1883)
    .auth("edge-user", "edge-pass")
    .apply();

// Edge services
let edge_services = [
    #{ name: "edge-device-manager", replicas: 1 },
    #{ name: "edge-data-processor", replicas: 1 }
];

for svc in edge_services {
    edge_ns.deployment(svc.name)
        .image("node:18-alpine")
        .replicas(svc.replicas)
        .apply();

    edge_ns.service(svc.name + "-svc")
        .target_deployment(svc.name)
        .port(8080)
        .apply();
}

// Example 14: Full IoT cloud-edge infrastructure
let cloud_ns = k.namespace("iot-cloud");

// Central registry
cloud_ns.registry("cloud-registry")
    .host("registry.cloud")
    .port(5000)
    .auth("cloud-admin", "secure-pass")
    .apply();

// Message broker cluster
cloud_ns.mqtt("cloud-mqtt-primary")
    .image("mosquitto:latest")
    .port(1883)
    .apply();

cloud_ns.mqtt("cloud-mqtt-secondary")
    .image("mosquitto:latest")
    .port(1883)
    .apply();

// Cloud services
cloud_ns.deployment("cloud-api")
    .image("node:18-alpine")
    .replicas(3)
    .apply();

cloud_ns.service("cloud-api-svc")
    .target_deployment("cloud-api")
    .port(8080)
    .apply();

cloud_ns.deployment("cloud-dashboard")
    .image("nginx:latest")
    .replicas(2)
    .apply();

cloud_ns.service("cloud-dashboard-svc")
    .target_deployment("cloud-dashboard")
    .port(80)
    .apply();

// Example 15: Display full infrastructure status
print(k.md());
print("Cloud namespace status:");
print(cloud_ns.md());
print("Edge namespace status:");
print(edge_ns.md());
