#!/usr/bin/env herodo
// TCP Route Configuration Examples
// This example shows how to create TCP routes for databases and other services

let namespace = "databases";

// Create a KubernetesManager for the namespace
let km = kubernetes(namespace);
print(`Connected to namespace: ${namespace(km)}`);

// ============================================
// PostgreSQL TCP Route
// ============================================

let postgres = tcp_route("postgres-main", namespace)
    .entrypoint("postgres")
    .service("postgres-primary", 5432)
    .build();

print("--- PostgreSQL Route ---");
print(to_yaml(postgres));

// ============================================
// Redis TCP Route
// ============================================

let redis = tcp_route("redis-cache", namespace)
    .entrypoint("redis")
    .service("redis-master", 6379)
    .build();

print("\n--- Redis Route ---");
print(to_yaml(redis));

// ============================================
// MySQL TCP Route with SNI
// ============================================

let mysql = tcp_route("mysql-main", namespace)
    .entrypoint("mysql")
    .sni("mysql.db.example.com")
    .service("mysql-primary", 3306)
    .tls_passthrough(true)
    .build();

print("\n--- MySQL Route with SNI ---");
print(to_yaml(mysql));

// ============================================
// MongoDB TCP Route
// ============================================

let mongodb = tcp_route("mongodb-cluster", namespace)
    .entrypoint("mongodb")
    .service("mongodb-router", 27017)
    .build();

print("\n--- MongoDB Route ---");
print(to_yaml(mongodb));

// ============================================
// Multi-database SNI routing
// ============================================

let db1 = tcp_route("db1-route", namespace)
    .entrypoint("databases")
    .sni("db1.example.com")
    .service("database-1", 5432)
    .tls_passthrough(true)
    .build();

let db2 = tcp_route("db2-route", namespace)
    .entrypoint("databases")
    .sni("db2.example.com")
    .service("database-2", 5432)
    .tls_passthrough(true)
    .build();

print("\n--- Multi-Database SNI Routing ---");
print("DB1 Route:");
print(to_yaml(db1));
print("\nDB2 Route:");
print(to_yaml(db2));

// ============================================
// SNI Regex for wildcard routing
// ============================================

let wildcard_db = tcp_route("wildcard-db", namespace)
    .entrypoint("databases")
    .sni_regex(".*\\.db\\.example\\.com")
    .service("db-router", 5432)
    .tls_passthrough(true)
    .build();

print("\n--- Wildcard SNI Route ---");
print(to_yaml(wildcard_db));

// ============================================
// Apply TCP routes to cluster
// ============================================

// Uncomment to apply:
// apply_tcp_route(km, postgres);
// apply_tcp_route(km, redis);
// apply_tcp_route(km, mysql);
// print("TCP routes applied!");

// List existing TCP routes
// let routes = list_tcp_routes(km, namespace);
// print("\nExisting TCP routes:");
// for r in routes {
//     print(`  - ${r}`);
// }

print("\n=== TCP Route Configuration Complete ===");
