name: Build Release

on:
  workflow_dispatch:
    inputs:
      version_tag:
        description: 'Version label for the build (e.g. alpha-1, beta-2)'
        required: false
        default: ''

jobs:
  build:
    name: Build on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            dist_cmd: dist:mac
          - os: ubuntu-latest
            dist_cmd: dist:linux
          - os: windows-latest
            dist_cmd: dist:win

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22.x
          cache: 'npm'

      - name: Cache Electron
        uses: actions/cache@v4
        with:
          path: |
            ~/.cache/electron
            ~/.cache/electron-builder
          key: ${{ runner.os }}-electron-cache

      - name: Install dependencies
        run: npm ci

      - name: Install Linux native build dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libpulse-dev

      - name: Build native DSP module
        run: npm run rebuild:native

      - name: Verify native module exists
        shell: bash
        run: |
          if [ ! -f native/build/Release/visualizer_dsp.node ]; then
            echo "ERROR: native DSP module was not built!"
            exit 1
          fi
          echo "Native module built successfully: $(ls -lh native/build/Release/visualizer_dsp.node)"

      - name: Build distributable
        run: npm run ${{ matrix.dist_cmd }}
        env:
          CSC_IDENTITY_AUTO_DISCOVERY: 'false'

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: prism-${{ runner.os }}${{ inputs.version_tag && format('-{0}', inputs.version_tag) || '' }}
          path: |
            dist/*.exe
            dist/*.dmg
            dist/*.zip
            dist/*.AppImage
            dist/*.deb
          if-no-files-found: error
          retention-days: 30
