name: Hourly GDELT Refresh (Polars)

# Scheduling: hourly. cron-job.org may still fire workflow_dispatch more often,
# so the throttle job below skips any run that starts less than 50 minutes
# after the last successful one. MotherDuck Lite has 10 CU-hours/month; the
# old 15-min cadence (96 runs/day) was burning the whole budget on ingest.
# Setup: see scripts/trigger_ingestion.py for cron-job.org configuration.
on:
  schedule:
    - cron: '0 * * * *'
  workflow_dispatch:

jobs:
  throttle:
    runs-on: ubuntu-latest
    outputs:
      proceed: ${{ steps.gate.outputs.proceed }}
    steps:
      - name: Skip if last successful run was under 50 minutes ago
        id: gate
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          last=$(gh run list --repo ${{ github.repository }} \
            --workflow gdelt_ingest.yml --status success --limit 1 \
            --json updatedAt --jq '.[0].updatedAt' || echo "")
          if [ -n "$last" ]; then
            last_s=$(date -d "$last" +%s); now_s=$(date +%s)
            mins=$(( (now_s - last_s) / 60 ))
            if [ "$mins" -lt 50 ]; then
              echo "Last successful run ${mins}m ago - skipping (hourly throttle)."
              echo "proceed=false" >> "$GITHUB_OUTPUT"
              exit 0
            fi
          fi
          echo "proceed=true" >> "$GITHUB_OUTPUT"

  refresh-data:
    runs-on: ubuntu-latest
    needs: throttle
    if: needs.throttle.outputs.proceed == 'true'
    timeout-minutes: 30

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

    # 2. Set up Python with caching
    - name: Set up Python
      uses: actions/setup-python@v5.6.0
      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 Dagster Pipeline (Polars-powered)
    - name: Execute Dagster Job (Polars)
      env:
        MOTHERDUCK_TOKEN: ${{ secrets.MOTHERDUCK_TOKEN }}
      run: |
        echo "🚀 Starting GDELT Ingestion (Polars + custom validation)..."
        python -m dagster job execute -f etl/pipeline_polars.py -j gdelt_ingestion_job
        echo "✅ Data Refresh Complete."

    # 5. Keep Streamlit app awake (Playwright browser automation)
    - name: Install Playwright + Chromium
      run: |
        pip install playwright
        playwright install chromium --with-deps

    - name: Keep Streamlit Alive (Playwright)
      env:
        STREAMLIT_APP_URL: ${{ vars.STREAMLIT_APP_URL || 'https://global-news-intel-platform.streamlit.app/' }}
      run: |
        python << 'EOF'
        import asyncio
        import os
        from playwright.async_api import async_playwright

        async def keep_alive():
            app_url = os.environ.get("STREAMLIT_APP_URL", "https://global-news-intel-platform.streamlit.app/")
            print("🔄 Starting Playwright browser...")
            async with async_playwright() as p:
                browser = await p.chromium.launch(headless=True)
                page = await browser.new_page()

                try:
                    # Navigate to the app
                    print(f"📡 Connecting to Streamlit app: {app_url}")
                    await page.goto(app_url,
                                    wait_until="domcontentloaded", timeout=60000)
                    
                    # Check if app is sleeping (look for wake button)
                    await asyncio.sleep(3)
                    content = await page.content()
                    
                    if "Zzzz" in content or "gone to sleep" in content:
                        print("😴 App is sleeping! Attempting to wake...")
                        # Try to click the wake button
                        try:
                            wake_btn = page.get_by_text("Yes, get this app back up!")
                            await wake_btn.click(timeout=5000)
                            print("🔘 Clicked wake button, waiting for app to start...")
                            await page.wait_for_timeout(30000)  # Wait 30s for app to wake
                            print("✅ Wake request sent!")
                        except Exception as e:
                            print(f"⚠️  Could not click wake button: {e}")
                    else:
                        # App is awake - wait a bit to establish WebSocket connection
                        print("✅ App is awake! Establishing connection...")
                        await page.wait_for_timeout(10000)  # Stay connected for 10s
                        print("✅ Keep-alive complete - WebSocket connection established")
                    
                except Exception as e:
                    print(f"⚠️  Error: {e}")
                finally:
                    await browser.close()

        asyncio.run(keep_alive())
        EOF

    - name: Notify on failure
      if: failure()
      run: |
        echo "::error::GDELT hourly ingest 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 ingest failed $(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"body\":\"The hourly GDELT ingestion job failed.\\n\\nRun: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\",\"labels\":[\"bug\"]}" || true
