---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: openwebui-data
  namespace: ai-stack
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: local-path
  resources:
    requests:
      storage: 5Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openwebui
  namespace: ai-stack
spec:
  replicas: 1
  selector:
    matchLabels:
      app: openwebui
  template:
    metadata:
      labels:
        app: openwebui
    spec:
      containers:
        - name: openwebui
          image: ghcr.io/open-webui/open-webui:main
          ports:
            - containerPort: 8080
          env:
            - name: OLLAMA_BASE_URL
              value: "http://ollama.ai-stack.svc.cluster.local:11434"
            - name: WEBUI_SECRET_KEY
              value: "cambia-esto-secret-key-openwebui"
            - name: DEFAULT_MODELS
              value: "qwen2.5:3b"
            - name: ENABLE_SIGNUP
              value: "false"
            - name: ENABLE_LOGIN_FORM
              value: "true"
          volumeMounts:
            - name: openwebui-data
              mountPath: /app/backend/data
          resources:
            requests:
              memory: "512Mi"
              cpu: "200m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 15
      volumes:
        - name: openwebui-data
          persistentVolumeClaim:
            claimName: openwebui-data

---
apiVersion: v1
kind: Service
metadata:
  name: openwebui
  namespace: ai-stack
spec:
  selector:
    app: openwebui
  ports:
    - port: 80
      targetPort: 8080
  type: ClusterIP

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: openwebui
  namespace: ai-stack
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/proxy-body-size: "50m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "300"
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - chat.cloud.mielito.com
      secretName: openwebui-tls
  rules:
    - host: chat.cloud.mielito.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: openwebui
                port:
                  number: 80
