name: 12-Hour Embedding Generation (Voyage AI)

on:
  schedule:
    # Runs every 12 hours (at midnight and noon UTC)
    - cron: '0 0,12 * * *'
  workflow_dispatch: # Manual trigger

jobs:
  generate-embeddings:
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
    # 1. Checkout code
    - name: Checkout code
      uses: actions/checkout@v4

    # 2. Set up Python with caching
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'
        cache: 'pip'

    # 3. Install Dependencies
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    # 4. Run Embedding Job
    - name: Generate Embeddings (Voyage AI)
      env:
        MOTHERDUCK_TOKEN: ${{ secrets.MOTHERDUCK_TOKEN }}
        VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
      run: |
        echo "🧠 Starting Embedding Generation..."
        python -m dagster job execute -f etl/embedding_job.py -j gdelt_embedding_job
        echo "✅ Embedding Generation Complete."

    # 5. Run Headline Polish Job (runs even if embeddings failed)
    - name: Polish Headlines (Cerebras)
      if: always()
      env:
        MOTHERDUCK_TOKEN: ${{ secrets.MOTHERDUCK_TOKEN }}
        CEREBRAS_API_KEY: ${{ secrets.CEREBRAS_API_KEY }}
      run: |
        echo "📰 Starting Headline Polish..."
        python -m dagster job execute -f etl/headline_polish_job.py -j gdelt_headline_polish_job
        echo "✅ Headline Polish Complete."

    - name: Notify on failure
      if: failure()
      run: |
        echo "::error::GDELT 12-hr embedding job failed — check logs above"
        curl -s -o /dev/null -w "%{http_code}" \
          -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
          -H "Accept: application/vnd.github.v3+json" \
          -X POST "https://api.github.com/repos/${{ github.repository }}/issues" \
          -d "{\"title\":\"[ALERT] GDELT embeddings failed $(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"body\":\"The 12-hr embedding generation job failed.\\n\\nRun: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\",\"labels\":[\"bug\"]}" || true
