name: Build ReBreak Magic Windows

# Baut den NSIS-Installer der neuen unified ReBreak-Magic-App (Tauri) auf einem
# echten Windows-Runner — vom Mac aus geht kein Cross-Compile (MSVC + WebView2).
# Artefakt: x64-Installer, herunterladbar unter Actions → Run → Artifacts.

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - "apps/rebreak-magic/**"
      - ".github/workflows/build-rebreak-magic-win.yml"
      - "ops/mdm/supervise-magic/**"

permissions:
  contents: read

concurrency:
  group: build-rebreak-magic-win
  cancel-in-progress: true

jobs:
  build:
    name: NSIS Installer (x64)
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

      - uses: dtolnay/rust-toolchain@stable

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.22"

      - name: Install deps (rebreak-magic)
        run: pnpm install --filter @rebreak/magic --no-frozen-lockfile

      - name: Build supervise-magic Sidecar (Windows)
        working-directory: ops/mdm/supervise-magic
        shell: pwsh
        run: |
          go build -o bin/rebreak-supervise-magic-x86_64-pc-windows-msvc.exe ./cmd/supervise
          $triple = (rustc -vV | Select-String 'host: (.*)').Matches.Groups[1].Value
          New-Item -ItemType Directory -Force -Path "../../apps/rebreak-magic/src-tauri/binaries" | Out-Null
          Copy-Item "bin/rebreak-supervise-magic-x86_64-pc-windows-msvc.exe" "../../apps/rebreak-magic/src-tauri/binaries/supervise-magic-$triple.exe" -Force

      - name: Build Tauri-App (Frontend + NSIS)
        working-directory: apps/rebreak-magic
        run: pnpm tauri build

      - name: Upload Installer
        uses: actions/upload-artifact@v4
        with:
          name: ReBreak-Magic-Windows-x64
          path: apps/rebreak-magic/src-tauri/target/release/bundle/nsis/*.exe
          if-no-files-found: error
