name: Build

on:
  push:
    branches: [main]
    paths-ignore:
      - 'docs/**'
      - 'README.md'
      - 'LICENSE'
      - '*.md'
  pull_request:
    paths-ignore:
      - 'docs/**'
      - '*.md'

concurrency:
  group: build-${{ github.ref }}
  cancel-in-progress: true

# Node.js is installed at the start of every job so actions/cache@v4
# (which uses Node) can run. Installing Node costs ~10s; the resulting
# Go module + apt cache restore typically saves 30-60s on warm runs.

jobs:
  test:
    runs-on: go
    steps:
      - name: Disable apt auto-clean (preserve cache for actions/cache)
        run: |
          rm -f /etc/apt/apt.conf.d/docker-clean
          echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache

      - name: Install Node (required by actions/cache)
        run: |
          apt-get update >/dev/null
          apt-get install -y --no-install-recommends nodejs >/dev/null

      - name: Checkout
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git clone --no-checkout "https://x-access-token:${GITHUB_TOKEN}@git.okcu.io/${GITHUB_REPOSITORY}.git" /tmp/src
          git -C /tmp/src checkout "$GITHUB_SHA"
          cp -a /tmp/src/. .

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
          key: go-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
          restore-keys: go-${{ runner.os }}-

      - name: Vet
        run: go vet ./...
      - name: Test with race
        run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
      - name: Linux smoke build
        run: |
          mkdir -p bin
          go build -trimpath -ldflags="-s -w" -o bin/drover ./cmd/drover
          ./bin/drover --version

  build-windows:
    runs-on: go
    needs: test
    steps:
      - name: Disable apt auto-clean (preserve cache for actions/cache)
        run: |
          rm -f /etc/apt/apt.conf.d/docker-clean
          echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache

      - name: Install Node (required by actions/cache)
        run: |
          apt-get update >/dev/null
          apt-get install -y --no-install-recommends nodejs >/dev/null

      - name: Checkout
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git clone --no-checkout "https://x-access-token:${GITHUB_TOKEN}@git.okcu.io/${GITHUB_REPOSITORY}.git" /tmp/src
          git -C /tmp/src checkout "$GITHUB_SHA"
          cp -a /tmp/src/. .

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
          key: go-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
          restore-keys: go-${{ runner.os }}-

      - name: Cross-compile drover.exe (windows/amd64)
        env:
          GOOS: windows
          GOARCH: amd64
          CGO_ENABLED: '0'
        run: |
          SHORT_SHA="${GITHUB_SHA:0:7}"
          BUILD_DATE="$(date -u +%Y-%m-%d)"
          mkdir -p bin
          # -H=windowsgui = subsystem WINDOWS, double-click no longer
          # flashes a console window. main.go calls AttachConsole on
          # startup so CLI invocations from cmd/PowerShell still print
          # to the parent terminal.
          # -tags desktop,production is REQUIRED by Wails (see release.yml).
          go build -trimpath -tags "desktop,production" \
            -ldflags="-s -w -H=windowsgui \
            -X main.Version=dev-${SHORT_SHA} \
            -X main.Commit=${SHORT_SHA} \
            -X main.BuildDate=${BUILD_DATE}" \
            -o bin/drover.exe ./cmd/drover
          ls -la bin/
          sha256sum bin/drover.exe
