name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-22.04
    permissions:
      contents: write
    strategy:
      matrix:
        python-version: ['3.10', '3.12']
    steps:
      - name: Checkout source
        uses: actions/checkout@v4

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

      - name: Install system dependencies 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: Build Python wheel
        run: |
          # Set LD_LIBRARY_PATH for wheel building
          export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
          PYTHON_VERSION=${{ matrix.python-version }} OUTPUT_DIR=dist-py${{ matrix.python-version == '3.10' && '310' || '312' }} ./scripts/build_wheel.sh
        env:
          VERSION: ${{ env.VERSION }}

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

  publish-release:
    needs: build
    runs-on: ubuntu-22.04
    permissions:
      contents: write
    steps:
      - name: Checkout source
        uses: actions/checkout@v4

      - name: Download all wheel artifacts
        uses: actions/download-artifact@v4
        with:
          path: mooncake-wheel/dist-all

      - name: Prepare wheels for release
        run: |
          # Move all wheels to a single directory
          mkdir -p mooncake-wheel/dist-release
          find mooncake-wheel/dist-all -name "*.whl" -exec cp {} mooncake-wheel/dist-release/ \;
          ls -la mooncake-wheel/dist-release/
          # List all collected wheels
          echo "Collected wheels for release:"
          ls -la mooncake-wheel/dist-release/

      - name: Upload wheels to GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: mooncake-wheel/dist-release/*.whl

      - name: Publish package to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: mooncake-wheel/dist-release/
          password: ${{ secrets.PYPI_API_TOKEN }}
