apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-app
  labels:
    app: django-app
    component: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-app
  template:
    metadata:
      labels:
        app: django-app
        component: backend
    spec:
      containers:
      - name: django
        image: media-analyzer-backend:latest
        ports:
        - containerPort: 8000
        env:
        - name: DEBUG
          value: "false"
        - name: DJANGO_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: django-secrets
              key: secret-key
        - name: DB_HOST
          value: "postgres"
        - name: DB_NAME
          value: "media_analyzer"
        - name: DB_USER
          valueFrom:
            secretKeyRef:
              name: postgres-secrets
              key: username
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secrets
              key: password
        - name: REDIS_SERVICE_HOST
          value: "redis"
        - name: REDIS_SERVICE_PORT
          value: "6379"
        - name: ALLOWED_HOSTS
          value: "localhost,127.0.0.1,backend,frontend,nginx-rtmp,*"
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: "/var/secrets/google/credentials.json"
        - name: GCP_BUCKET_NAME
          value: "media-analyzer-segments"
        - name: GCP_PROJECT_ID
          valueFrom:
            configMapKeyRef:
              name: gcp-config
              key: project-id
        - name: USE_CLOUD_VISION
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: use-cloud-vision
        - name: USE_CLOUD_STORAGE
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: use-cloud-storage
        - name: LOGO_CONFIDENCE_THRESHOLD
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: logo-confidence-threshold
        volumeMounts:
        - name: gcp-credentials
          mountPath: /var/secrets/google
          readOnly: true
        # Note: When USE_CLOUD_STORAGE=true, media files go directly to GCS
        # Only mount local storage when USE_CLOUD_STORAGE=false
        - name: media-storage
          mountPath: /app/media
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /api/
            port: 8000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/
            port: 8000
          initialDelaySeconds: 5
          periodSeconds: 5
      volumes:
      - name: gcp-credentials
        secret:
          secretName: gcp-credentials
      - name: media-storage
        persistentVolumeClaim:
          claimName: media-pvc