name: Update Devices JSON

on:
  schedule:
    # Run everyday (At 00:00 UTC)
    - cron: "0 0 * * *"
  workflow_dispatch: # allow manual run from Actions tab

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: pip install requests

      - name: Run update script
        run: python update_devices.py

      - name: Commit and push changes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          # Fetch latest changes from remote
          git fetch origin main

          # Add and commit new devices.json
          git add devices.json
          git commit -m "Auto update devices.json" || echo "No changes to commit"

          # Force push to remote
          git push --force https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:main
