name: Scrape GitHub Data

on:
  schedule:
    - cron: '0 0 * * *' # Every day at midnight UTC
  workflow_dispatch: # Allow manual trigger

jobs:
  scrape:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          if [ -f scraper/requirements.txt ]; then
            pip install -r scraper/requirements.txt
          fi

      - name: Run scraper
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: python scraper/scraper.py

      - name: Commit and push changes
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email 'github-actions[bot]@users.noreply.github.com'
          git add data/tools.json
          git diff --quiet && git diff --staged --quiet || (git commit -m 'chore: nightly data enrichment [skip ci]' && git push)
