name: Build and Release

on:
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch: {}

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    name: Build artifacts on matrix and create release
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Install dependencies
        run: npm ci

      - name: Generate icons (if source present)
        run: |
          if [ -f assets/source.png ]; then
            bash scripts/generate-icons.sh assets/source.png || true
          else
            echo "No source icon found at assets/source.png; skipping icon generation"
          fi

      - name: Build Linux artifacts
        if: runner.os == 'Linux'
        run: npm run build:linux

      - name: Build Windows artifacts
        if: runner.os == 'Windows'
        run: npm run build:win

      - name: Build macOS artifacts
        if: runner.os == 'macOS'
        run: npm run build:mac

  # Note: The following separate jobs run on specific runners because macOS and Windows builds
  # typically need to run on their respective OS runners. They upload artifacts to the workflow
  # run so the final job can create a release.

  build-matrix:
    needs: build-and-release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '18'
      - run: npm ci
      - name: Generate icons
        run: |
          if [ -f assets/source.png ]; then
            bash scripts/generate-icons.sh assets/source.png || true
          else
            echo "No source icon found at assets/source.png; skipping icon generation"
          fi
      - name: Build
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          CSC_LINK: ${{ secrets.CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_ID_PASSWORD: ${{ secrets.APPLE_ID_PASSWORD }}
        run: |
          if [ "${{ runner.os }}" == "Linux" ]; then
            npm run build:linux
          elif [ "${{ runner.os }}" == "Windows" ]; then
            npm run build:win
          else
            npm run build:mac
          fi
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist-${{ runner.os }}
          path: dist/**

  create-release:
    needs: build-matrix
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts (Linux)
        uses: actions/download-artifact@v4
        with:
          name: dist-ubuntu-latest
          path: dist-linux || true

      - name: Download artifacts (Windows)
        uses: actions/download-artifact@v4
        with:
          name: dist-Windows
          path: dist-win || true

      - name: Download artifacts (macOS)
        uses: actions/download-artifact@v4
        with:
          name: dist-macos-latest
          path: dist-mac || true

      - name: Create GitHub Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ github.ref_name }}
          release_name: Release ${{ github.ref_name }}
          draft: true
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

      - name: Upload release assets (Linux)
        uses: softprops/action-gh-release@v1
        with:
          files: dist-linux/**
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

      - name: Upload release assets (Windows)
        uses: softprops/action-gh-release@v1
        with:
          files: dist-win/**
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

      - name: Upload release assets (macOS)
        uses: softprops/action-gh-release@v1
        with:
          files: dist-mac/**
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

  # Reminder: set repository secrets: GH_TOKEN, CSC_LINK, CSC_KEY_PASSWORD, APPLE_ID, APPLE_ID_PASSWORD
