apiVersion: v1
kind: ConfigMap
metadata:
  name: surrealdb-config
  namespace: vapora
data:
  startup.sql: |
    -- SurrealDB startup SQL
    DEFINE NAMESPACE vapora;
    USE NAMESPACE vapora;
    DEFINE DATABASE main;
    USE DATABASE main;

    -- Define scopes for multi-tenancy
    DEFINE SCOPE user SESSION 24h;

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: surrealdb
  namespace: vapora
  labels:
    app: surrealdb
    component: database
spec:
  serviceName: surrealdb
  replicas: 1
  selector:
    matchLabels:
      app: surrealdb
  template:
    metadata:
      labels:
        app: surrealdb
        component: database
    spec:
      containers:
      - name: surrealdb
        image: surrealdb/surrealdb:latest
        args:
          - start
          - --log
          - info
          - --user
          - root
          - --pass
          - root
          - file:/data
        ports:
        - containerPort: 8000
          name: http
          protocol: TCP
        env:
        - name: SURREAL_PATH
          value: "/data"
        volumeMounts:
        - name: data
          mountPath: /data
        - name: config
          mountPath: /etc/surrealdb
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 5
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3
        resources:
          requests:
            cpu: 200m
            memory: 256Mi
          limits:
            cpu: 500m
            memory: 512Mi
      volumes:
      - name: config
        configMap:
          name: surrealdb-config
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 20Gi

---
apiVersion: v1
kind: Service
metadata:
  name: surrealdb
  namespace: vapora
  labels:
    app: surrealdb
    component: database
spec:
  clusterIP: None
  selector:
    app: surrealdb
  ports:
  - port: 8000
    targetPort: 8000
    name: http
    protocol: TCP
