name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  release:
    runs-on: ${{ matrix.os }}
    
    strategy:
      matrix:
        include:
          - os: windows-latest
            platform: win32
          - os: ubuntu-latest  
            platform: linux
          - os: macos-latest
            platform: darwin
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Setup pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 10.12.3
          run_install: false
      
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
      
      - name: Setup pnpm cache
        uses: actions/cache@v3
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      
      - name: Build application
        run: |
          pnpm run build
          pnpm run dist
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CSC_IDENTITY_AUTO_DISCOVERY: false
      
      - name: List build files (debug)
        shell: bash
        run: |
          echo "Listing release directory:"
          ls -la release/ || echo "No release directory found"
          find . -name "*.exe" -o -name "*.dmg" -o -name "*.AppImage" -o -name "*.deb" -o -name "*.rpm" || echo "No build files found"
      
      - name: Upload Release Assets
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            release/*.exe
            release/*.zip
            release/*.AppImage
          name: "CC Switch ${{ github.ref_name }}"
          body: |
            ## CC Switch ${{ github.ref_name }}
            
            Claude Code 供应商切换工具
            
            ### 下载
            
            #### Windows 用户
            - **安装版 (推荐)**: `CC Switch Setup ${{ github.ref_name }}.exe`
            - **便携版**: `CC Switch ${{ github.ref_name }}.exe`
            
            #### macOS 用户（推荐使用通用版本）
            - **通用版本**: `CC Switch-${{ github.ref_name }}-mac.zip` - 兼容所有Mac（Intel + M系列）
            
            #### Linux 用户
            - **AppImage**: `CC Switch-${{ github.ref_name }}.AppImage`
            
            ### macOS 安装说明
            1. 下载 ZIP 文件后解压
            2. 首次打开可能出现"未知开发者"警告
            3. 前往"系统设置" → "隐私与安全性" → 点击"仍要打开"
            4. 或者使用命令: `xattr -cr "/path/to/CC Switch.app"`
            
            ### 注意事项
            - macOS 版本使用 Intel 架构，通过 Rosetta 2 在 M 系列芯片上运行
            - 兼容性和稳定性最佳，性能损失minimal
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}