name: Build Windows Desktop Client

on:
  workflow_dispatch:

jobs:
  build-windows-client:
    name: Build Windows Client
    runs-on: ubuntu-latest

    defaults:
      run:
        shell: bash
        working-directory: desktop-client

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

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

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

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            build-essential \
            clang \
            lld \
            llvm \
            curl \
            wget \
            file \
            nsis \
            pkg-config \
            libssl-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            patchelf

      - name: Install cargo-xwin
        run: |
          if ! cargo xwin --version >/dev/null 2>&1; then
            cargo install --locked cargo-xwin
          fi

      - name: Install desktop client dependencies
        run: npm install

      - name: Build bundled Windows tunnel helper
        run: npm run helper:windows-x64

      - name: Build Windows installer
        run: npm run tauri:build:windows-x64:linux

      - name: Upload Windows installer
        uses: christopherhx/gitea-upload-artifact@v4
        with:
          name: NexaVPN-windows-installer
          path: |
            desktop-client/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*.exe
            desktop-client/src-tauri/target/x86_64-pc-windows-msvc/release/bundle/nsis/*.msi
          if-no-files-found: error

      - name: Upload raw Windows build outputs
        uses: christopherhx/gitea-upload-artifact@v4
        with:
          name: NexaVPN-windows-raw-build
          path: |
            desktop-client/src-tauri/target/x86_64-pc-windows-msvc/release/nexavpn-desktop.exe
            desktop-client/src-tauri/bundled/windows-x64/nexavpn-tunnel-helper.exe
          if-no-files-found: error
