name: Build Desktop Applications

on:
  workflow_dispatch:  # 手動觸發
    inputs:
      platforms:
        description: '選擇要構建的平台'
        required: true
        default: 'all'
        type: choice
        options:
        - all
        - windows
        - macos
        - linux
      upload_artifacts:
        description: '是否上傳構建產物'
        required: true
        default: true
        type: boolean

  push:
    paths:
      - 'src-tauri/**'  # 桌面應用代碼變更時自動觸發
      - 'scripts/build_desktop.py'
    branches:
      - main

  pull_request:
    paths:
      - 'src-tauri/**'
      - 'scripts/build_desktop.py'

env:
  CARGO_TERM_COLOR: always

jobs:
  # 多平台桌面應用構建
  build-desktop:
    strategy:
      fail-fast: false  # 允許部分平台失敗
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary: mcp-feedback-enhanced-desktop.exe
            name: windows
            enabled: ${{ github.event.inputs.platforms == 'all' || github.event.inputs.platforms == 'windows' || github.event.inputs.platforms == '' }}
          - os: macos-latest
            target: x86_64-apple-darwin
            binary: mcp-feedback-enhanced-desktop
            name: macos-intel
            enabled: ${{ github.event.inputs.platforms == 'all' || github.event.inputs.platforms == 'macos' || github.event.inputs.platforms == '' }}
          - os: macos-latest
            target: aarch64-apple-darwin
            binary: mcp-feedback-enhanced-desktop
            name: macos-arm64
            enabled: ${{ github.event.inputs.platforms == 'all' || github.event.inputs.platforms == 'macos' || github.event.inputs.platforms == '' }}
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary: mcp-feedback-enhanced-desktop
            name: linux
            enabled: ${{ github.event.inputs.platforms == 'all' || github.event.inputs.platforms == 'linux' || github.event.inputs.platforms == '' }}

    runs-on: ${{ matrix.os }}
    if: ${{ matrix.enabled != 'false' }}

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

    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: ${{ matrix.target }}

    - name: Install uv
      uses: astral-sh/setup-uv@v4
      with:
        version: "latest"

    - name: Set up Python
      run: uv python install

    - name: Install dependencies
      run: uv sync --dev

    - name: Cache Rust dependencies
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          src-tauri/target
        key: ${{ runner.os }}-cargo-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-${{ matrix.target }}-
          ${{ runner.os }}-cargo-

    - name: Build desktop application for ${{ matrix.name }}
      run: |
        cd src-tauri
        cargo build --release --target ${{ matrix.target }} --bin mcp-feedback-enhanced-desktop

    - name: Verify build output
      run: |
        echo "🔍 檢查構建產物..."
        if [ "${{ matrix.os }}" = "windows-latest" ]; then
          ls -la "src-tauri/target/${{ matrix.target }}/release/${{ matrix.binary }}" || echo "❌ Windows 二進制文件不存在"
        else
          ls -la "src-tauri/target/${{ matrix.target }}/release/${{ matrix.binary }}" || echo "❌ ${{ matrix.name }} 二進制文件不存在"
        fi
      shell: bash

    - name: Upload desktop binary
      if: ${{ github.event.inputs.upload_artifacts != 'false' }}
      uses: actions/upload-artifact@v4
      with:
        name: desktop-${{ matrix.name }}
        path: src-tauri/target/${{ matrix.target }}/release/${{ matrix.binary }}
        retention-days: 30  # 保留 30 天
        compression-level: 6

  # 構建摘要
  build-summary:
    needs: build-desktop
    runs-on: ubuntu-latest
    if: always()

    steps:
    - name: Generate build summary
      run: |
        echo "## 🖥️ 桌面應用構建摘要" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### 構建結果" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY

        # 檢查各平台構建狀態
        if [ "${{ needs.build-desktop.result }}" = "success" ]; then
          echo "✅ 所有平台構建成功" >> $GITHUB_STEP_SUMMARY
        elif [ "${{ needs.build-desktop.result }}" = "failure" ]; then
          echo "❌ 部分平台構建失敗" >> $GITHUB_STEP_SUMMARY
        else
          echo "⚠️ 構建狀態: ${{ needs.build-desktop.result }}" >> $GITHUB_STEP_SUMMARY
        fi

        echo "" >> $GITHUB_STEP_SUMMARY
        echo "### 下一步" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "- 構建產物已上傳到 GitHub Artifacts" >> $GITHUB_STEP_SUMMARY
        echo "- 可以在發佈流程中使用這些二進制文件" >> $GITHUB_STEP_SUMMARY
        echo "- 如需重新構建，請手動觸發此工作流程" >> $GITHUB_STEP_SUMMARY
