import { CodeBlock } from "../components/CodeBlock";
import { Callout } from "../components/Callout";

export function KubernetesPage() {
  return (
    <div>
      <h1 id="kubernetes">Kubernetes Deployment (Helm)</h1>
      <p>
        Veylant IA ships a production-grade Helm chart at{" "}
        <code>deploy/helm/veylant-proxy/</code>. The chart includes Deployment, Service, HPA,
        PodDisruptionBudget, and ServiceMonitor resources.
      </p>

      <h2 id="prerequisites">Prerequisites</h2>
      <ul>
        <li>Kubernetes 1.28+ (EKS v1.31 tested)</li>
        <li>Helm 3.12+</li>
        <li>Metrics Server (for HPA)</li>
        <li>Prometheus Operator (optional, for ServiceMonitor)</li>
        <li>Istio 1.20+ (for blue/green deployment)</li>
      </ul>

      <h2 id="install">Install the Chart</h2>
      <CodeBlock
        language="bash"
        code={`# Add the Helm repository (GHCR OCI)
helm pull oci://ghcr.io/veylant/charts/veylant-proxy --version 1.0.0

# Install with production values
helm upgrade --install veylant-proxy \\
  oci://ghcr.io/veylant/charts/veylant-proxy \\
  --version 1.0.0 \\
  --namespace veylant \\
  --create-namespace \\
  -f deploy/helm/veylant-proxy/values-production.yaml \\
  --set image.tag=1.0.0 \\
  --set config.crypto.key=$(openssl rand -base64 32) \\
  --set config.providers.openai.apiKey=$OPENAI_API_KEY`}
      />

      <h2 id="values">Production Values</h2>
      <CodeBlock
        language="yaml"
        code={`# values-production.yaml
replicaCount: 3

image:
  repository: ghcr.io/veylant/ia-gateway
  tag: "1.0.0"

autoscaling:
  enabled: true
  minReplicas: 3
  maxReplicas: 15
  targetCPUUtilizationPercentage: 70
  targetMemoryUtilizationPercentage: 80

resources:
  requests:
    cpu: "500m"
    memory: "256Mi"
  limits:
    cpu: "2000m"
    memory: "1Gi"

config:
  server:
    env: production
  pii:
    fail_open: false    # return 503 on PII failure in production

podDisruptionBudget:
  enabled: true
  minAvailable: 1`}
      />

      <h2 id="hpa">Horizontal Pod Autoscaler</h2>
      <p>
        The HPA scales from 3 to 15 replicas based on CPU (70%) and memory (80%) utilization.
        Scale-up is fast (30s stabilization); scale-down is conservative (5 minutes) to avoid
        thrashing.
      </p>
      <CodeBlock
        language="bash"
        code={`# Check HPA status
kubectl get hpa -n veylant

# Watch scaling events
kubectl describe hpa veylant-proxy -n veylant`}
      />

      <h2 id="terraform">Terraform (AWS EKS)</h2>
      <p>
        Infrastructure as Code for a production EKS cluster is in{" "}
        <code>deploy/terraform/</code>:
      </p>
      <ul>
        <li>EKS v1.31, 3-AZ node groups (t3.large)</li>
        <li>S3 bucket for PostgreSQL backups (7-day retention)</li>
        <li>IRSA for pod-level AWS permissions</li>
        <li>VPC, subnets, security groups</li>
      </ul>
      <CodeBlock
        language="bash"
        code={`cd deploy/terraform
terraform init
terraform plan -var="region=eu-west-3" -var="cluster_name=veylant-prod"
terraform apply`}
      />

      <Callout type="warning" title="Terraform state">
        Configure remote state (S3 + DynamoDB locking) before running in production. The default
        local state is not suitable for team use.
      </Callout>
    </div>
  );
}
