name: Build Multi-Arch Binaries (Direct Release Upload)

on:
  workflow_dispatch:

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build ${{ matrix.arch }}
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86_64
            platform: linux/amd64
          - arch: arm64
            platform: linux/arm64
          - arch: armhf
            platform: linux/arm/v7

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Build binary (Ubuntu 20.04)
        run: |
          docker run --rm \
            --platform=${{ matrix.platform }} \
            -v "${{ github.workspace }}:/workspace" \
            -w /workspace \
            ubuntu:20.04 \
            bash -euxo pipefail -c '
              apt-get update

              apt-get install -y \
                python3 \
                python3-pip \
                python3-dev \
                build-essential \
                binutils

              pip3 install --upgrade pip

              pip3 install \
                pyinstaller \
                fastapi \
                uvicorn \
                aiofiles \
                pydantic \
                python-multipart

              pyinstaller nas-media-player.spec --clean --noconfirm

              mkdir -p dist_release
              cp dist/nas-media-player dist_release/nas-media-player-${{ matrix.arch }}
              chmod +x dist_release/nas-media-player-${{ matrix.arch }}
            '

      - name: Generate timestamp tag (shared logic)
        id: tag
        run: |
          echo "TAG=build-$(date +'%Y%m%d-%H%M%S')" >> $GITHUB_OUTPUT

      - name: Upload to GitHub Release (direct)
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.tag.outputs.TAG }}
          name: Release ${{ steps.tag.outputs.TAG }}
          files: dist_release/nas-media-player-${{ matrix.arch }}
          generate_release_notes: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
