name: 'Build & Test (Linux)'

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        python-version: ['3.10', '3.12']
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Configure and build project
      run: |
        sudo apt update -y
        sudo bash -x dependencies.sh -y
        mkdir build
        cd build
        cmake .. -DUSE_HTTP=ON
        sudo make install -j
      shell: bash
      
    - name: Start Metadata Server
      run: |
        cd mooncake-transfer-engine/example/http-metadata-server-python
        pip install aiohttp
        python ./bootstrap_server.py &
      shell: bash
      
    - name: Start Mooncake Master
      run: |
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
        mooncake_master &
      shell: bash
      
    - name: Test (in build env)
      run: |
        cd build
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
        ldconfig -v || echo "always continue"
        MC_METADATA_SERVER=http://127.0.0.1:8080/metadata make test -j ARGS="-V"
      shell: bash
      
    - name: Stop Mooncake Master Service
      run: |
        pkill mooncake_master || true
      shell: bash
    
    - name: Build Python wheel
      run: |
        # Build wheel with specific Python version
        PYTHON_VERSION=${{ matrix.python-version }} OUTPUT_DIR=dist-py${{ matrix.python-version == '3.10' && '310' || '312' }} ./scripts/build_wheel.sh
      shell: bash

    - name: Upload Python wheel artifact
      uses: actions/upload-artifact@v4
      with:
        name: mooncake-wheel-ubuntu-py${{ matrix.python-version == '3.10' && '310' || '312' }}
        path: mooncake-wheel/dist-py${{ matrix.python-version == '3.10' && '310' || '312' }}/*.whl

  test-wheel-ubuntu:
    needs: build
    strategy:
      matrix:
        ubuntu-version: [ubuntu-22.04, ubuntu-24.04]
        python-version: ["3.10", "3.12"]
    runs-on: ${{ matrix.ubuntu-version }}
    steps:
    - uses: actions/checkout@v4 

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

    - name: Download wheel artifact
      uses: actions/download-artifact@v4
      with:
        name: mooncake-wheel-ubuntu-py${{ matrix.python-version == '3.10' && '310' || '312' }}
        path: mooncake-wheel/dist
        
    - name: Verify wheel file exists
      run: |
        ls -la mooncake-wheel/dist/
        if [ ! -f mooncake-wheel/dist/*.whl ]; then
          echo "ERROR: No wheel file found in mooncake-wheel/dist/"
          exit 1
        fi
      shell: bash

    - name: Run installation test script
      run: |
        bash scripts/test_installation.sh
      shell: bash
    
    - name: Start metadata server
      run: |
        cd mooncake-transfer-engine/example/http-metadata-server-python
        pip install aiohttp
        python ./bootstrap_server.py &
      shell: bash

    - name: Run tests
      run: |
        source test_env/bin/activate
        ./scripts/run_tests.sh
        deactivate
      shell: bash
  
  build-flags:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        python-version: ['3.10', '3.12']
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    
    - name: Install dependencies
      run: |
        sudo apt update -y
        sudo bash -x dependencies.sh -y
      shell: bash

    - name: Build transfer engine only
      run: |
        cd mooncake-transfer-engine
        mkdir build
        cd build
        cmake .. -DUSE_ETCD=OFF -DUSE_REDIS=ON -DUSE_HTTP=ON -DWITH_METRICS=ON -DBUILD_UNIT_TESTS=ON -DBUILD_EXAMPLES=ON
        sudo make install -j
      shell: bash

    - name: Configure and build project with all settings are ON
      run: |
        mkdir build
        cd build
        cmake .. -DUSE_ETCD=ON -DUSE_REDIS=ON -DUSE_HTTP=ON -DWITH_STORE=ON -DWITH_P2P_STORE=ON -DWITH_METRICS=ON -DBUILD_UNIT_TESTS=ON -DBUILD_EXAMPLES=ON
        sudo make install -j
      shell: bash
      # TODO: lack USE_CUDA and USE_NVMEOF

    - name: Configure and build project with unit tests and examples
      run: |
        cd build
        cmake .. -DBUILD_UNIT_TESTS=ON -DBUILD_EXAMPLES=ON
        sudo make install -j
      shell: bash
      # TODO: lack WITH_RUST_EXAMPLE
    
    - name: Build Python wheel
      run: |
        # Build wheel with specific Python version
        PYTHON_VERSION=${{ matrix.python-version }} OUTPUT_DIR=dist-py${{ matrix.python-version == '3.10' && '310' || '312' }} ./scripts/build_wheel.sh
      shell: bash

  spell-check:
    name: Spell Check with Typos
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout Actions Repository
      uses: actions/checkout@v4
    - name: Spell Check Repo
      uses: crate-ci/typos@v1.30.2
