name: CI

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

permissions:
  contents: read

jobs:
  # ── Cross-Platform Build & Test ──────────────────────────────
  test:
    name: Test (${{ matrix.os }} / Go ${{ matrix.go-version }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        go-version: ["1.25"]

    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}

      - name: Download dependencies
        run: go mod download

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

      - name: Test (unit + integration, default tags)
        timeout-minutes: 10
        run: go test -v -race -timeout 8m -coverprofile=coverage.out $(go list ./... | grep -v /tests/benchmarks)
        shell: bash

      - name: Test (unit, treesitter tag)
        # Runs the indexer and kuzu packages that require CGo + the
        # `treesitter` build tag. The default-tag run above silently
        # skips these packages because every .go file inside them is
        # build-tag-gated. We need this second pass to actually cover
        # the AST/query/heuristics code paths.
        if: matrix.os != 'windows-latest'
        timeout-minutes: 10
        run: CGO_ENABLED=1 go test -tags treesitter -race -timeout 8m -coverprofile=coverage-ts.out ./internal/graph/indexer/... ./internal/graph/kuzu/...
        shell: bash

      - name: Test (unit, treesitter tag — modules/chunking)
        # modules/chunking is a separate Go module with its own go.mod.
        # We test it from inside its own directory.
        if: matrix.os != 'windows-latest'
        timeout-minutes: 10
        run: |
          cd modules/chunking
          CGO_ENABLED=1 go test -tags treesitter -race -timeout 8m -coverprofile=coverage-chunking.out ./...
        shell: bash

      - name: Upload coverage to Codecov
        if: matrix.os == 'ubuntu-latest'
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage.out,./coverage-ts.out,./modules/chunking/coverage-chunking.out
          fail_ci_if_error: false
          verbose: true

  # ── Build Verification (all platforms, both tags) ────────────
  build-check:
    name: Build (${{ matrix.os }} / ${{ matrix.tags }})
    runs-on: ${{ matrix.os }}
    timeout-minutes: 10
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        tags: ["", "treesitter", "native"]
        exclude:
          # treesitter (CGo) requires C toolchain — skip on Windows for now
          - os: windows-latest
            tags: treesitter
          - os: windows-latest
            tags: native

    steps:
      - uses: actions/checkout@v4

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

      - name: Set up Rust (for native tag)
        if: matrix.tags == 'native'
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Build Rust Core (for native tag)
        if: matrix.tags == 'native'
        run: cd ext/gleann-core-rs && cargo build --release

      - name: Build
        run: |
          if [ -n "${{ matrix.tags }}" ]; then
            # Add RPATH for Linux to find the shared library during build-time checks if needed
            # (though -o /dev/null might skip some runtime checks, ld still needs the lib)
            export CGO_LDFLAGS="-L$(pwd)/ext/gleann-core-rs/target/release"
            go build -tags "${{ matrix.tags }}" -o /dev/null ./cmd/gleann/
          else
            go build -o /dev/null ./cmd/gleann/
          fi
        shell: bash

  # ── Documentation Drift Check ────────────────────────────────
  doc-drift:
    name: Doc Drift
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run doc-drift detector
        run: bash scripts/doc-drift.sh
