# EN: FnB Engine - Table, Session, Kitchen & Reservation Management (SignalR for Kitchen Display)
# VI: FnB Engine - Quan ly Ban, Phien, Bep & Dat ban (SignalR cho Man hinh bep)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fnb-engine
  namespace: production
  labels:
    app: fnb-engine
    environment: production
    platform: goodgo
    tier: backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: fnb-engine
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: fnb-engine
        environment: production
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - fnb-engine
              topologyKey: kubernetes.io/hostname
      containers:
      - name: fnb-engine
        image: goodgo/fnb-engine-net:IMAGE_TAG
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
        envFrom:
        - configMapRef:
            name: goodgo-config
        - secretRef:
            name: goodgo-secrets
        env:
        - name: ConnectionStrings__DefaultConnection
          valueFrom:
            secretKeyRef:
              name: goodgo-secrets
              key: FNB_DATABASE_URL
        - name: IamService__ServiceName
          value: "fnb-engine"
        # EN: Redis for SignalR backplane (Kitchen Display)
        # VI: Redis cho SignalR backplane (Man hinh bep)
        - name: ConnectionStrings__Redis
          valueFrom:
            secretKeyRef:
              name: goodgo-secrets
              key: ConnectionStrings__Redis
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
            path: /health/live
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /health/ready
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3
        startupProbe:
          httpGet:
            path: /health/live
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
          failureThreshold: 12
---
apiVersion: v1
kind: Service
metadata:
  name: fnb-engine
  namespace: production
  labels:
    app: fnb-engine
    environment: production
spec:
  selector:
    app: fnb-engine
  ports:
  - name: http
    protocol: TCP
    port: 8080
    targetPort: 8080
  type: ClusterIP
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: fnb-engine-hpa
  namespace: production
  labels:
    app: fnb-engine
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: fnb-engine
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
