name: Build Release

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

env:
  CARGO_TERM_COLOR: always

jobs:
  # 构建 Linux 服务端 (musl 静态链接，兼容所有 Linux 发行版)
  build-server-linux:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-musl

      - name: Install musl-tools
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - name: Build Server (musl static)
        run: cargo build --release --package easyremote-server --target x86_64-unknown-linux-musl

      - name: Prepare release
        run: |
          mkdir -p release/server
          cp target/x86_64-unknown-linux-musl/release/easyremote-server release/server/
          cp -r crates/server/static release/server/
          echo '#!/bin/bash' > release/server/start.sh
          echo 'cd "$(dirname "$0")"' >> release/server/start.sh
          echo 'chmod +x easyremote-server' >> release/server/start.sh
          echo './easyremote-server' >> release/server/start.sh
          chmod +x release/server/start.sh
          cd release && tar -czvf ../easyremote-server-linux-x86_64.tar.gz server/

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: easyremote-server-linux-x86_64
          path: easyremote-server-linux-x86_64.tar.gz

  # 构建 Windows 服务端
  build-server-windows:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc

      - name: Build Server
        run: cargo build --release --package easyremote-server --target x86_64-pc-windows-msvc

      - name: Prepare release
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path release/server
          Copy-Item target/x86_64-pc-windows-msvc/release/easyremote-server.exe release/server/
          Copy-Item -Recurse crates/server/static release/server/
          Compress-Archive -Path release/server/* -DestinationPath easyremote-server-windows-x86_64.zip

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: easyremote-server-windows-x86_64
          path: easyremote-server-windows-x86_64.zip

  # 构建 Windows 客户端 (Tauri)
  build-client-windows:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

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

      - name: Install frontend dependencies
        working-directory: crates/client-tauri/ui
        run: npm ci

      - name: Build frontend
        working-directory: crates/client-tauri/ui
        run: npm run build

      - name: Install Tauri CLI
        run: cargo install tauri-cli

      - name: Build Tauri app
        working-directory: crates/client-tauri
        run: cargo tauri build

      - name: Upload MSI installer
        uses: actions/upload-artifact@v4
        with:
          name: easyremote-client-windows-x86_64-msi
          path: target/release/bundle/msi/*.msi

      - name: Upload EXE
        uses: actions/upload-artifact@v4
        with:
          name: easyremote-client-windows-x86_64-exe
          path: target/release/easyremote-client.exe

  # 创建 Release (仅在 tag 推送时)
  create-release:
    needs: [build-server-linux, build-server-windows, build-client-windows]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Display structure
        run: ls -R artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            artifacts/easyremote-server-linux-x86_64/*.tar.gz
            artifacts/easyremote-server-windows-x86_64/*.zip
            artifacts/easyremote-client-windows-x86_64-msi/*.msi
            artifacts/easyremote-client-windows-x86_64-exe/*.exe
          draft: false
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
