name: Build

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

jobs:
  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v6

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

      - name: Install system deps (Ubuntu)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libnetcdf-dev libhdf5-dev pkg-config

      - name: Install system deps (macOS)
        if: matrix.os == 'macos-latest'
        run: |
          brew install netcdf

      - name: Install system deps (Windows)
        if: matrix.os == 'windows-latest'
        uses: conda-incubator/setup-miniconda@v4
        with:
          auto-update-conda: true
          activate-environment: ci
          python-version: "3.11"
          channels: conda-forge
          channel-priority: strict

      - name: Install and configure netcdf-rs build (Windows)
        if: matrix.os == 'windows-latest'
        shell: bash -el {0}
        run: ./.github/scripts/configure-netcdf-env.sh

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

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

      - name: Set Cargo features
        id: cargo_features
        shell: bash
        run: |
          # `tetration-metal` is macOS-only (see Cargo.toml). Do not use `--all-features` on Linux/Windows.
          # `tetration-gpu` and `tetration-rocm` are mutually exclusive — enable at most one driver backend.
          FEAT="tetration-netcdf,tetration-hdf5,tetration-gpu"
          if [ "${{ runner.os }}" = "macOS" ]; then
            FEAT="${FEAT},tetration-metal"
          fi
          echo "value=${FEAT}" >> "${GITHUB_OUTPUT}"

      - name: Run clippy
        run: cargo clippy --features ${{ steps.cargo_features.outputs.value }} -- -D warnings

      - name: Run tests
        run: cargo test --features ${{ steps.cargo_features.outputs.value }} --verbose

  msrv:
    name: MSRV Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust (MSRV)
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: 1.95

      - name: Install NetCDF (Ubuntu)
        run: |
          sudo apt-get update
          sudo apt-get install -y libnetcdf-dev libhdf5-dev pkg-config

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

      - name: Check MSRV
        run: cargo check --all-targets

  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v6

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

      - name: Install system deps (Ubuntu)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libnetcdf-dev libhdf5-dev pkg-config

      - name: Install system deps (macOS)
        if: matrix.os == 'macos-latest'
        run: |
          brew install netcdf

      - name: Install system deps (Windows)
        if: matrix.os == 'windows-latest'
        uses: conda-incubator/setup-miniconda@v4
        with:
          auto-update-conda: true
          activate-environment: ci
          python-version: "3.11"
          channels: conda-forge
          channel-priority: strict

      - name: Install and configure netcdf-rs build (Windows)
        if: matrix.os == 'windows-latest'
        shell: bash -el {0}
        run: ./.github/scripts/configure-netcdf-env.sh

      - name: Build
        run: cargo build --release

      - name: Upload artifacts
        uses: actions/upload-artifact@v7
        with:
          name: tet-${{ matrix.os }}
          path: target/release/tet*
