name: CI

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

env:
  CARGO_TERM_COLOR: always
  # All features EXCEPT napi (requires Node.js N-API at link time) and wasm (requires wasm target)
  SDK_FEATURES: "full,json-schema,test-utils,keypair-serialization,websocket"

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo check --features $SDK_FEATURES
        env:
          RUSTFLAGS: "-Dwarnings"

  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@nightly
        with:
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --features $SDK_FEATURES -- -D warnings
        env:
          RUSTFLAGS: "-Dwarnings"

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --features $SDK_FEATURES
        env:
          RUSTFLAGS: "-Dwarnings"

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@cargo-llvm-cov
      - name: Check coverage threshold (80%)
        run: |
          coverage=$(cargo llvm-cov --features $SDK_FEATURES --summary-only 2>/dev/null | grep 'TOTAL' | awk '{print $10}' | tr -d '%')
          echo "Line coverage: $coverage%"
          if [ -z "$coverage" ]; then
            echo "WARNING: Could not parse coverage, skipping threshold check"
            exit 0
          fi
          if (( $(echo "$coverage < 80" | bc -l) )); then
            echo "ERROR: Coverage $coverage% is below 80% threshold"
            exit 1
          fi
          echo "Coverage check passed: $coverage% >= 80%"

  examples:
    name: Examples
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --examples --features $SDK_FEATURES
        env:
          RUSTFLAGS: "-Dwarnings"

  docs:
    name: Docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo doc --features $SDK_FEATURES --no-deps
        env:
          RUSTDOCFLAGS: "-Dwarnings"

  wasm:
    name: WASM Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --target wasm32-unknown-unknown --features wasm --no-default-features

  wasm-test:
    name: WASM Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown
      - uses: Swatinem/rust-cache@v2
      - uses: browser-actions/setup-chrome@v1
      - run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
      - run: wasm-pack test --headless --chrome -- --features wasm --no-default-features

  license:
    name: License Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo install cargo-deny
      - name: Check licenses with cargo-deny
        run: cargo deny check licenses

  security:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo install cargo-audit cargo-deny
      - run: cargo audit
      - run: cargo deny check

  file-size:
    name: File Size Check (1500 lines)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check file sizes
        run: |
          find src -name '*.rs' | while read file; do
            lines=$(wc -l < "$file")
            if [ "$lines" -gt 1500 ]; then
              echo "ERROR: $file has $lines lines (max 1500)"
              exit 1
            fi
          done

  # Cross-platform builds (including napi native addon)
  # NOTE: No RUSTFLAGS env here — .cargo/config.toml provides per-target linker flags
  # needed for napi cdylib (unresolved N-API symbols resolved at runtime by Node.js)
  build-matrix:
    name: Build (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Build library (all non-platform features)
        run: cargo build --release --features ${{ env.SDK_FEATURES }}
      - name: Build napi native addon
        run: cargo build --release --features napi
