name: Build and Release

on:
    push:
        tags:
            - 'v*.*.*'

env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
    build:
        if: github.server_url == 'https://github.com'

        strategy:
            matrix:
                include:
                    - os: macos-latest
                      platform: darwin
                      arch: x64
                    - os: macos-latest
                      platform: darwin
                      arch: arm64
                    - os: ubuntu-latest
                      platform: linux
                      arch: x64
                    - os: windows-latest
                      platform: win32
                      arch: x64

        runs-on: ${{ matrix.os }}

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

            - name: Setup Node.js
              uses: actions/setup-node@v4
              with:
                  node-version: 20

            - name: Setup pnpm
              uses: pnpm/action-setup@v4
              with:
                  version: 9

            - name: Install dependencies
              run: pnpm install

            - name: Build for ${{ matrix.platform }}-${{ matrix.arch }}
              run: pnpm run make -- --arch=${{ matrix.arch }}
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

            - name: Upload artifacts
              uses: actions/upload-artifact@v4
              with:
                  name: release-${{ matrix.platform }}-${{ matrix.arch }}
                  path: |
                      out/make/**/*.dmg
                      out/make/**/*.zip
                      out/make/**/*.exe
                      out/make/**/*.AppImage
                      out/make/**/*.deb
                      out/make/**/*.rpm
                  if-no-files-found: ignore
                  compression-level: 0

    release:
        needs: build
        runs-on: macos-latest
        if: github.server_url == 'https://github.com'

        permissions:
            contents: write

        steps:
            - name: Download all artifacts
              uses: actions/download-artifact@v4
              with:
                  path: artifacts
                  pattern: release-*
                  merge-multiple: true

            - name: List artifacts
              run: find artifacts -type f | head -50

            - name: Create Draft Release
              uses: softprops/action-gh-release@v2
              with:
                  draft: true
                  generate_release_notes: true
                  files: |
                      artifacts/**/*.dmg
                      artifacts/**/*.zip
                      artifacts/**/*.exe
                      artifacts/**/*.AppImage
                      artifacts/**/*.deb
                      artifacts/**/*.rpm
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
