name: CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  # ─── Linux (primary) ────────────────────────────────────────────────────────
  linux:
    name: Build & Test (Linux)
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install Protoc
      run: sudo apt-get update && sudo apt-get install -y protobuf-compiler

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

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

    - name: Clippy
      run: cargo clippy --all-targets -- -D warnings

    - name: Build
      run: cargo build --verbose

    - name: Test
      run: cargo test --verbose

  # ─── macOS (Apple Silicon / Intel) ───────────────────────────────────────────
  # Catches macOS-specific regressions: mimalloc/ring segfault (v0.9.7),
  # Mach thread_policy_set pinning, SO_REUSEPORT behaviour differences.
  macos:
    name: Build & Test (macOS)
    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install Protoc
      run: brew install protobuf

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

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

    - name: Clippy
      run: cargo clippy --all-targets -- -D warnings

    - name: Build
      run: cargo build --verbose

    - name: Test
      run: cargo test --verbose

  # ─── Windows (MSVC) ─────────────────────────────────────────────────────────
  # mimalloc's global-allocator override conflicts with the MSVC CRT when the
  # crate is linked as a DLL.  SO_REUSEPORT (socket2) is Linux/macOS only.
  # We compile the library crate only and set GRPC_GW_WINDOWS_CI=1 so any
  # future platform-conditional code can detect the CI context.
  windows:
    name: Build & Test (Windows / MSVC)
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install Protoc
      run: choco install protoc --yes
      shell: powershell

    - name: Install Rust (stable MSVC toolchain)
      uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: stable-x86_64-pc-windows-msvc
        components: rustfmt, clippy

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

    - name: Build (lib only)
      run: cargo build --lib --verbose
      env:
        GRPC_GW_WINDOWS_CI: "1"

    - name: Test (lib only)
      run: cargo test --lib --verbose
      env:
        GRPC_GW_WINDOWS_CI: "1"
