name: Build package

permissions:
  contents: read
  actions: write

on:
  workflow_call:
    inputs:
      package:
        required: true
        type: string
      artifact_name:
        required: true
        type: string
  workflow_dispatch:
    inputs:
      package:
        description: "Name of the package to build"
        required: true
        default: "livekit-agents"
      artifact_name:
        description: "Artifact name for the distribution package"
        required: true
        default: "build-artifact"

jobs:
  build_plugins:
    runs-on: ubuntu-latest
    if: inputs.package != 'livekit-blingfire' && inputs.package != 'livekit-blockguard' && inputs.package != 'livekit-durable'
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          submodules: true
          lfs: true

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: "3.10"

      - name: Install uv
        uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
        with:
          enable-cache: true
          cache-dependency-glob: "uv.lock"

      - name: Build package
        run: uv build --package ${{inputs.package}}

      - name: Upload distribution package
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ inputs.artifact_name }}
          path: dist/

  build_durable:
    if: inputs.package == 'livekit-durable'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            archs: x86_64
          - os: namespace-profile-default-arm64
            archs: aarch64
          - os: windows-latest
            archs: AMD64
          - os: macos-latest
            archs: x86_64 arm64

    defaults:
      run:
        working-directory: livekit-plugins/livekit-durable
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: "3.11"

      - name: Install cibuildwheel
        run: |
          python -m pip install --upgrade pip
          pip install cibuildwheel

      - name: Build wheels
        run: cibuildwheel --output-dir dist
        env:
          CIBW_BUILD_VERBOSITY: 3
          CIBW_ARCHS: ${{ matrix.archs }}

      - name: Upload distribution package
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ inputs.artifact_name }}-${{ matrix.os }}
          path: livekit-plugins/livekit-durable/dist/

  build_blockguard:
    if: inputs.package == 'livekit-blockguard'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            archs: x86_64
          - os: namespace-profile-default-arm64
            archs: aarch64
          - os: windows-latest
            archs: AMD64
          - os: macos-latest
            archs: x86_64 arm64

    defaults:
      run:
        working-directory: livekit-plugins/livekit-blockguard
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: "3.11"

      - name: Install cibuildwheel
        run: |
          python -m pip install --upgrade pip
          pip install cibuildwheel

      - name: Build wheels
        run: cibuildwheel --output-dir dist
        env:
          CIBW_BUILD_VERBOSITY: 3
          CIBW_ARCHS: ${{ matrix.archs }}

      - name: Upload distribution package
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ inputs.artifact_name }}-${{ matrix.os }}
          path: livekit-plugins/livekit-blockguard/dist/

  build_blingfire:
    if: inputs.package == 'livekit-blingfire'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # wheels to build:
        include:
          - os: ubuntu-latest
            archs: x86_64
          - os: namespace-profile-default-arm64
            archs: aarch64
          - os: windows-latest
            archs: AMD64
          - os: macos-latest
            archs: x86_64 arm64

    defaults:
      run:
        working-directory: livekit-plugins/livekit-blingfire
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
        with:
          python-version: "3.11"

      - name: Install cibuildwheel
        run: |
          python -m pip install --upgrade pip
          pip install cibuildwheel

      - name: Build wheels
        run: cibuildwheel --output-dir dist
        env:
          CIBW_BUILD_VERBOSITY: 3
          CIBW_ARCHS: ${{ matrix.archs }}

      - name: Upload distribution package
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ inputs.artifact_name }}-${{ matrix.os }}
          path: livekit-plugins/livekit-blingfire/dist/

  merge_artifacts:
    if: ${{ always() && (inputs.package == 'livekit-blingfire' || inputs.package == 'livekit-blockguard' || inputs.package == 'livekit-durable') }}
    runs-on: ubuntu-latest
    needs: [build_blingfire, build_blockguard, build_durable]
    steps:
      - name: Download all platform wheels
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          pattern: ${{ inputs.artifact_name }}-*
          path: all_dists
          merge-multiple: true

      - run: ls -R all_dists

      - name: Upload unified wheel artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: ${{ inputs.artifact_name }}
          path: all_dists/
