name: CI

on:
  push:
    branches: [main]
  pull_request:
  workflow_run:
    workflows: ["Auto Merge"]
    types: [completed]

permissions:
  contents: read

jobs:
  typos:
    name: Typos
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: crate-ci/typos@v1.43.5

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
      - uses: golangci/golangci-lint-action@v9

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
      - name: Download model
        run: curl -fsSL -o model.json "https://huggingface.co/datasets/happyhackingspace/dit/resolve/main/model.json"
      - run: go test -race -coverprofile=coverage.out ./...

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
      - run: go build ./...

  govulncheck:
    name: Govulncheck
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
      - run: go install golang.org/x/vuln/cmd/govulncheck@latest
      - run: govulncheck ./...

  release:
    name: Release
    needs: [typos, lint, test, build, govulncheck]
    if: >-
      (github.ref == 'refs/heads/main' && github.event_name == 'push') ||
      (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
      - name: Determine next version
        id: version
        run: |
          latest=$(git tag -l 'v*' | sort -V | tail -1)
          if [ -z "$latest" ]; then latest="v0.0.0"; fi
          major=$(echo "$latest" | sed 's/v//' | cut -d. -f1)
          minor=$(echo "$latest" | sed 's/v//' | cut -d. -f2)
          patch=$(echo "$latest" | sed 's/v//' | cut -d. -f3)
          next="v${major}.${minor}.$((patch + 1))"
          echo "tag=$next" >> "$GITHUB_OUTPUT"
          echo "Previous: $latest -> Next: $next"
      - name: Create tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag ${{ steps.version.outputs.tag }}
          git push origin ${{ steps.version.outputs.tag }}
      - uses: goreleaser/goreleaser-action@v7
        with:
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
