apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-session-service
  namespace: bakery-ia
  labels:
    app: demo-session-service
    component: demo-session
spec:
  replicas: 2
  selector:
    matchLabels:
      app: demo-session-service
  template:
    metadata:
      labels:
        app: demo-session-service
        component: demo-session
    spec:
      serviceAccountName: demo-session-sa
      containers:
      - name: demo-session-service
        image: bakery/demo-session-service:latest
        ports:
        - containerPort: 8000
          name: http
        envFrom:
        - configMapRef:
            name: bakery-config
        env:
        - name: SERVICE_NAME
          value: "demo-session-service"
        - name: ALERT_PROCESSOR_SERVICE_URL
          value: "http://alert-processor:8000"
        - name: DEMO_SESSION_DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DATABASE_URL
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-secrets
              key: REDIS_PASSWORD
        - name: REDIS_URL
          value: "rediss://:$(REDIS_PASSWORD)@redis-service:6379/0?ssl_cert_reqs=none"
        - name: AUTH_SERVICE_URL
          value: "http://auth-service:8000"
        - name: TENANT_SERVICE_URL
          value: "http://tenant-service:8000"
        - name: INVENTORY_SERVICE_URL
          value: "http://inventory-service:8000"
        - name: RECIPES_SERVICE_URL
          value: "http://recipes-service:8000"
        - name: SALES_SERVICE_URL
          value: "http://sales-service:8000"
        - name: ORDERS_SERVICE_URL
          value: "http://orders-service:8000"
        - name: PRODUCTION_SERVICE_URL
          value: "http://production-service:8000"
        - name: SUPPLIERS_SERVICE_URL
          value: "http://suppliers-service:8000"
        - name: LOG_LEVEL
          value: "INFO"
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 30
          periodSeconds: 30
        readinessProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 10
          periodSeconds: 10
        startupProbe:
          httpGet:
            path: /health
            port: 8000
          initialDelaySeconds: 10
          periodSeconds: 5
          failureThreshold: 30
      initContainers:
      - name: wait-for-redis
        image: busybox:1.36
        command: ['sh', '-c', 'until nc -z redis-service 6379; do echo waiting for redis; sleep 2; done']
      - name: wait-for-migrations
        image: postgres:17-alpine
        command: ["sh", "-c",
          "echo 'Waiting for database to be ready...' && \
           until pg_isready -h demo-session-db-service -p 5432; do sleep 2; done && \
           echo 'Database ready, checking for demo_sessions table...' && \
           MAX_ATTEMPTS=60 && \
           ATTEMPT=1 && \
           while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do \
             if psql -h demo-session-db-service -U demo_session_user -d demo_session_db -c 'SELECT 1 FROM demo_sessions LIMIT 1;' 2>/dev/null; then \
               break; \
             fi; \
             echo \"Waiting for demo_sessions table to be created by migrations... (attempt $ATTEMPT/$MAX_ATTEMPTS)\"; \
             ATTEMPT=$((ATTEMPT + 1)); \
             sleep 5; \
           done && \
           if [ $ATTEMPT -gt $MAX_ATTEMPTS ]; then \
             echo 'ERROR: demo_sessions table not created after maximum attempts'; \
             exit 1; \
           fi && \
           echo 'demo_sessions table is ready!' && \
           echo 'Checking if table has required columns...' && \
           psql -h demo-session-db-service -U demo_session_user -d demo_session_db -c '\\d demo_sessions' && \
           echo 'Table structure verified!'"]
        env:
        - name: PGPASSWORD
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DB_PASSWORD
        resources:
          requests:
            memory: "64Mi"
            cpu: "50m"
          limits:
            memory: "128Mi"
            cpu: "100m"
