name: Lint & Format

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

env:
  CARGO_TERM_COLOR: always

jobs:
  lint:
    name: Lint & Format
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev build-essential protobuf-compiler

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

      # Note: Using specific features instead of --all-features to avoid sp1-sdk
      # build issues (linker OOM on Linux, C++20 on Windows).
      - name: Run clippy (strict)
        run: cargo clippy --all-features -- -D warnings -D clippy::unwrap_used -D clippy::expect_used

      - name: Check for backup files
        run: |
          if git ls-files | grep -E '(\.bak\d*|\.backup)$'; then
            echo "Found backup files (.bak/.backup). Please remove them." >&2
            exit 1
          fi
