#!/usr/bin/env python3
import json
import requests
import sys

# Load current config
with open('/tmp/tunnel-config.json') as f:
    response = json.load(f)

current_config = response['result']['config']
current_ingress = current_config['ingress']

print(f"Current ingress rules: {len(current_ingress)}")
print("Current hostnames:", set(rule.get('hostname', 'default') for rule in current_ingress))

# New auth-dev3 ingress rules (insert BEFORE the catch-all 404)
auth_ingress = [
    {
        "hostname": "auth-dev3.heyarchie.com",
        "path": "/api/*",
        "service": "http://auth-api.authentication.svc.cluster.local:8000",
        "originRequest": {
            "noTLSVerify": True,
            "keepAliveConnections": 10
        }
    },
    {
        "hostname": "auth-dev3.heyarchie.com",
        "path": "/health",
        "service": "http://auth-api.authentication.svc.cluster.local:8000",
        "originRequest": {
            "noTLSVerify": True,
            "keepAliveConnections": 10
        }
    },
    {
        "hostname": "auth-dev3.heyarchie.com",
        "path": "/docs",
        "service": "http://auth-api.authentication.svc.cluster.local:8000",
        "originRequest": {
            "noTLSVerify": True,
            "keepAliveConnections": 10
        }
    },
    {
        "hostname": "auth-dev3.heyarchie.com",
        "service": "http://auth-api.authentication.svc.cluster.local:8000",
        "originRequest": {
            "noTLSVerify": True,
            "keepAliveConnections": 10
        }
    }
]

# Insert auth rules before the catch-all (last rule)
new_ingress = current_ingress[:-1] + auth_ingress + [current_ingress[-1]]

# Create new config
new_config = {
    "config": {
        "ingress": new_ingress,
        "warp-routing": current_config.get("warp-routing", {"enabled": False})
    }
}

print(f"\nNew ingress rules: {len(new_ingress)}")
print("New hostnames:", set(rule.get('hostname', 'default') for rule in new_ingress))

# Save to file
with open('/tmp/new-tunnel-config.json', 'w') as f:
    json.dump(new_config, f, indent=2)

print("\n✅ New configuration prepared")
print("   - Added 4 ingress rules for auth-dev3.heyarchie.com")
print("   - Configuration saved to /tmp/new-tunnel-config.json")

