apiVersion: apps/v1
kind: Deployment
metadata:
  name: ai-service
  namespace: erp
  labels:
    app: ai-service
    version: v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: ai-service
  template:
    metadata:
      labels:
        app: ai-service
        version: v1
    spec:
      containers:
        - name: ai-service
          image: erp-ai-service:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8087
              name: http
              protocol: TCP
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: "k8s"
            - name: DB_HOST
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: DB_HOST
            - name: DB_PORT
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: DB_PORT
            - name: DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: DB_NAME
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: DB_USER
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: DB_PASSWORD
            - name: REDIS_HOST
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: REDIS_HOST
            - name: REDIS_PORT
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: REDIS_PORT
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: REDIS_PASSWORD
            - name: NACOS_HOST
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: NACOS_HOST
            - name: AI_DEFAULT_SERVICE
              value: "openai"
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
            limits:
              cpu: 2000m
              memory: 1Gi
          livenessProbe:
            httpGet:
              path: /api/ai/health
              port: 8087
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /api/ai/health
              port: 8087
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
  name: ai-service
  namespace: erp
  labels:
    app: ai-service
spec:
  type: ClusterIP
  ports:
    - port: 8087
      targetPort: 8087
      protocol: TCP
      name: http
  selector:
    app: ai-service
---
apiVersion: v1
kind: Service
metadata:
  name: ai-service-lb
  namespace: erp
  labels:
    app: ai-service
spec:
  type: LoadBalancer
  ports:
    - port: 8087
      targetPort: 8087
      protocol: TCP
      name: http
  selector:
    app: ai-service
