apiVersion: batch/v1
kind: Job
metadata:
  name: fusero-backend-db-init
spec:
  backoffLimit: 0
  template:
    metadata:
      name: fusero-backend-db-init
    spec:
      imagePullSecrets:
        - name: regcred
      containers:
        - name: migrate-seed
          image: {{ .Values.backend.image }}
          command: ["/bin/sh", "-c"]
          args:
            - |
              echo "=== Current Directory ==="
              pwd
              ls -la
              
              echo "=== Environment Variables ==="
              env | grep -i postgres
              echo "=== Testing Connection ==="
              echo "Using password: $POSTGRES_PASSWORD"
              PGPASSWORD=$POSTGRES_PASSWORD psql -h postgres-service -p 5432 -U prod_admin -d fusero-db -c "SELECT 1"
              
              echo "=== Checking Database Tables ==="
              PGPASSWORD=$POSTGRES_PASSWORD psql -h postgres-service -p 5432 -U prod_admin -d fusero-db -c "\dt"
              
              echo "=== Running Migrations ==="
              npx mikro-orm migration:up
              
              echo "=== Checking Database Tables Again ==="
              PGPASSWORD=$POSTGRES_PASSWORD psql -h postgres-service -p 5432 -U prod_admin -d fusero-db -c "\dt"
              
              echo "=== Running Seeds ==="
              npm run seed
          env:
            - name: POSTGRES_HOSTNAME
              value: "postgres-service"
            - name: POSTGRES_PORT
              value: "5432"
            - name: POSTGRES_DB
              value: "fusero-db"
            - name: POSTGRES_USER
              value: "prod_admin"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fusero-backend-secrets
                  key: POSTGRES_PASSWORD
            - name: POSTGRES_HOST_AUTH_METHOD
              value: "scram-sha-256"
            - name: DEFAULT_ADMIN_USERNAME
              value: "{{ .Values.backend.env.DEFAULT_ADMIN_USERNAME }}"
            - name: DEFAULT_ADMIN_EMAIL
              value: "{{ .Values.backend.env.DEFAULT_ADMIN_EMAIL }}"
            - name: DEFAULT_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fusero-backend-secrets
                  key: DEFAULT_ADMIN_PASSWORD
            - name: JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: fusero-backend-secrets
                  key: JWT_SECRET
            - name: CHATGPT_API_KEY
              valueFrom:
                secretKeyRef:
                  name: fusero-backend-secrets
                  key: CHATGPT_API_KEY
            - name: CANVAS_API_KEY
              valueFrom:
                secretKeyRef:
                  name: fusero-backend-secrets
                  key: CANVAS_API_KEY
            - name: CANVAS_API_URL
              value: "{{ .Values.backend.env.CANVAS_API_URL }}"
            - name: NODE_ENV
              value: "production"
      restartPolicy: Never
