name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Run tests
        run: |
          # Get packages with test files, excluding packages without tests to avoid covdata errors
          PACKAGES=$(go list ./internal/... | grep -v '/testing$')
          go test $PACKAGES -coverprofile=coverage.out

      - name: Build
        run: go build -o bin/mh .

  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Check formatting
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Code is not formatted. Run 'go fmt ./...'"
            gofmt -d .
            exit 1
          fi

      - name: Vet
        run: go vet ./...
