name: Shell Script Quality Checks

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: write

jobs:
  shell-quality-checks:
    runs-on: macos-latest

    steps:
      - name: Checkout source code
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.24.6"
          cache: true

      - name: Cache Homebrew packages
        uses: actions/cache@v4
        with:
          path: |
            ~/Library/Caches/Homebrew
            /usr/local/Cellar/bats-core
            /usr/local/Cellar/shfmt
            /usr/local/Cellar/shellcheck
          key: ${{ runner.os }}-brew-v1
          restore-keys: |
            ${{ runner.os }}-brew-

      - name: Install shell linting and testing tools
        run: brew install bats-core shfmt shellcheck

      - name: Auto-format shell scripts with shfmt
        run: ./scripts/format.sh

      - name: Run shellcheck linter and bats tests
        run: |
          ./scripts/check.sh
          echo "✓ All quality checks passed"

      - name: Run all bats tests with summary
        run: |
          echo "Running all test suites..."
          bats tests/*.bats --formatter tap
          echo ""
          echo "Test summary:"
          echo "  Total test files: $(ls tests/*.bats | wc -l | tr -d ' ')"
          echo "  Total tests: $(grep -c "^@test" tests/*.bats | awk -F: '{sum+=$2} END {print sum}')"
