name: Update dump.csv

on:
  push:
    branches:
      - main
  schedule:
    - cron: '0 * * * *'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  update-data:

    permissions:
      contents: write
      id-token: write  # Required for OIDC

    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v6

    - name: Setup Node.js
      uses: actions/setup-node@v6
      with:
        node-version: '24'
        registry-url: 'https://registry.npmjs.org'

    - name: Install devDependencies
      run: npm i

    - name: Update
      run: npm start

    - name: Test
      run: npm test

    - name: Commit and push changes
      id: commit
      run: |
        git config --global user.name "github-actions[bot]"
        git config --global user.email "github-actions[bot]@users.noreply.github.com"
        git add dump_pairs.bin
        git add dump_tokens.bin
        git add dump_p2tt.bin
        if ! git diff --cached --quiet; then
          npm version patch --force -m "chore: update dump_*.bin to %s [skip ci]"
          git push --follow-tags
          echo "can_publish=true" >> $GITHUB_OUTPUT
        else
          echo "No changes"
        fi

    - name: Publish to NPM
      if: steps.commit.outputs.can_publish == 'true'
      run: npm publish
