apiVersion: apps/v1
kind: Deployment
metadata:
  name: suppliers-service
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: suppliers-service
    app.kubernetes.io/component: microservice
    app.kubernetes.io/part-of: bakery-ia
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: suppliers-service
      app.kubernetes.io/component: microservice
  template:
    metadata:
      labels:
        app.kubernetes.io/name: suppliers-service
        app.kubernetes.io/component: microservice
    spec:
      initContainers:
      # Wait for Redis to be ready
      - name: wait-for-redis
        image: redis:7.4-alpine
        command:
        - sh
        - -c
        - |
          echo "Waiting for Redis to be ready..."
          until redis-cli -h $REDIS_HOST -p $REDIS_PORT --tls --cert /tls/redis-cert.pem --key /tls/redis-key.pem --cacert /tls/ca-cert.pem -a "$REDIS_PASSWORD" ping | grep -q PONG; do
            echo "Redis not ready yet, waiting..."
            sleep 2
          done
          echo "Redis is ready!"
        env:
        - name: REDIS_HOST
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: REDIS_HOST
        - name: REDIS_PORT
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: REDIS_PORT
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-secrets
              key: REDIS_PASSWORD
        volumeMounts:
        - name: redis-tls
          mountPath: /tls
          readOnly: true
      - name: wait-for-migration
        image: postgres:17-alpine
        command:
        - sh
        - -c
        - |
          echo "Waiting for suppliers database and migrations to be ready..."
          # Wait for database to be accessible
          until pg_isready -h $SUPPLIERS_DB_HOST -p $SUPPLIERS_DB_PORT -U $SUPPLIERS_DB_USER; do
            echo "Database not ready yet, waiting..."
            sleep 2
          done
          echo "Database is ready!"
          # Give migrations extra time to complete after DB is ready
          echo "Waiting for migrations to complete..."
          sleep 10
          echo "Ready to start service"
        env:
        - name: SUPPLIERS_DB_HOST
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: SUPPLIERS_DB_HOST
        - name: SUPPLIERS_DB_PORT
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: DB_PORT
        - name: SUPPLIERS_DB_USER
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: SUPPLIERS_DB_USER
      containers:
      - name: suppliers-service
        image: bakery/suppliers-service:latest
        ports:
        - containerPort: 8000
          name: http
        envFrom:
        - configMapRef:
            name: bakery-config
        - secretRef:
            name: database-secrets
        - secretRef:
            name: redis-secrets
        - secretRef:
            name: rabbitmq-secrets
        - secretRef:
            name: jwt-secrets
        - secretRef:
            name: external-api-secrets
        - secretRef:
            name: payment-secrets
        - secretRef:
            name: email-secrets
        - secretRef:
            name: monitoring-secrets
        - secretRef:
            name: pos-integration-secrets
        - secretRef:
            name: whatsapp-secrets
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health/live
            port: 8000
          initialDelaySeconds: 30
          timeoutSeconds: 5
          periodSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /health/ready
            port: 8000
          initialDelaySeconds: 15
          timeoutSeconds: 3
          periodSeconds: 5
          failureThreshold: 5
      volumes:
      - name: redis-tls
        secret:
          secretName: redis-tls-secret
          defaultMode: 0400

---
apiVersion: v1
kind: Service
metadata:
  name: suppliers-service
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: suppliers-service
    app.kubernetes.io/component: microservice
spec:
  type: ClusterIP
  ports:
  - port: 8000
    targetPort: 8000
    protocol: TCP
    name: http
  selector:
    app.kubernetes.io/name: suppliers-service
    app.kubernetes.io/component: microservice
