name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  go:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
          cache: true

      - name: Check formatting
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Files not formatted:"
            gofmt -l .
            exit 1
          fi

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

      - name: Test
        run: go test ./... -v -race

      - name: Build
        run: CGO_ENABLED=0 go build -o /dev/null ./cmd/hatch

  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t hatch:ci .

      - name: Smoke test the Docker image
        run: |
          docker run --rm -d -p 8080:8080 --name hatch-ci hatch:ci
          sleep 2
          curl -fsS http://localhost:8080/healthz | grep -q 'ok'
          docker stop hatch-ci

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

      - uses: actions/setup-go@v5
        with:
          go-version: '1.25'
          cache: true

      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@v6
        with:
          version: latest

  lint-docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check markdown formatting
        uses: DavidAnson/markdownlint-cli2-action@v16
        with:
          globs: '**/*.md'
        continue-on-error: true

  check-paperclip:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Need full history for PR range
      - name: Check for Paperclip references in commit messages
        run: ./scripts/check-paperclip.sh
        env:
          GITHUB_EVENT_NAME: ${{ github.event_name }}
          GITHUB_BASE_REF: ${{ github.base_ref }}
