name: Hourly GDELT Refresh (Polars)

# Scheduling: GitHub cron, hourly. The external cron-job.org trigger was
# deleted, so no throttle logic is needed anymore. (A previous throttle job
# counted skipped runs as successes and starved ingestion entirely - keep
# any future rate limiting based on the refresh-data JOB conclusion, never
# the run conclusion.) MotherDuck Lite has 10 CU-hours/month; hourly is the
# budget-safe cadence.
on:
  schedule:
    - cron: '0 * * * *'
  workflow_dispatch:

jobs:
  refresh-data:
    runs-on: ubuntu-latest
    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
