name: Rust CI (Fmt, Clippy, Tests)

on:
  push:
    branches: [ main, dev, dev-staging ]
  pull_request:
    branches: [ main, dev, dev-staging ]

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        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
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - name: Run Clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Fetch HBA files
        shell: bash
        run: |
          if [ -n "${{ vars.HBA_MICRO_LATEST_URL }}" ]; then
            HBA_MICRO_LATEST_URL="${{ vars.HBA_MICRO_LATEST_URL }}" \
              bash .github/scripts/fetch_micro_hba.sh
          else
            echo "HBA_MICRO_LATEST_URL not set, skipping HBA fetch"
          fi
        continue-on-error: true
      - name: Run Tests
        run: cargo test --workspace --all-targets --all-features
