name: CI

on:
  push:
    branches: [main, develop]
    paths-ignore:
      - 'gwm-ts/**'
      - '*.md'
  pull_request:
    branches: [main, develop]
    paths-ignore:
      - 'gwm-ts/**'
      - '*.md'

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Check formatting
        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
      - name: Run Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: [fmt, clippy]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run tests
        run: cargo test --verbose

  build:
    name: Build
    runs-on: ${{ matrix.os }}
    needs: [test]
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: gwm
          - os: macos-15-intel
            target: x86_64-apple-darwin
            artifact_name: gwm
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: gwm
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: gwm.exe

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - name: Build release
        run: cargo build --release --target ${{ matrix.target }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: gwm-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
          retention-days: 7

  msrv:
    name: MSRV Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@1.80.0
      - uses: Swatinem/rust-cache@v2
      - name: Check MSRV
        run: cargo check --all-features
