name: Build and Release (Modern)

on:
  push:
    tags:
      - 'v*.*.*'  # 当推送 v1.0.0 格式的标签时触发
  workflow_dispatch:  # 手动触发
    inputs:
      version:
        description: 'Release version (e.g., v1.0.0)'
        required: true
        default: 'v1.0.0'

env:
  GO_VERSION: '1.21'
  NODE_VERSION: '18'

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: ${{ env.GO_VERSION }}

    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}

    - name: Set up Yarn
      run: |
        corepack enable
        yarn --version

    - name: Cache Go modules
      uses: actions/cache@v4
      with:
        path: |
          ~/go/pkg/mod
          ~/.cache/go-build
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Cache Node modules
      uses: actions/cache@v4
      with:
        path: |
          chuan-next/node_modules
          chuan-next/.next/cache
        key: ${{ runner.os }}-node-${{ hashFiles('chuan-next/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-node-

    - name: Install Go dependencies
      run: |
        go mod download
        go mod tidy

    - name: Install Node.js dependencies
      run: |
        cd chuan-next
        yarn install --frozen-lockfile

    - name: Run tests
      run: |
        go test -v ./...

    - name: Build application
      run: |
        chmod +x build-fullstack.sh
        ./build-fullstack.sh

    - name: Create checksums
      run: |
        cd dist
        for file in *; do
          sha256sum "$file" > "$file.sha256"
        done

    - name: Determine version
      id: version
      run: |
        if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
          echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
        else
          echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
        fi

    - name: Get build date
      id: build_date
      run: echo "date=$(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT

    - name: Create Release
      uses: softprops/action-gh-release@v2
      with:
        tag_name: ${{ steps.version.outputs.version }}
        name: File Transfer Server ${{ steps.version.outputs.version }}
        body: |
          ## 文件快传 ${{ steps.version.outputs.version }}
          
          🚀 **新版本发布**
          
          ### 📦 下载说明
          
          选择适合你的操作系统的版本：
          
          - **Windows**: `file-transfer-server.exe`
          - **macOS (Intel)**: `file-transfer-server-macos-amd64`
          - **macOS (Apple Silicon)**: `file-transfer-server-macos-arm64`
          - **Linux (x64)**: `file-transfer-server-linux-amd64`
          - **Linux (ARM64)**: `file-transfer-server-linux-arm64`
          
          ### 🛠️ 使用方法
          
          1. 下载对应平台的二进制文件
          2. 给文件添加执行权限（Linux/macOS）: `chmod +x 文件名`
          3. 运行: `./文件名` 或直接双击（Windows）
          4. 访问: http://localhost:8080
          
          ### ✨ 特性
          
          - ✅ 前端界面完全嵌入，单文件部署
          - ✅ 静态编译，无外部依赖
          - ✅ 支持多平台部署
          - ✅ 支持文件和文本传输
          - ✅ WebRTC 点对点传输
          - ✅ 实时状态同步
          
          ### 🔐 校验和
          
          每个二进制文件都附带了 SHA256 校验和文件（.sha256），用于验证文件完整性。
          
          ---
          
          📅 构建时间: ${{ steps.build_date.outputs.date }}
          🏗️ 构建环境: GitHub Actions
        files: |
          dist/file-transfer-server.exe
          dist/file-transfer-server.exe.sha256
          dist/file-transfer-server-macos-amd64
          dist/file-transfer-server-macos-amd64.sha256
          dist/file-transfer-server-macos-arm64
          dist/file-transfer-server-macos-arm64.sha256
          dist/file-transfer-server-linux-amd64
          dist/file-transfer-server-linux-amd64.sha256
          dist/file-transfer-server-linux-arm64
          dist/file-transfer-server-linux-arm64.sha256
        draft: false
        prerelease: false
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Show build summary
      run: |
        echo "🎉 Build completed successfully!"
        echo "📦 Release: ${{ steps.version.outputs.version }}"
        echo "🏗️ Built files:"
        ls -lh dist/
