name: Build Multi-Platform Release

on:
  push:
    tags:
      - 'v*' # 当推送形如 v1.0.0 的 tag 时触发构建
  workflow_dispatch: # 支持手动触发构建

jobs:
  # 1. 编译 Windows 端 (.exe)
  build-windows:
    runs-on: windows-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pyinstaller

      - name: Build EXE
        run: |
          python -m PyInstaller build.spec --clean

      - name: Upload Windows Artifact
        uses: actions/upload-artifact@v4
        with:
          name: CursorTokenLogin-Windows
          path: dist/CursorTokenLogin.exe

  # 2. 编译 macOS 端 (.app)
  build-macos:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-13, macos-latest]
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pyinstaller Pillow

      - name: Set Architecture Name
        run: |
          if [ "${{ matrix.os }}" = "macos-13" ]; then
            echo "ARCH_NAME=Intel" >> $GITHUB_ENV
          else
            echo "ARCH_NAME=AppleSilicon" >> $GITHUB_ENV
          fi
        shell: bash

      - name: Build App Bundle
        run: |
          python -m PyInstaller build.spec --clean

      - name: Zip macOS App
        run: |
          cd dist
          zip -r CursorTokenLogin-macOS-${{ env.ARCH_NAME }}.zip CursorTokenLogin.app

      - name: Upload macOS Artifact
        uses: actions/upload-artifact@v4
        with:
          name: CursorTokenLogin-macOS-${{ env.ARCH_NAME }}
          path: dist/CursorTokenLogin-macOS-${{ env.ARCH_NAME }}.zip

  # 3. 编译 Linux 端并打包为 .deb 和 .rpm
  build-linux:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pyinstaller
          # 安装打包 deb 和 rpm 所需的系统工具
          sudo apt-get update
          sudo apt-get install -y dpkg rpm alien

      - name: Build Linux Binary
        run: |
          python -m PyInstaller build.spec --clean

      - name: Pack DEB Package
        run: |
          # 创建 deb 包目录结构
          mkdir -p deb-package/DEBIAN
          mkdir -p deb-package/usr/bin
          mkdir -p deb-package/usr/share/applications
          mkdir -p deb-package/usr/share/pixmaps

          # 拷贝编译好的二进制文件
          cp dist/CursorTokenLogin deb-package/usr/bin/cursortokenlogin
          chmod +x deb-package/usr/bin/cursortokenlogin

          # 拷贝图标
          cp logo.png deb-package/usr/share/pixmaps/cursortokenlogin.png

          # 创建控制文件
          cat <<EOF > deb-package/DEBIAN/control
          Package: cursortokenlogin
          Version: 1.0.0
          Section: utils
          Priority: optional
          Architecture: amd64
          Maintainer: Yunzer
          Description: Cursor Token Login Helper
          EOF

          # 创建桌面启动快捷方式
          cat <<EOF > deb-package/usr/share/applications/cursortokenlogin.desktop
          [Desktop Entry]
          Name=CursorTokenLogin
          Comment=Cursor Token Login Helper
          Exec=/usr/bin/cursortokenlogin
          Icon=cursortokenlogin
          Terminal=false
          Type=Application
          Categories=Utility;Development;
          EOF

          # 构建 deb
          dpkg-deb --build deb-package cursortokenlogin.deb

      - name: Pack RPM Package (Convert from DEB using alien)
        run: |
          # 使用 alien 工具将 deb 快速转换为 rpm，避免编写复杂的 spec 构建脚本
          sudo alien --to-rpm --scripts cursortokenlogin.deb
          # 重命名生成的 rpm 文件方便识别
          mv *.rpm cursortokenlogin.rpm

      - name: Upload Linux Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: CursorTokenLogin-Linux
          path: |
            cursortokenlogin.deb
            cursortokenlogin.rpm

  # 4. 自动创建 GitHub Release 并上传 5 个包
  create-release:
    needs: [build-windows, build-macos, build-linux]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v') # 仅在推送 tag 时执行 Release 发布
    steps:
      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./release-files
          merge-multiple: true

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            release-files/CursorTokenLogin.exe
            release-files/CursorTokenLogin-macOS-Intel.zip
            release-files/CursorTokenLogin-macOS-AppleSilicon.zip
            release-files/cursortokenlogin.deb
            release-files/cursortokenlogin.rpm
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
