name: CI

on:
  push:
    branches:
      - main
      - master
  pull_request:

jobs:
  rust-test:
    name: Rust Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable

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

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

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

      - name: Run cargo check
        run: cargo check --all --verbose

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

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

      - name: Run cargo fmt check
        run: cargo fmt --all -- --check

  python-test:
    name: Python Tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.8', '3.11', '3.13']

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install maturin
        run: pip install maturin pytest

      - name: Build Python package
        working-directory: rbufrp
        run: maturin develop --release

      - name: Test Python package
        working-directory: rbufrp
        run: |
          python -c "import rbufrp; print(rbufrp.__version__)"
          python -c "import rbufrp; print(rbufrp.get_tables_path())"

  check-formatting:
    name: Check Formatting
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install ruff
        run: pip install ruff

      - name: Check Python formatting with ruff
        run: |
          ruff check rbufrp/src/rbufrp/
          ruff format --check rbufrp/src/rbufrp/
