name: Build Windows

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build Windows
    runs-on: ubuntu-latest
    
    steps:
    # 使用 git clone 而非 actions/checkout
    - name: Clone repository
      run: |
        URL="${GITEA_SERVER_URL}/${GITEA_REPOSITORY}.git"
        git clone $URL .
        git checkout ${{ gitea.sha }}
        
    - name: Install Rust
      run: |
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal
        source $HOME/.cargo/env
        # 添加 Windows MSVC 目标（xwin 需要）
        rustup target add x86_64-pc-windows-msvc
        
    - name: Install xwin (MinGW CRT)
      run: |
        source $HOME/.cargo/env
        # 安装 xwin（需要 x86_64-pc-windows-msvc 目标）
        cargo install xwin
        
    - name: Download MinGW CRT
      run: |
        source $HOME/.cargo/env
        # 下载预编译的 MinGW CRT
        xwin download --accept-license
        
    - name: Build
      run: |
        source $HOME/.cargo/env
        
        # 设置 xwin 环境变量
        export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER=xwin-link
        export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_AR=xwin-lib
        export CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RANLIB=true
        
        # 使用 xwin 提供的 SDK
        export XWIN_PATH=$HOME/.cache/xwin
        
        cargo build --release --target x86_64-pc-windows-msvc
        
    - name: Package
      run: |
        mkdir -p dist
        cp target/x86_64-pc-windows-msvc/release/readflow.exe dist/
        cd dist
        echo "ReadFlow for Windows" > README.txt
        echo "Version: ${GITEA_REF_NAME}" >> README.txt
        echo "Built with: xwin + MSVC" >> README.txt
        echo "Date: $(date)" >> README.txt
        zip -r readflow-windows-x86_64.zip readflow.exe README.txt
        
    - name: Upload to Release
      run: |
        TAG="${GITEA_REF_NAME}"
        TOKEN="${GITEA_TOKEN}"
        URL="${GITEA_SERVER_URL}"
        REPO="${GITEA_REPOSITORY}"
        
        # Get or create release
        RELEASE=$(curl -s "${URL}/api/v1/repos/${REPO}/releases/tags/${TAG}" \
          -H "Authorization: token ${TOKEN}")
        
        RELEASE_ID=$(echo $RELEASE | jq -r '.id')
        
        if [ "$RELEASE_ID" == "null" ] || [ -z "$RELEASE_ID" ]; then
          RELEASE_ID=$(curl -s -X POST "${URL}/api/v1/repos/${REPO}/releases" \
            -H "Authorization: token ${TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{\"tag_name\": \"${TAG}\", \"name\": \"ReadFlow ${TAG}\", \"body\": \"Release ${TAG}\\n\\nBuilt with xwin (预编译 MinGW CRT)\", \"draft\": false}" \
            | jq -r '.id')
        fi
        
        echo "Release ID: ${RELEASE_ID}"
        
        # Upload asset
        curl -s -X POST "${URL}/api/v1/repos/${REPO}/releases/${RELEASE_ID}/assets?name=readflow-windows-x86_64.zip" \
          -H "Authorization: token ${TOKEN}" \
          -H "Content-Type: application/zip" \
          --data-binary @dist/readflow-windows-x86_64.zip
        
        echo "✅ Upload completed!"
      env:
        GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        GITEA_SERVER_URL: http://192.168.120.110:4000
        GITEA_REPOSITORY: damai/readflow
        GITEA_REF_NAME: ${{ gitea.ref_name }}
