$ErrorActionPreference = "Stop"

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
$RepoRoot = Resolve-Path "$ScriptDir/../.."
$BuildDir = "$RepoRoot/build/bin/Release"
$DistRoot = "$RepoRoot/dist"
$PackageName = "WinDictation"
$PackageDir = "$DistRoot/$PackageName"
$ZipFile = "$DistRoot/${PackageName}.zip"

Write-Host "=== Packaging WinDictation ===" -ForegroundColor Cyan

# 1. Clean and Create Directories
if (Test-Path $DistRoot) { Remove-Item -Recurse -Force $DistRoot }
New-Item -ItemType Directory -Path $PackageDir -Force | Out-Null
New-Item -ItemType Directory -Path "$PackageDir/models" -Force | Out-Null

Write-Host "  [+] Created dist directory" -ForegroundColor Green

# 2. Copy Executable and DLLs
Write-Host "  [+] Copying binaries..." -ForegroundColor Yellow
Copy-Item "$BuildDir/win-dictation.exe" $PackageDir
Get-ChildItem "$BuildDir/*.dll" | Copy-Item -Destination $PackageDir
Write-Host "      - win-dictation.exe" -ForegroundColor Gray
Write-Host "      - DLLs (SDL2, CUDA, GGML, Whisper)" -ForegroundColor Gray

# 3. Copy Model
Write-Host "  [+] Copying models..." -ForegroundColor Yellow
if (Test-Path "$BuildDir/models/ggml-base.en.bin") {
    Copy-Item "$BuildDir/models/ggml-base.en.bin" "$PackageDir/models/"
    Write-Host "      - ggml-base.en.bin" -ForegroundColor Gray
} else {
    Write-Host "      [WARN] Base model not found in build directory!" -ForegroundColor Red
}

# 4. Copy Documentation
Write-Host "  [+] Copying documentation..." -ForegroundColor Yellow
Copy-Item "$ScriptDir/README.md" "$PackageDir/README.txt"
Write-Host "      - README.txt" -ForegroundColor Gray

# 5. Create Zip
Write-Host "  [+] Creating zip archive..." -ForegroundColor Yellow
Compress-Archive -Path "$PackageDir/*" -DestinationPath $ZipFile -Force

Write-Host ""
Write-Host "=== Package Ready ===" -ForegroundColor Green
Write-Host "Location: $ZipFile" -ForegroundColor Cyan
Write-Host "Contents:" -ForegroundColor Cyan
Get-ChildItem -Recurse $PackageDir | Select-Object Name, Length | Format-Table -AutoSize



