param(
    [string]$PythonExecutable = "C:\Users\Administrator\miniconda3\envs\cmvr-head-client\python.exe",
    [switch]$SkipBuild
)

$ErrorActionPreference = "Stop"

$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$buildScript = Join-Path $repoRoot "cpp\common\curve\build_python.ps1"
$uiScript = Join-Path $repoRoot "ui\face_servo_control.py"

if (-not (Test-Path $PythonExecutable)) {
    throw "Python executable not found at $PythonExecutable"
}

if (-not (Test-Path $uiScript)) {
    throw "UI script not found at $uiScript"
}

if (-not $SkipBuild) {
    if (-not (Test-Path $buildScript)) {
        throw "Build script not found at $buildScript"
    }

    Write-Host "[1/2] Rebuilding _s_curve_native..."
    powershell -ExecutionPolicy Bypass -File $buildScript -PythonExecutable $PythonExecutable
    if ($LASTEXITCODE -ne 0) {
        throw "Native module rebuild failed."
    }
} else {
    Write-Host "[1/2] Skipping native rebuild."
}

Write-Host "[2/2] Launching UI..."
& $PythonExecutable $uiScript
