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

$ErrorActionPreference = "Stop"

$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\\..\\..")
$sourceDir = Join-Path $repoRoot "cpp\\common\\curve"
$buildDir = Join-Path $sourceDir "build"
$vsDevCmd = "C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\Common7\\Tools\\VsDevCmd.bat"

if (-not (Test-Path $vsDevCmd)) {
    throw "VsDevCmd.bat not found at $vsDevCmd"
}

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

New-Item -ItemType Directory -Force -Path $buildDir | Out-Null

$configure = "call `"$vsDevCmd`" -arch=x64 && cmake -S `"$sourceDir`" -B `"$buildDir`" -G Ninja -DCMAKE_BUILD_TYPE=Release -DPython_EXECUTABLE=`"$PythonExecutable`""
cmd /c $configure
if ($LASTEXITCODE -ne 0) {
    throw "CMake configure failed."
}

$build = "call `"$vsDevCmd`" -arch=x64 && cmake --build `"$buildDir`" --config Release"
cmd /c $build
if ($LASTEXITCODE -ne 0) {
    throw "CMake build failed."
}

Write-Host "Built Python module:"
Get-ChildItem $buildDir -Filter "_s_curve_native*.pyd" -Recurse | Select-Object FullName
