Set-Location $PSScriptRoot
Write-Host "============================================"
Write-Host "Building NexaOffice WorkSuite - clean build"
Write-Host "============================================"

if (!(Test-Path "assets\nexaoffice.ico")) {
    Write-Error "assets\nexaoffice.ico not found."
    exit 1
}
if (!(Test-Path "nexaoffice_worksuite.spec")) {
    Write-Error "nexaoffice_worksuite.spec not found."
    exit 1
}

Get-Process NexaOffice -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Get-Process NexaOffice_WorkSuite -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1

foreach ($folder in @("dist","build","__pycache__")) {
    if (Test-Path $folder) {
        try {
            attrib -R "$folder\*.*" /S /D 2>$null
            Remove-Item -Recurse -Force -ErrorAction Stop $folder
        } catch {
            $stamp = Get-Date -Format "yyyyMMdd_HHmmss"
            Rename-Item $folder "$folder`_old_$stamp" -ErrorAction SilentlyContinue
        }
    }
}
Get-ChildItem -Recurse -Directory -Filter __pycache__ -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

python -m pip install -r requirements.txt
if (Test-Path "requirements-build.txt") {
    python -m pip install -r requirements-build.txt
} else {
    python -m pip install "pyinstaller>=6.10,<7" pillow
}

Write-Host ""
Write-Host "Running release policy check..."
python tools\release_policy_check.py --write-report
if ($LASTEXITCODE -ne 0) {
    Write-Error "Release policy check failed. If this is a 10-version milestone, create the milestone check doc first."
    exit $LASTEXITCODE
}

Write-Host ""
Write-Host "Running security preflight checks..."
python tools\security_preflight.py --write-report
if ($LASTEXITCODE -ne 0) {
    Write-Error "Security preflight failed. Please check data\security_preflight_latest.json."
    exit $LASTEXITCODE
}

Write-Host ""
Write-Host "Running PyInstaller with spec file..."
Write-Host "NOTE: --noupx is not passed because PyInstaller disallows it with .spec files. UPX is disabled inside the spec."
pyinstaller --clean --noconfirm --distpath "$PSScriptRoot\dist" --workpath "$PSScriptRoot\build" "$PSScriptRoot\nexaoffice_worksuite.spec"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Build failed."
    exit $LASTEXITCODE
}

Write-Host ""
Write-Host "Done."
Write-Host "EXE: $PSScriptRoot\dist\NexaOffice_WorkSuite\NexaOffice_WorkSuite.exe"
Write-Host "Icon embedded from: $PSScriptRoot\assets\nexaoffice.ico"
Write-Host "Runtime data folder: $env:APPDATA\NexaOffice\data"
powershell -NoProfile -ExecutionPolicy Bypass -File tools\sign_windows_build.ps1 | Out-Host
python tools\build_manifest.py | Out-Host
python tools\package_trust_report.py | Out-Host
powershell -NoProfile -ExecutionPolicy Bypass -File tools\windows_defender_scan.ps1 | Out-Host
Write-Host "If antivirus flags it, it is usually because unsigned PyInstaller apps are treated as unknown. Code-signing and vendor false-positive submission are the official fixes for public distribution."
Write-Host "Security preflight: $PSScriptRoot\data\security_preflight_latest.json"
Write-Host "Code signing report: $PSScriptRoot\data\code_signing_latest.json"
Write-Host "Defender helper report: $PSScriptRoot\data\windows_defender_scan_latest.json"
Write-Host "Trust report: $PSScriptRoot\data\package_trust_report_latest.json"
