# 1. Ensure 'uv' is in the PATH for this specific session
$env:Path += ";$env:USERPROFILE\.local\bin"

# 2. Clean old build artifacts using Python
python -c "import shutil, os, glob; [shutil.rmtree(p, ignore_errors=True) for p in ['app/dist', 'app/build'] + glob.glob('app/*.spec')]"

# 3. Navigate to the app folder
cd app

# 4. Execute the build using uv and pyinstaller
# This targets run_app.py and collects necessary assets
uv run pyinstaller --name "Digiqual" `
    --noconfirm `
    --windowed `
    --collect-all digiqual `
    --collect-all shiny `
    --collect-all faicons `
    --collect-all shinyswatch `
    --collect-all htmltools `
    --collect-all pywebview `
    --collect-all matplotlib `
    --hidden-import="uvicorn.loops.auto" `
    --hidden-import="uvicorn.protocols.http.auto" `
    --hidden-import="uvicorn.lifespan.on" `
    --hidden-import="engineio.async_drivers.threading" `
    run_app.py

# 5. Return to the root directory
cd ..

Write-Host "✅ Real build process finished! Your .exe is in app/dist/Digiqual/Digiqual.exe" -ForegroundColor Green