name: Data File Lint & Format

on:
  push:
    paths:
      - "**/*.yml"
      - "**/*.yaml"
      - "**/*.json"
      - ".gitattributes"
      - ".github/workflows/data-files.yml"
  pull_request:
    paths:
      - "**/*.yml"
      - "**/*.yaml"
      - "**/*.json"
      - ".gitattributes"
      - ".github/workflows/data-files.yml"
  workflow_dispatch:
    inputs:
      apply_fixes:
        description: "Format YAML/JSON files and commit changes back to this branch"
        required: false
        default: false
        type: boolean

permissions:
  contents: write

jobs:
  data-files:
    name: YAML and JSON
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "24"

      - name: Install data-file tools
        run: |
          sudo apt-get update
          sudo apt-get install -y jq
          npm install --global prettier

      - name: Lint YAML
        uses: ibiqlik/action-yamllint@v3
        with:
          file_or_dir: "."
          config_data: |
            extends: default
            rules:
              document-start: disable
              truthy: disable
              line-length:
                max: 120
                level: warning
              indentation:
                spaces: 2
                indent-sequences: consistent

      - name: Find strict JSON files
        run: |
          find . -type f -name '*.json' \
            -not -path './.git/*' \
            -not -path './vscodium/.config/VSCodium/User/*' \
            -print | sort > strict-json-files.txt

      - name: Validate strict JSON
        run: |
          set -euo pipefail
          [[ -s strict-json-files.txt ]] || exit 0
          while IFS= read -r file; do
            jq -e . "$file" >/dev/null
          done < strict-json-files.txt

      - name: Check Prettier formatting
        if: ${{ github.event_name != 'workflow_dispatch' || inputs.apply_fixes != true }}
        run: |
          prettier --check --ignore-unknown --end-of-line lf --print-width 100 --tab-width 2 --use-tabs false "**/*.{yml,yaml}"
          if [[ -s strict-json-files.txt ]]; then
            prettier --check --ignore-unknown --end-of-line lf --print-width 100 --tab-width 2 --use-tabs false $(cat strict-json-files.txt)
          fi

      - name: Apply Prettier formatting
        if: ${{ github.event_name == 'workflow_dispatch' && inputs.apply_fixes == true }}
        run: |
          prettier --write --ignore-unknown --end-of-line lf --print-width 100 --tab-width 2 --use-tabs false "**/*.{yml,yaml}"
          if [[ -s strict-json-files.txt ]]; then
            prettier --write --ignore-unknown --end-of-line lf --print-width 100 --tab-width 2 --use-tabs false $(cat strict-json-files.txt)
          fi

      - name: Commit formatting fixes
        if: ${{ github.event_name == 'workflow_dispatch' && inputs.apply_fixes == true }}
        uses: stefanzweifel/git-auto-commit-action@v7
        with:
          commit_message: Apply data file formatting
