name: Build and Release Electron App

on:
  push:
    tags:
      - "v*.*.*" # Triggers on version tags like v1.0.0
  workflow_dispatch: # Allows manual triggering
    inputs:
      version:
        description: "Version to release (e.g., v1.0.0)"
        required: true
        default: "v0.1.0"

jobs:
  build-and-release:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            name: macOS
          - os: windows-latest
            name: Windows
          - os: ubuntu-latest
            name: Linux

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

    permissions:
      contents: write

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
          cache-dependency-path: package-lock.json

      - name: Install dependencies
        # Use npm install instead of npm ci to correctly resolve platform-specific
        # optional dependencies (e.g., @tailwindcss/oxide, lightningcss binaries)
        run: npm install

      - name: Build Electron App (macOS)
        if: matrix.os == 'macos-latest'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npm run build:electron -- --mac --x64 --arm64

      - name: Build Electron App (Windows)
        if: matrix.os == 'windows-latest'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npm run build:electron -- --win --x64

      - name: Build Electron App (Linux)
        if: matrix.os == 'ubuntu-latest'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npm run build:electron -- --linux --x64

      - name: Upload Release Assets
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.event.inputs.version || github.ref_name }}
          files: |
            apps/app/dist/*.exe
            apps/app/dist/*.dmg
            apps/app/dist/*.AppImage
            apps/app/dist/*.zip
            apps/app/dist/*.deb
            apps/app/dist/*.rpm
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
