apiVersion: apps/v1
kind: Deployment
metadata:
  name: gotrue
  namespace: appflowy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gotrue
  template:
    metadata:
      labels:
        app: gotrue
    spec:
      containers:
        - name: gotrue
          image: appflowyinc/gotrue:latest
          ports:
            - containerPort: 9999
          env:
            - name: GOTRUE_SAML_ENABLED
              value: "true"
            - name: GOTRUE_SAML_PRIVATE_KEY
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_SAML_PRIVATE_KEY
            # ----- DB (Postgres HA) -----
            - name: GOTRUE_DB_DRIVER
              value: postgres
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_DATABASE_URL
            - name: GOTRUE_ADMIN_EMAIL
              value: hello@beatrice.wtf
            - name: GOTRUE_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_ADMIN_PASSWORD
            - name: GOTRUE_DISABLE_SIGNUP
              value: "true"
            - name: GOTRUE_SITE_URL
              value: "appflowy-flutter://"
            - name: GOTRUE_URI_ALLOW_LIST
              value: "**"
            - name: GOTRUE_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_JWT_SECRET
            - name: GOTRUE_JWT_EXP
              value: "7200"
            - name: GOTRUE_SMTP_HOST
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_HOST
            - name: GOTRUE_SMTP_PORT
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_PORT
            - name: GOTRUE_SMTP_USER
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_USER
            - name: GOTRUE_SMTP_PASS
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_PASS
            - name: GOTRUE_SMTP_ADMIN_EMAIL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_USER
            - name: PORT
              value: "9999"
            - name: GOTRUE_JWT_ADMIN_GROUP_NAME
              value: supabase_admin
            - name: API_EXTERNAL_URL
              value: https://orbit.panic.haus/gotrue
            - name: GOTRUE_MAILER_URLPATHS_CONFIRMATION
              value: /gotrue/verify
            - name: GOTRUE_MAILER_URLPATHS_INVITE
              value: /gotrue/verify
            - name: GOTRUE_MAILER_URLPATHS_RECOVERY
              value: /gotrue/verify
            - name: GOTRUE_MAILER_URLPATHS_EMAIL_CHANGE
              value: /gotrue/verify


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: appflowy-cloud
  namespace: appflowy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: appflowy-cloud
  template:
    metadata:
      labels:
        app: appflowy-cloud
    spec:
      containers:
        - name: appflowy-cloud
          image: appflowyinc/appflowy_cloud:latest
          ports:
            - containerPort: 8000
          env:
            # ----- Database -----
            - name: APPFLOWY_DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_DATABASE_URL
            - name: APPFLOWY_REDIS_URI
              value: "redis://redis-lb.redis.svc.cluster.local:6379"

            # ----- GoTrue (Auth) -----
            - name: APPFLOWY_GOTRUE_BASE_URL
              value: "http://gotrue.appflowy.svc.cluster.local:9999"
            - name: APPFLOWY_GOTRUE_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_JWT_SECRET
            - name: APPFLOWY_GOTRUE_JWT_EXP
              value: "7200"

            # ----- S3 / Minio -----
            - name: APPFLOWY_S3_USE_MINIO
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_USE_MINIO
            - name: APPFLOWY_S3_MINIO_URL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_MINIO_URL
            - name: APPFLOWY_S3_BUCKET
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_BUCKET
            - name: APPFLOWY_S3_REGION
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: AWS_REGION
            - name: APPFLOWY_S3_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: AWS_ACCESS_KEY
            - name: APPFLOWY_S3_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: AWS_SECRET_KEY
            #- name: APPFLOWY_S3_PRESIGNED_URL_ENDPOINT
            #  value: "https://minio.example.com"
              # ← Replace with your actual public Minio endpoint if different

            # ----- Mailer (AppFlowy Cloud) -----
            - name: APPFLOWY_MAILER_SMTP_HOST
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_HOST
            - name: APPFLOWY_MAILER_SMTP_PORT
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_PORT
            - name: APPFLOWY_MAILER_SMTP_USERNAME
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_USER
            - name: APPFLOWY_MAILER_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_PASS
            - name: APPFLOWY_MAILER_SMTP_EMAIL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_USER
            - name: APPFLOWY_MAILER_SMTP_TLS_KIND
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: SMTP_TLS_KIND

            # ----- General -----
            - name: APPFLOWY_ACCESS_CONTROL
              value: "true"
            - name: RUST_LOG
              value: info
            - name: APPFLOWY_ENVIRONMENT
              value: production
            - name: APPFLOWY_WEB_URL
              value: "https://orbit.panic.haus"   # ← your public AppFlowy URL
          readinessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 20
            periodSeconds: 20

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: admin-frontend
  namespace: appflowy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: admin-frontend
  template:
    metadata:
      labels:
        app: admin-frontend
    spec:
      containers:
        - name: admin-frontend
          image: appflowyinc/admin_frontend:latest
          ports:
            - containerPort: 80
          env:
            - name: ADMIN_FRONTEND_REDIS_URL
              value: "redis://redis-lb.redis.svc.cluster.local:6379"
            - name: ADMIN_FRONTEND_GOTRUE_URL
              value: "http://gotrue.appflowy.svc.cluster.local:9999"
            - name: ADMIN_FRONTEND_APPFLOWY_CLOUD_URL
              value: "http://appflowy-cloud.appflowy.svc.cluster.local:8000"
            - name: ADMIN_FRONTEND_PATH_PREFIX
              value: "/console"
            - name: ADMIN_FRONTEND_PORT
              value: "80"
          readinessProbe:
            httpGet:
              path: /console
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 10

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: appflowy-worker
  namespace: appflowy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: appflowy-worker
  template:
    metadata:
      labels:
        app: appflowy-worker
    spec:
      containers:
        - name: appflowy-worker
          image: appflowyinc/appflowy_worker:latest
          env:
            - name: RUST_LOG
              value: info
            - name: APPFLOWY_ENVIRONMENT
              value: production
            - name: APPFLOWY_WORKER_REDIS_URL
              value: "redis://redis-lb.redis.svc.cluster.local:6379"
            - name: APPFLOWY_WORKER_DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: GOTRUE_DATABASE_URL
            - name: APPFLOWY_WORKER_DATABASE_NAME
              value: appflowy_db
            - name: APPFLOWY_S3_USE_MINIO
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_USE_MINIO
            - name: APPFLOWY_S3_MINIO_URL
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_MINIO_URL
            - name: APPFLOWY_S3_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: AWS_ACCESS_KEY
            - name: APPFLOWY_S3_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: AWS_SECRET_KEY
            - name: APPFLOWY_S3_BUCKET
              valueFrom:
                secretKeyRef:
                  name: appflowy-secrets
                  key: APPFLOWY_S3_BUCKET

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: appflowy-web
  namespace: appflowy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: appflowy-web
  template:
    metadata:
      labels:
        app: appflowy-web
    spec:
      containers:
        - name: appflowy-web
          image: appflowyinc/appflowy_web:latest
          ports:
            - containerPort: 80
          env:
            - name: APPFLOWY_CLOUD_URL
              value: "http://appflowy-cloud.appflowy.svc.cluster.local:8000"