"""
VAPORA Storage Configuration
Defines SurrealDB, Redis, NATS, and persistent volumes
"""

import k.api.all as k

# ===== SURREALDB STATEFULSET =====

surrealdb_statefulset = k.StatefulSet {
    apiVersion = "apps/v1"
    kind = "StatefulSet"
    metadata = {
        name = "surrealdb"
        namespace = "vapora-system"
        labels = {"app": "surrealdb"}
    }
    spec = {
        serviceName = "surrealdb"
        replicas = 3
        selector = {matchLabels = {"app": "surrealdb"}}
        template = {
            metadata = {labels = {"app": "surrealdb"}}
            spec = {
                containers = [{
                    name = "surrealdb"
                    image = "surrealdb/surrealdb:1.8"
                    imagePullPolicy = "IfNotPresent"
                    ports = [{name = "http", containerPort = 8000}]
                    args = [
                        "start",
                        "file:/data/vapora",
                        "--auth",
                        "--user", "root",
                        "--pass", "$(SURREAL_PASSWORD)"
                    ]
                    env = [
                        {name = "SURREAL_PASSWORD", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "surrealdb-password"}}}
                        {name = "RUST_LOG", value = "info"}
                    ]
                    livenessProbe = {
                        httpGet = {path = "/health", port = 8000}
                        initialDelaySeconds = 30
                        periodSeconds = 10
                    }
                    readinessProbe = {
                        httpGet = {path = "/health", port = 8000}
                        initialDelaySeconds = 10
                        periodSeconds = 5
                    }
                    resources = {
                        requests = {cpu = "2000m", memory = "4Gi"}
                        limits = {cpu = "4000m", memory = "8Gi"}
                    }
                    volumeMounts = [
                        {name = "data", mountPath = "/data"}
                    ]
                }]
            }
        }
        volumeClaimTemplates = [{
            metadata = {name = "data"}
            spec = {
                accessModes = ["ReadWriteOnce"]
                storageClassName = "ssd"
                resources = {requests = {storage = "50Gi"}}
            }
        }]
    }
}

surrealdb_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "surrealdb", namespace = "vapora-system"}
    spec = {
        clusterIP = "None"  # Headless service
        ports = [{name = "http", port = 8000, targetPort = 8000}]
        selector = {"app": "surrealdb"}
    }
}

# ===== REDIS STATEFULSET =====

redis_statefulset = k.StatefulSet {
    apiVersion = "apps/v1"
    kind = "StatefulSet"
    metadata = {
        name = "redis"
        namespace = "vapora-system"
        labels = {"app": "redis"}
    }
    spec = {
        serviceName = "redis"
        replicas = 3
        selector = {matchLabels = {"app": "redis"}}
        template = {
            metadata = {labels = {"app": "redis"}}
            spec = {
                containers = [{
                    name = "redis"
                    image = "redis:7.2-alpine"
                    imagePullPolicy = "IfNotPresent"
                    ports = [{name = "redis", containerPort = 6379}]
                    command = [
                        "redis-server",
                        "--masterauth", "$(REDIS_PASSWORD)",
                        "--requirepass", "$(REDIS_PASSWORD)",
                        "--appendonly", "yes",
                        "--replicaof", "redis-0.redis.vapora-system.svc.cluster.local", "6379"
                    ]
                    env = [
                        {name = "REDIS_PASSWORD", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "redis-password"}}}
                    ]
                    livenessProbe = {
                        exec = {command = ["redis-cli", "ping"]}
                        initialDelaySeconds = 30
                        periodSeconds = 10
                    }
                    readinessProbe = {
                        exec = {command = ["redis-cli", "ping"]}
                        initialDelaySeconds = 10
                        periodSeconds = 5
                    }
                    resources = {
                        requests = {cpu = "1000m", memory = "2Gi"}
                        limits = {cpu = "2000m", memory = "4Gi"}
                    }
                    volumeMounts = [
                        {name = "data", mountPath = "/data"}
                    ]
                }]
            }
        }
        volumeClaimTemplates = [{
            metadata = {name = "data"}
            spec = {
                accessModes = ["ReadWriteOnce"]
                storageClassName = "ssd"
                resources = {requests = {storage = "20Gi"}}
            }
        }]
    }
}

redis_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "redis", namespace = "vapora-system"}
    spec = {
        clusterIP = "None"  # Headless service
        ports = [{name = "redis", port = 6379, targetPort = 6379}]
        selector = {"app": "redis"}
    }
}

# ===== NATS JETSTREAM STATEFULSET =====

nats_statefulset = k.StatefulSet {
    apiVersion = "apps/v1"
    kind = "StatefulSet"
    metadata = {
        name = "nats"
        namespace = "vapora-system"
        labels = {"app": "nats"}
    }
    spec = {
        serviceName = "nats"
        replicas = 3
        selector = {matchLabels = {"app": "nats"}}
        template = {
            metadata = {labels = {"app": "nats"}}
            spec = {
                containers = [{
                    name = "nats"
                    image = "nats:2.10-alpine"
                    imagePullPolicy = "IfNotPresent"
                    ports = [
                        {name = "client", containerPort = 4222}
                        {name = "cluster", containerPort = 6222}
                        {name = "monitor", containerPort = 8222}
                    ]
                    command = ["nats-server"]
                    args = [
                        "-c", "/etc/nats/nats.conf"
                    ]
                    livenessProbe = {
                        httpGet = {path = "/varz", port = 8222}
                        initialDelaySeconds = 30
                        periodSeconds = 10
                    }
                    readinessProbe = {
                        exec = {command = ["nats", "-s", "nats://localhost:4222", "server", "info"]}
                        initialDelaySeconds = 10
                        periodSeconds = 5
                    }
                    resources = {
                        requests = {cpu = "1000m", memory = "2Gi"}
                        limits = {cpu = "2000m", memory = "4Gi"}
                    }
                    volumeMounts = [
                        {name = "config", mountPath = "/etc/nats", readOnly = true}
                        {name = "data", mountPath = "/var/lib/nats"}
                    ]
                }]
                volumes = [{
                    name = "config"
                    configMap = {name = "nats-config"}
                }]
            }
        }
        volumeClaimTemplates = [{
            metadata = {name = "data"}
            spec = {
                accessModes = ["ReadWriteOnce"]
                storageClassName = "ssd"
                resources = {requests = {storage = "30Gi"}}
            }
        }]
    }
}

nats_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "nats", namespace = "vapora-system"}
    spec = {
        clusterIP = "None"  # Headless service
        ports = [
            {name = "client", port = 4222, targetPort = 4222}
            {name = "cluster", port = 6222, targetPort = 6222}
            {name = "monitor", port = 8222, targetPort = 8222}
        ]
        selector = {"app": "nats"}
    }
}

# ===== PERSISTENT VOLUME CLAIMS =====

pvc_agent_state = k.PersistentVolumeClaim {
    apiVersion = "v1"
    kind = "PersistentVolumeClaim"
    metadata = {
        name = "vapora-agent-state-pvc"
        namespace = "vapora-system"
    }
    spec = {
        accessModes = ["ReadWriteMany"]
        storageClassName = "ssd"
        resources = {requests = {storage = "20Gi"}}
    }
}

pvc_routing_cache = k.PersistentVolumeClaim {
    apiVersion = "v1"
    kind = "PersistentVolumeClaim"
    metadata = {
        name = "vapora-routing-cache-pvc"
        namespace = "vapora-system"
    }
    spec = {
        accessModes = ["ReadWriteOnce"]
        storageClassName = "ssd"
        resources = {requests = {storage = "5Gi"}}
    }
}

pvc_plugins = k.PersistentVolumeClaim {
    apiVersion = "v1"
    kind = "PersistentVolumeClaim"
    metadata = {
        name = "vapora-plugins-pvc"
        namespace = "vapora-system"
    }
    spec = {
        accessModes = ["ReadWriteMany"]
        storageClassName = "ssd"
        resources = {requests = {storage = "10Gi"}}
    }
}

# ===== NATS CONFIG MAP =====

nats_config = k.ConfigMap {
    apiVersion = "v1"
    kind = "ConfigMap"
    metadata = {
        name = "nats-config"
        namespace = "vapora-system"
    }
    data = {
        "nats.conf" = """
port: 4222
cluster {
  port: 6222
  routes: [
    nats://nats-0.nats.vapora-system.svc.cluster.local:6222
    nats://nats-1.nats.vapora-system.svc.cluster.local:6222
    nats://nats-2.nats.vapora-system.svc.cluster.local:6222
  ]
}
jetstream {
  store_dir: /var/lib/nats
  max_memory_store: 8GB
  max_file_store: 30GB
}
monitor_port: 8222
"""
    }
}

# ===== OUTPUT =====

output = {
    surrealdb = {statefulset = surrealdb_statefulset, service = surrealdb_service}
    redis = {statefulset = redis_statefulset, service = redis_service}
    nats = {statefulset = nats_statefulset, service = nats_service, config = nats_config}
    pvcs = {
        agent_state = pvc_agent_state
        routing_cache = pvc_routing_cache
        plugins = pvc_plugins
    }
}
