name: Build and Release

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

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

jobs:
  # 构建 Windows 版本
  build-windows:
    runs-on: windows-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Configure Git to use HTTPS instead of SSH
        run: git config --global url."https://github.com/".insteadOf "git@github.com:"

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

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

      - name: Install dependencies
        run: pnpm install --no-frozen-lockfile

      - name: Build and Package
        env:
          CSC_IDENTITY_AUTO_DISCOVERY: false
        run: |
          pnpm run build
          pnpm exec electron-builder --win --publish never

      - name: List build output
        run: Get-ChildItem -Path release/build -Recurse | Select-Object FullName, Length

      - name: Upload Windows artifacts
        uses: actions/upload-artifact@v4
        with:
          name: windows-build
          path: release/build/*.exe
          if-no-files-found: error
          retention-days: 5

  # 构建 macOS 版本
  build-macos:
    runs-on: macos-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Configure Git to use HTTPS instead of SSH
        run: git config --global url."https://github.com/".insteadOf "git@github.com:"

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

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

      - name: Install dependencies
        run: pnpm install --no-frozen-lockfile

      - name: Build and Package
        env:
          CSC_IDENTITY_AUTO_DISCOVERY: false
        run: |
          pnpm run build
          pnpm exec electron-builder --mac --publish never

      - name: List build output
        run: ls -la release/build/

      - name: Upload macOS artifacts
        uses: actions/upload-artifact@v4
        with:
          name: macos-build
          path: |
            release/build/*.dmg
            release/build/*.zip
          if-no-files-found: error
          retention-days: 5

  # 构建 Linux 版本
  build-linux:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Configure Git to use HTTPS instead of SSH
        run: git config --global url."https://github.com/".insteadOf "git@github.com:"

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

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

      - name: Install dependencies
        run: pnpm install --no-frozen-lockfile

      - name: Build and Package
        run: |
          pnpm run build
          pnpm exec electron-builder --linux --publish never

      - name: List build output
        run: ls -la release/build/

      - name: Upload Linux artifacts
        uses: actions/upload-artifact@v4
        with:
          name: linux-build
          path: |
            release/build/*.AppImage
            release/build/*.deb
          if-no-files-found: error
          retention-days: 5

  # 发布 Release
  release:
    needs: [build-windows, build-macos, build-linux]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    
    permissions:
      contents: write
    
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Display structure of downloaded files
        run: ls -R artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            artifacts/windows-build/*
            artifacts/macos-build/*
            artifacts/linux-build/*
          draft: false
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
