"""
VAPORA Microservices Configuration
Defines Deployment, Service, and ConfigMap for each VAPORA service
"""

import k.api.all as k

# ===== BACKEND SERVICE (Axum REST API) =====

backend_deployment = k.Deployment {
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = "vapora-backend"
        namespace = "vapora-system"
        labels = {"app": "vapora-backend"}
    }
    spec = {
        replicas = 3
        strategy = {type = "RollingUpdate", rollingUpdate = {maxSurge = 1, maxUnavailable = 0}}
        selector = {matchLabels = {"app": "vapora-backend"}}
        template = {
            metadata = {labels = {"app": "vapora-backend"}}
            spec = {
                serviceAccountName = "vapora-backend"
                containers = [{
                    name = "backend"
                    image = "vapora/backend:0.2.0"
                    imagePullPolicy = "IfNotPresent"
                    ports = [
                        {name = "http", containerPort = 8080, protocol = "TCP"}
                        {name = "metrics", containerPort = 9090, protocol = "TCP"}
                    ]
                    env = [
                        {name = "RUST_LOG", value = "info,vapora_backend=debug"}
                        {name = "DATABASE_URL", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "database-url"}}}
                        {name = "NATS_URL", value = "nats://nats-0.vapora-system:4222"}
                        {name = "REDIS_URL", value = "redis://redis-0.vapora-system:6379"}
                    ]
                    livenessProbe = {
                        httpGet = {path = "/api/v1/health", port = 8080}
                        initialDelaySeconds = 10
                        periodSeconds = 10
                    }
                    readinessProbe = {
                        httpGet = {path = "/api/v1/ready", port = 8080}
                        initialDelaySeconds = 5
                        periodSeconds = 5
                    }
                    resources = {
                        requests = {cpu = "1000m", memory = "2Gi"}
                        limits = {cpu = "2000m", memory = "4Gi"}
                    }
                    volumeMounts = [
                        {name = "config", mountPath = "/etc/vapora", readOnly = true}
                    ]
                }]
                volumes = [{
                    name = "config"
                    configMap = {name = "vapora-backend-config"}
                }]
            }
        }
    }
}

backend_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "vapora-backend", namespace = "vapora-system"}
    spec = {
        type = "ClusterIP"
        ports = [
            {name = "http", port = 8080, targetPort = 8080, protocol = "TCP"}
            {name = "metrics", port = 9090, targetPort = 9090, protocol = "TCP"}
        ]
        selector = {"app": "vapora-backend"}
    }
}

# ===== FRONTEND SERVICE (Leptos UI) =====

frontend_deployment = k.Deployment {
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = "vapora-frontend"
        namespace = "vapora-system"
        labels = {"app": "vapora-frontend"}
    }
    spec = {
        replicas = 2
        strategy = {type = "RollingUpdate", rollingUpdate = {maxSurge = 1, maxUnavailable = 0}}
        selector = {matchLabels = {"app": "vapora-frontend"}}
        template = {
            metadata = {labels = {"app": "vapora-frontend"}}
            spec = {
                containers = [{
                    name = "frontend"
                    image = "vapora/frontend:0.2.0"
                    imagePullPolicy = "IfNotPresent"
                    ports = [{name = "http", containerPort = 3000, protocol = "TCP"}]
                    env = [
                        {name = "API_ENDPOINT", value = "http://vapora-backend.vapora-system:8080"}
                        {name = "ENVIRONMENT", value = "production"}
                    ]
                    livenessProbe = {
                        httpGet = {path = "/", port = 3000}
                        initialDelaySeconds = 10
                        periodSeconds = 30
                    }
                    readinessProbe = {
                        httpGet = {path = "/", port = 3000}
                        initialDelaySeconds = 5
                        periodSeconds = 5
                    }
                    resources = {
                        requests = {cpu = "500m", memory = "512Mi"}
                        limits = {cpu = "1000m", memory = "1Gi"}
                    }
                }]
            }
        }
    }
}

frontend_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "vapora-frontend", namespace = "vapora-system"}
    spec = {
        type = "ClusterIP"
        ports = [{name = "http", port = 3000, targetPort = 3000, protocol = "TCP"}]
        selector = {"app": "vapora-frontend"}
    }
}

# ===== AGENT RUNTIME SERVICE =====

agents_deployment = k.Deployment {
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = "vapora-agents"
        namespace = "vapora-system"
        labels = {"app": "vapora-agents"}
    }
    spec = {
        replicas = 3
        strategy = {type = "RollingUpdate"}
        selector = {matchLabels = {"app": "vapora-agents"}}
        template = {
            metadata = {labels = {"app": "vapora-agents"}}
            spec = {
                serviceAccountName = "vapora-agents"
                nodeSelector = {"workload": "vapora"}
                containers = [{
                    name = "agents"
                    image = "vapora/agents:0.2.0"
                    imagePullPolicy = "IfNotPresent"
                    ports = [{name = "metrics", containerPort = 9090}]
                    env = [
                        {name = "RUST_LOG", value = "debug,vapora_agents=trace"}
                        {name = "NATS_URL", value = "nats://nats-0.vapora-system:4222"}
                        {name = "DATABASE_URL", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "database-url"}}}
                        {name = "CLAUDE_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "claude-api-key"}}}
                        {name = "OPENAI_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "openai-api-key"}}}
                        {name = "GEMINI_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "gemini-api-key"}}}
                    ]
                    resources = {
                        requests = {cpu = "4000m", memory = "8Gi"}
                        limits = {cpu = "8000m", memory = "16Gi"}
                    }
                    volumeMounts = [
                        {name = "agent-state", mountPath = "/var/vapora/agent-state"}
                    ]
                }]
                volumes = [{
                    name = "agent-state"
                    persistentVolumeClaim = {claimName = "vapora-agent-state-pvc"}
                }]
            }
        }
    }
}

agents_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "vapora-agents", namespace = "vapora-system"}
    spec = {
        clusterIP = "None"  # Headless service for StatefulSet-like behavior
        ports = [{name = "metrics", port = 9090, targetPort = 9090}]
        selector = {"app": "vapora-agents"}
    }
}

# ===== LLM ROUTER SERVICE =====

llm_router_deployment = k.Deployment {
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = "vapora-llm-router"
        namespace = "vapora-system"
        labels = {"app": "vapora-llm-router"}
    }
    spec = {
        replicas = 2
        strategy = {type = "RollingUpdate"}
        selector = {matchLabels = {"app": "vapora-llm-router"}}
        template = {
            metadata = {labels = {"app": "vapora-llm-router"}}
            spec = {
                containers = [{
                    name = "router"
                    image = "vapora/llm-router:0.2.0"
                    imagePullPolicy = "IfNotPresent"
                    ports = [
                        {name = "http", containerPort = 8899}
                        {name = "metrics", containerPort = 9090}
                    ]
                    env = [
                        {name = "RUST_LOG", value = "debug,vapora_llm_router=trace"}
                        {name = "DATABASE_URL", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "database-url"}}}
                        {name = "REDIS_URL", value = "redis://redis-0.vapora-system:6379"}
                        {name = "ROUTING_MODE", value = "hybrid"}
                        {name = "CLAUDE_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "claude-api-key"}}}
                        {name = "OPENAI_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "openai-api-key"}}}
                        {name = "GEMINI_API_KEY", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "gemini-api-key"}}}
                    ]
                    livenessProbe = {
                        httpGet = {path = "/health", port = 8899}
                        initialDelaySeconds = 10
                        periodSeconds = 10
                    }
                    resources = {
                        requests = {cpu = "1000m", memory = "2Gi"}
                        limits = {cpu = "2000m", memory = "4Gi"}
                    }
                    volumeMounts = [{name = "routing-cache", mountPath = "/routing-cache"}]
                }]
                volumes = [{
                    name = "routing-cache"
                    persistentVolumeClaim = {claimName = "vapora-routing-cache-pvc"}
                }]
            }
        }
    }
}

llm_router_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "vapora-llm-router", namespace = "vapora-system"}
    spec = {
        type = "ClusterIP"
        ports = [
            {name = "http", port = 8899, targetPort = 8899}
            {name = "metrics", port = 9090, targetPort = 9090}
        ]
        selector = {"app": "vapora-llm-router"}
    }
}

# ===== MCP GATEWAY SERVICE =====

mcp_gateway_deployment = k.Deployment {
    apiVersion = "apps/v1"
    kind = "Deployment"
    metadata = {
        name = "vapora-mcp-gateway"
        namespace = "vapora-system"
        labels = {"app": "vapora-mcp-gateway"}
    }
    spec = {
        replicas = 2
        strategy = {type = "RollingUpdate"}
        selector = {matchLabels = {"app": "vapora-mcp-gateway"}}
        template = {
            metadata = {labels = {"app": "vapora-mcp-gateway"}}
            spec = {
                containers = [{
                    name = "gateway"
                    image = "vapora/mcp-gateway:0.2.0"
                    imagePullPolicy = "IfNotPresent"
                    ports = [
                        {name = "http", containerPort = 8888}
                        {name = "metrics", containerPort = 9090}
                    ]
                    env = [
                        {name = "RUST_LOG", value = "debug,vapora_mcp=trace"}
                        {name = "NATS_URL", value = "nats://nats-0.vapora-system:4222"}
                        {name = "DATABASE_URL", valueFrom = {secretKeyRef = {name = "vapora-secrets", key = "database-url"}}}
                        {name = "MCP_PLUGINS_PATH", value = "/plugins"}
                    ]
                    livenessProbe = {
                        httpGet = {path = "/health", port = 8888}
                        initialDelaySeconds = 10
                        periodSeconds = 10
                    }
                    resources = {
                        requests = {cpu = "1000m", memory = "2Gi"}
                        limits = {cpu = "2000m", memory = "4Gi"}
                    }
                    volumeMounts = [{name = "plugins", mountPath = "/plugins"}]
                }]
                volumes = [{
                    name = "plugins"
                    persistentVolumeClaim = {claimName = "vapora-plugins-pvc"}
                }]
            }
        }
    }
}

mcp_gateway_service = k.Service {
    apiVersion = "v1"
    kind = "Service"
    metadata = {name = "vapora-mcp-gateway", namespace = "vapora-system"}
    spec = {
        type = "ClusterIP"
        ports = [
            {name = "http", port = 8888, targetPort = 8888}
            {name = "metrics", port = 9090, targetPort = 9090}
        ]
        selector = {"app": "vapora-mcp-gateway"}
    }
}

# ===== SERVICE ACCOUNTS =====

service_accounts = [
    {
        name = "vapora-backend"
        namespace = "vapora-system"
    },
    {
        name = "vapora-agents"
        namespace = "vapora-system"
    },
    {
        name = "vapora-llm-router"
        namespace = "vapora-system"
    }
]

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

output = {
    backend = {deployment = backend_deployment, service = backend_service}
    frontend = {deployment = frontend_deployment, service = frontend_service}
    agents = {deployment = agents_deployment, service = agents_service}
    llm_router = {deployment = llm_router_deployment, service = llm_router_service}
    mcp_gateway = {deployment = mcp_gateway_deployment, service = mcp_gateway_service}
}
