name: Build Artifacts

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version label (e.g. dev, 0.3.0-test)'
        required: false
        default: 'dev'

jobs:
  build-windows:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build frontend
        run: npm ci && npm run build

      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install build tools
        run: sudo apt-get update && sudo apt-get install -y --no-install-recommends nsis zip

      - name: Build Windows distribution
        run: |
          chmod +x build-dist-windows.sh
          ./build-dist-windows.sh "v${{ inputs.version }}"

      - name: Build NSIS installer
        run: makensis -DVERSION="${{ inputs.version }}" installer.nsi

      - uses: actions/upload-artifact@v3
        with:
          name: MediaServer-${{ inputs.version }}-win-x64
          path: |
            build/MediaServer-*.zip
            build/MediaServer-*-setup.exe
          retention-days: 90

  build-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Build frontend
        run: npm ci && npm run build

      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Build Linux distribution
        run: |
          chmod +x build-dist-linux.sh
          ./build-dist-linux.sh "v${{ inputs.version }}"

      - uses: actions/upload-artifact@v3
        with:
          name: MediaServer-${{ inputs.version }}-linux-x64
          path: build/MediaServer-*-linux-x64.tar.gz
          retention-days: 90
