name: Build and Test

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

jobs:
  test-rust:
    name: Test Rust code
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
        components: rustfmt, clippy
    
    - name: Cache cargo registry
      uses: actions/cache@v3
      with:
        path: ~/.cargo/registry
        key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
    
    - name: Cache cargo index
      uses: actions/cache@v3
      with:
        path: ~/.cargo/git
        key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
    
    - name: Cache cargo build
      uses: actions/cache@v3
      with:
        path: target
        key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
    
    - name: Check formatting
      run: cargo fmt -- --check
    
    - name: Run clippy
      run: cargo clippy -- -D warnings
    
    - name: Run tests
      run: cargo test

  test-python:
    name: Test Python on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

    steps:
    - uses: actions/checkout@v4

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

    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true

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

    - name: Create virtual environment (Unix)
      if: runner.os != 'Windows'
      run: |
        python -m venv .venv
        echo "${{ github.workspace }}/.venv/bin" >> $GITHUB_PATH
        echo "VIRTUAL_ENV=${{ github.workspace }}/.venv" >> $GITHUB_ENV

    - name: Create virtual environment (Windows)
      if: runner.os == 'Windows'
      run: |
        python -m venv .venv
        echo "${{ github.workspace }}\.venv\Scripts" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo "VIRTUAL_ENV=${{ github.workspace }}\.venv" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

    - name: Install Python dependencies (Unix)
      if: runner.os != 'Windows'
      run: |
        pip install --upgrade pip
        pip install maturin pytest numpy networkx pandas

    - name: Install Python dependencies (Windows)
      if: runner.os == 'Windows'
      run: |
        python -m pip install maturin pytest numpy networkx pandas

    - name: Build extension (Unix)
      if: runner.os != 'Windows'
      env:
        PYO3_USE_ABI3_FORWARD_COMPATIBILITY: "1"
      run: |
        source .venv/bin/activate
        maturin develop --release

    - name: Build extension (Windows)
      if: runner.os == 'Windows'
      env:
        PYO3_USE_ABI3_FORWARD_COMPATIBILITY: "1"
      run: |
        .\.venv\Scripts\Activate.ps1
        maturin develop --release

    - name: Run Python tests (Unix)
      if: runner.os != 'Windows'
      run: |
        source .venv/bin/activate
        pytest tests/ -v

    - name: Run Python tests (Windows)
      if: runner.os == 'Windows'
      run: |
        .\.venv\Scripts\Activate.ps1
        pytest tests/ -v

  benchmark:
    name: Performance benchmarks
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.11"
    
    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
    
    - name: Create virtual environment
      run: |
        python -m venv .venv
        echo "${{ github.workspace }}/.venv/bin" >> $GITHUB_PATH
        echo "VIRTUAL_ENV=${{ github.workspace }}/.venv" >> $GITHUB_ENV
    
    - name: Install dependencies
      run: |
        pip install --upgrade pip
        pip install maturin pytest
    
    - name: Build extension
      env:
        PYO3_USE_ABI3_FORWARD_COMPATIBILITY: "1"
      run: |
        source .venv/bin/activate
        maturin develop --release
    
    - name: Run benchmarks
      run: |
        source .venv/bin/activate
        python tests/performance_benchmark.py
        python tests/simple_performance_test.py
