# infrastructure/kubernetes/base/jobs/external-data-init-job.yaml
# One-time job to initialize 24 months of historical data for all enabled cities
apiVersion: batch/v1
kind: Job
metadata:
  name: external-data-init
  namespace: bakery-ia
  labels:
    app: external-service
    component: data-initialization
spec:
  ttlSecondsAfterFinished: 86400
  backoffLimit: 3
  template:
    metadata:
      labels:
        app: external-service
        job: data-init
    spec:
      imagePullSecrets:
      - name: dockerhub-creds
      restartPolicy: OnFailure

      initContainers:
      - name: wait-for-db
        image: postgres:17-alpine
        command:
          - sh
          - -c
          - |
            until pg_isready -h $EXTERNAL_DB_HOST -p $DB_PORT -U $EXTERNAL_DB_USER; do
              echo "Waiting for database..."
              sleep 2
            done
            echo "Database is ready"
        envFrom:
        - configMapRef:
            name: bakery-config
        - secretRef:
            name: database-secrets
      - name: wait-for-migration
        image: postgres:17-alpine
        command:
          - sh
          - -c
          - |
            echo "Waiting for external-service migration to complete..."
            sleep 15
            echo "Migration should be complete"
        envFrom:
        - configMapRef:
            name: bakery-config

      containers:
      - name: data-loader
        image: bakery/external-service:latest
        imagePullPolicy: Always

        command:
          - python
          - -m
          - app.jobs.initialize_data

        args:
          - "--months=6"  # Reduced from 24 to avoid memory/rate limit issues
          - "--log-level=INFO"

        envFrom:
        - configMapRef:
            name: bakery-config
        - secretRef:
            name: database-secrets
        - secretRef:
            name: external-api-secrets

        resources:
          requests:
            memory: "2Gi"  # Increased from 1Gi
            cpu: "500m"
          limits:
            memory: "4Gi"  # Increased from 2Gi
            cpu: "1000m"
