name: CI

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

env:
  CARGO_TERM_COLOR: always
  PROTOC_VERSION: "31.1"  # or "32.0-rc-2" etc.

jobs:
  test:
    name: Test Suite
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      # Install AMD GPU development libraries
      - name: Install AMD GPU dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libdrm-dev libdrm-amdgpu1

      # protoc: cached install via composite action
      - name: Setup protoc
        uses: ./.github/actions/setup-protoc
        with:
          version: ${{ env.PROTOC_VERSION }}
          platform: linux-x86_64

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

      - name: Run tests
        run: cargo test --verbose

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

      - name: Run clippy
        run: cargo clippy -- -D warnings

  build-check:
    name: Build Check
    runs-on: ubuntu-latest
    needs: test
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      # Install AMD GPU development libraries
      - name: Install AMD GPU dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libdrm-dev libdrm-amdgpu1

      # protoc: cached install via composite action
      - name: Setup protoc
        uses: ./.github/actions/setup-protoc
        with:
          version: ${{ env.PROTOC_VERSION }}
          platform: linux-x86_64

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

      - name: Build binary
        run: cargo build --release --bin all-smi

      - name: Build library-only (no default features)
        run: cargo build --no-default-features --lib

  docker-check:
    name: Docker Build Check
    runs-on: ubuntu-latest
    needs: test
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Build Docker image (no push)
        uses: docker/build-push-action@v7
        with:
          context: .
          platforms: linux/amd64
          push: false
          cache-from: type=gha
          cache-to: type=gha,mode=max