name: Build and Release Electron App

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch: # Allows manual triggering

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

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

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

      - name: Install dependencies
        run: npm install

      - name: Build Electron app
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        run: npm run build

      - name: Display Build Artifacts
        run: ls -l dist

      - name: Upload Linux artifact
        if: matrix.os == 'ubuntu-latest'
        uses: actions/upload-artifact@v4
        with:
          name: build-linux
          path: dist/*.zip

      - name: Upload Windows artifact
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: build-win
          path: dist/*.exe

      - name: Upload Mac artifact
        if: matrix.os == 'macos-latest'
        uses: actions/upload-artifact@v4
        with:
          name: build-mac
          path: dist/*.dmg

      - name: Sha256 Mac artifact
        if: matrix.os == 'macos-latest'
        run: shasum -a 256 dist/*.dmg

  create_release:
    runs-on: ubuntu-latest
    needs: build

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

      - name: Create release name
        run: node bin/github-getver.js "${{ github.event_name }}"

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

      - name: Download All Release Assets
        uses: actions/download-artifact@v4.1.7
        with:
          path: artifacts
          merge-multiple: true

      - name: Display Downloaded Assets
        run: ls -ltR artifacts

      - name: Zip Windows artifact
        run: zip -r artifacts/KiriMoto-win-x64.zip artifacts/KiriMoto-win-x64.exe

      - name: Upload Release Asset (Linux)
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: artifacts/KiriMoto-linux-x64.zip
          asset_name: KiriMoto-Ubuntu-x64-${{ env.TAG_NAME }}.zip
          asset_content_type: application/zip

      - name: Upload Release Asset (macOS)
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: artifacts/KiriMoto-mac-arm64.dmg
          asset_name: KiriMoto-MacOS-arm-${{ env.TAG_NAME }}.dmg
          asset_content_type: application/octet-stream

      - name: Upload Release Asset (Windows)
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: artifacts/KiriMoto-win-x64.zip
          asset_name: KiriMoto-Win-x64-${{ env.TAG_NAME }}.zip
          asset_content_type: application/zip
