name: Update JSON Date
on:
  pull_request:
    types: [opened, synchronize, reopened]
        
jobs:
  list-files:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout PR Branch
      uses: actions/checkout@v4
      with:
        ref: ${{ github.head_ref }}

    - name: Update JSON
      id: changed-files
      run: |
          FILES=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files --jq '.[].filename' | tr '\n' ' ')
          echo "changed_files=${FILES}"
          for FILE in $FILES; do
            if [[ "$FILE" =~ /(.*)\.json ]]; then
              NAME="${BASH_REMATCH[1]}"
            else
              echo "no new JSON in ${FILES}"
              continue
            fi
          
            JSON_FILE="json/${NAME}.json"
            if [[ -f "$JSON_FILE" ]]; then
              echo "Updating date_created in $JSON_FILE"
              jq --arg date "$(date +%Y-%m-%d)" '.date_created = $date' "$JSON_FILE" > tmp.json && mv tmp.json "$JSON_FILE"
            else
              echo "JSON file $FILES not found"
            fi
          done
             
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git diff --exit-code || git commit -am "Updating Dates in affected JSON files."
          git push 
      env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
