# Aktualisiert täglich .github/badges/data.json (Download-Lifetime mit Asset-Swap-Erkennung, OmniNX-Version, FW-Tag).
# Läuft auf Gitea Actions wie die übrigen Workflows (.github/workflows); github.api_url = Gitea-API.

name: Update badge-data

on:
  schedule:
    - cron: "0 5 * * *"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  refresh:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Regenerate badge data
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_API_URL: ${{ github.api_url }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          FIRMWARE_REPOSITORY: OmniNX/NX_Firmware
          # Optional: PAT with read access if NX_Firmware is private (job token often 404s on other repos).
          FIRMWARE_API_TOKEN: ${{ secrets.FIRMWARE_API_TOKEN }}
        run: python3 scripts/update-badge-data.py

      - name: Commit and push if changed
        run: |
          set -euo pipefail
          git config user.name "OmniNX CI"
          git config user.email "omninx-ci@users.noreply.git.niklascfw.de"
          git add .github/badges/data.json
          if git diff --staged --quiet; then
            echo ".github/badges/data.json unverändert."
            exit 0
          fi
          git commit -m "chore: refresh badge data [skip ci]"
          git push
