name: macOS Swift Format

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      checkout_ref:
        description: Git ref or SHA to check out before linting
        required: false
        type: string
        default: ""
      swift_lint_targets:
        description: Newline-separated Swift file paths under macos to lint
        required: false
        type: string
        default: ""

jobs:
  swift-format-lint:
    name: Swift Format & Lint
    runs-on: macos-26
    env:
      SWIFTFORMAT_VERSION: 0.60.1
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.checkout_ref != '' && inputs.checkout_ref || github.sha }}

      - name: Setup SwiftFormat
        run: |
          source macos/DevKit/Scripts/shell-utils.sh
          ensure_swiftformat 1

      - name: Prepare Swift lint targets
        if: ${{ inputs.swift_lint_targets != '' }}
        run: |
          set -euo pipefail
          targets_file="$RUNNER_TEMP/swift-lint-targets.txt"
          cat > "$targets_file" <<'EOF'
          ${{ inputs.swift_lint_targets }}
          EOF
          echo "Swift lint targets:"
          cat "$targets_file"
          echo "SWIFT_LINT_TARGETS_FILE=$targets_file" >> "$GITHUB_ENV"

      - name: Run Swift format and lint checks
        run: macos/DevKit/Scripts/swift_format_lint.sh --strict
