name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  build-and-test:
    runs-on: macos-latest
    defaults:
      run:
        shell: bash
        working-directory: swabble
    steps:
      - name: Checkout swabble
        uses: actions/checkout@v4
        with:
          path: swabble

      - name: Select Xcode 26.1 (prefer 26.1.1)
        run: |
          set -euo pipefail
          # pick the newest installed 26.1.x, fallback to newest 26.x
          CANDIDATE="$(ls -d /Applications/Xcode_26.1*.app 2>/dev/null | sort -V | tail -1 || true)"
          if [[ -z "$CANDIDATE" ]]; then
            CANDIDATE="$(ls -d /Applications/Xcode_26*.app 2>/dev/null | sort -V | tail -1 || true)"
          fi
          if [[ -z "$CANDIDATE" ]]; then
            echo "No Xcode 26.x found on runner" >&2
            exit 1
          fi
          echo "Selecting $CANDIDATE"
          sudo xcode-select -s "$CANDIDATE"
          xcodebuild -version

      - name: Show Swift version
        run: swift --version

      - name: Install tooling
        run: |
          brew update
          brew install swiftlint swiftformat

      - name: Format check
        run: |
          ./scripts/format.sh
          git diff --exit-code

      - name: Lint
        run: ./scripts/lint.sh

      - name: Test
        run: swift test --parallel
