name: Update Airline Data

on:
  schedule:
    - cron: '0 6 * * 1'  # every Monday at 6am UTC
  workflow_dispatch:

jobs:
  update:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'
      - run: npm ci
      - name: Fetch latest airlines.dat from OpenFlights
        run: wget -q https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat
      - name: Apply local corrections to airlines.dat
        run: node normalize.js
      - name: Regenerate airlines.json
        run: node convert.js
      - name: Commit and push if data changed
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          if git diff --quiet airlines.dat airlines.json; then
            echo "No data changes, skipping."
          else
            npm version patch --no-git-tag-version
            git add airlines.dat airlines.json package.json package-lock.json
            git commit -m "chore: update airline data from OpenFlights"
            git push origin master
          fi
