name: CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - name: Use Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 24
        cache: 'npm'

    - name: Install dependencies
      run: npm install

    - name: Run linter
      run: npm run lint

  test:
    needs: [lint]
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        node-version: [22, 24, 25]

    steps:
    - uses: actions/checkout@v4

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm run test:coverage

    - name: Upload test coverage data
      if: github.repository == 'bkimminich/mtg-playerinfo' && github.event_name == 'push' && matrix.os == 'ubuntu-latest' && matrix.node-version == '24'
      uses: actions/upload-artifact@v6
      with:
        name: test-lcov
        path: |
          coverage/lcov.info

  coverage-report:
    needs: [test]
    runs-on: ubuntu-latest
    if: github.repository == 'bkimminich/mtg-playerinfo' && github.event_name == 'push'
    steps:
      - name: "Check out Git repository"
        uses: actions/checkout@v4
      - name: "Download test coverage data"
        uses: actions/download-artifact@v7
        with:
          name: test-lcov
      - name: "Publish coverage to Coveralls"
        uses: coverallsapp/github-action@v2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          files: lcov.info
