name: Cross-platform CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  # Job 1: Linux, Windows, macOS (x86_64 and ARM64)
  test-mainstream:
    name: ${{ matrix.os }} (${{ matrix.arch }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            arch: x86_64
          - os: windows-latest
            arch: x86_64
          - os: macos-15-intel # Intel x86_64
            arch: x86_64
          - os: macos-latest # Apple Silicon ARM64
            arch: arm64

    steps:
      - uses: actions/checkout@v5

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

      - name: Build
        run: cargo build --verbose

      - name: Run Tests
        run: cargo test --verbose -- --nocapture

      - name: Build FFI (release)
        run: cargo build --features ffi --release
        env:
          RUSTFLAGS: "-C panic=abort"

      - name: Setup MSVC
        if: matrix.os == 'windows-latest'
        uses: ilammy/msvc-dev-cmd@v1

      - name: Build C example (unix)
        if: matrix.os != 'windows-latest'
        run: cc -I./include examples/ffi.c -L./target/release -lbtree_store -o ffi

      - name: Run C example (linux)
        if: matrix.os == 'ubuntu-latest'
        run: LD_LIBRARY_PATH=./target/release ./ffi

      - name: Run C example (macos)
        if: startsWith(matrix.os, 'macos')
        run: DYLD_LIBRARY_PATH=./target/release ./ffi

      - name: Build C example (windows)
        if: matrix.os == 'windows-latest'
        shell: cmd
        run: |
          set BTREE_LIB=btree_store.lib
          if exist target\release\btree_store.dll.lib set BTREE_LIB=btree_store.dll.lib
          cl /I include examples\ffi.c /Fe:ffi.exe /link /LIBPATH:target\release %BTREE_LIB%

      - name: Run C example (windows)
        if: matrix.os == 'windows-latest'
        shell: cmd
        run: |
          if exist target\release\btree_store.dll copy target\release\btree_store.dll .
          ffi.exe

  # Job 2: FreeBSD (Runs in a VM on top of Linux)
  test-freebsd:
    name: FreeBSD (x86_64)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Run tests in FreeBSD VM
        uses: vmactions/freebsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg install -y rust
          run: |
            cargo test --verbose -- --nocapture
            RUSTFLAGS="-C panic=abort" cargo build --features ffi --release
            cc -I./include examples/ffi.c -L./target/release -lbtree_store -o ffi
            LD_LIBRARY_PATH=./target/release ./ffi

  # Job 3: Linux ARM64 (Cross-compile check)
  # This verifies that the code compiles for Linux on ARM.
  # We use 'cross' to simplify cross-compilation.
  test-linux-arm64:
    name: Linux (ARM64 cross-compile)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-unknown-linux-gnu

      - name: Install cross
        run: cargo install cross

      - name: Build for ARM64
        run: cross build --target aarch64-unknown-linux-gnu --features ffi --verbose

      # Note: Running tests for ARM64 on x86_64 requires QEMU via cross.
      # If you want to actually run the tests:
      - name: Run tests for ARM64
        run: cross test --target aarch64-unknown-linux-gnu --features ffi --verbose -- --nocapture
