name: CI

on:
  push:
    branches: [main]
    tags: ["v*"]
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-D warnings"

jobs:
  ci:
    name: Test / Lint / Format
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-

      - name: Check formatting
        run: cargo fmt --check

      - name: Build
        run: cargo build

      - name: Build (release)
        run: cargo build --release

      - name: Clippy
        run: cargo clippy --all-targets --all-features

      - name: Test
        run: cargo test --all-features

  coverage:
    name: Code coverage
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-coverage-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-coverage-

      - name: Check coverage thresholds
        run: cargo llvm-cov --fail-under-lines 95 --fail-under-regions 90

  wasm:
    name: WASM build (demo)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: demo/package-lock.json

      - name: Install demo dependencies
        working-directory: demo
        run: npm ci

      - name: Build demo (wasm-pack + vite)
        working-directory: demo
        run: npm run build

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: ci
    if: startsWith(github.ref, 'refs/tags/v')

    steps:
      - uses: actions/checkout@v4

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Publish
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  publish-npm:
    name: Publish to npm
    runs-on: ubuntu-latest
    needs: ci
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      id-token: write
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: "https://registry.npmjs.org"

      - name: Update npm (trusted publishing needs >= 11.5.1)
        run: npm install -g npm@latest

      - name: Build bundler target
        run: wasm-pack build --target bundler --out-dir npm/bundler --out-name navigo -- --features wasm

      - name: Build web target
        run: wasm-pack build --target web --out-dir npm/web --out-name navigo -- --features wasm

      - name: Remove wasm-pack scaffolding
        run: rm -f npm/bundler/{package.json,.gitignore,README.md,LICENSE} npm/web/{package.json,.gitignore,README.md,LICENSE}

      - name: Copy LICENSE
        run: cp LICENSE npm/LICENSE

      - name: Sync version with Cargo.toml
        working-directory: npm
        run: |
          VERSION=$(grep -m1 '^version' ../Cargo.toml | sed -E 's/version = "(.*)"/\1/')
          npm pkg set version="$VERSION"

      - name: Publish
        working-directory: npm
        run: npm publish --access public
