name: Build Windows Internal Installer

on:
  push:
    branches:
      - main
    paths:
      - ".github/workflows/build-windows-manual.yml"
  workflow_dispatch:
    inputs:
      version:
        description: "Internal test installer version"
        required: true
        default: "0.1.1"
      artifact_name:
        description: "Uploaded artifact name"
        required: true
        default: "Guanghu-Windows-x64-internal"

env:
  NODE_OPTIONS: --max-old-space-size=4096
  RUST_TARGET_CACHE_VERSION: v2026-07-16-guanghu-windows-internal

jobs:
  build-windows:
    name: Build Windows x64 internal installer
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "pnpm"

      - name: Install Linux cross-build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y nsis lld llvm clang

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

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            src-tauri/target
            .xwin-cache
          key: ${{ runner.os }}-internal-windows-cargo-x86_64-pc-windows-msvc-${{ env.RUST_TARGET_CACHE_VERSION }}-${{ hashFiles('src-tauri/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-internal-windows-cargo-x86_64-pc-windows-msvc-${{ env.RUST_TARGET_CACHE_VERSION }}-

      - name: Install cargo-xwin
        run: cargo install --locked cargo-xwin

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Clear cached Windows bundle artifacts
        run: |
          rm -rf src-tauri/target/x86_64-pc-windows-msvc/release/bundle

      - name: Set internal app version
        run: |
          VERSION="${{ inputs.version }}"
          if [ -z "$VERSION" ]; then VERSION="0.1.1"; fi
          jq --arg v "$VERSION" '.version = $v' src-tauri/tauri.conf.json > tmp.json && mv tmp.json src-tauri/tauri.conf.json
          sed -i "s/^version = \".*\"/version = \"$VERSION\"/" src-tauri/Cargo.toml

      - name: Build unsigned NSIS installer for internal testing
        env:
          VITE_SENTRY_DSN: ""
          SENTRY_DSN: ""
          VITE_POSTHOG_KEY: ""
          VITE_POSTHOG_HOST: ""
          XWIN_CACHE_DIR: ${{ github.workspace }}/.xwin-cache
        run: |
          pnpm tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc --bundles nsis

      - name: Validate installer
        shell: bash
        run: |
          shopt -s nullglob
          installers=(src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe)
          if [ ${#installers[@]} -eq 0 ]; then
            echo "::error::Windows build produced no NSIS setup executable."
            exit 1
          fi
          for installer in "${installers[@]}"; do
            echo "Built Windows internal installer: $installer"
          done

      - name: Upload Windows internal installer
        uses: actions/upload-artifact@v4
        with:
          name: Guanghu-Windows-x64-internal
          path: src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*-setup.exe
          if-no-files-found: error
          retention-days: 7
