# DBAOps Futuristic Enterprise - Build Release Package
# Creates a clean, shippable ZIP for customer deployment

param(
    [string]$OutputDir = ".\Release",
    [string]$Version = "2.4.0"
)

$ErrorActionPreference = "Stop"
$packageRoot = $PSScriptRoot
$packageName = "DBAOps-Futuristic-Enterprise-v$Version"
$destRoot = Join-Path $OutputDir $packageName

$excludeDirs = @("DBAOps_Backups", "node_modules", ".git", "Release", ".cursor", "terminals", "agent-transcripts")
$excludePatterns = @("*.ps1.bak", "*.log", "*.tmp", ".DS_Store", "Thumbs.db")

Write-Host ""
Write-Host "DBAOps Futuristic Enterprise - Build Release Package" -ForegroundColor Cyan
Write-Host "Version: $Version | Output: $destRoot" -ForegroundColor Cyan
Write-Host ""

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

$includeItems = @(
    "Setup.bat", "Setup.ps1", "Deploy-DBAOpsUltimate.ps1", "Build-ReleasePackage.ps1",
    "README.md", "LICENSE.txt", "PACKAGING_GUIDE.md", "SHIPMENT_CHECKLIST.md",
    "Application", "Database", "SQLAgent", "IIS", "Protection"
)

foreach ($item in $includeItems) {
    $src = Join-Path $packageRoot $item
    $dst = Join-Path $destRoot $item
    if (Test-Path $src) {
        if (Test-Path $src -PathType Container) {
            robocopy $src $dst /E /XD $excludeDirs /XF $excludePatterns /NFL /NDL /NJH /NJS /NC /NS /NP | Out-Null
        } else {
            Copy-Item $src $dst -Force
        }
        Write-Host "[OK] $item" -ForegroundColor Green
    }
}

$apiNodeModules = Join-Path $destRoot "Application\WebDashboard\api\node_modules"
if (Test-Path $apiNodeModules) {
    Remove-Item $apiNodeModules -Recurse -Force
    Write-Host "[OK] Removed api/node_modules" -ForegroundColor Green
}

$zipPath = Join-Path $OutputDir "$packageName.zip"
if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
Compress-Archive -Path $destRoot -DestinationPath $zipPath -CompressionLevel Optimal

$sizeMB = [math]::Round((Get-Item $zipPath).Length/1MB, 2)
Write-Host ""
Write-Host "Package built: $zipPath" -ForegroundColor Green
Write-Host "Size: $sizeMB MB" -ForegroundColor White
Write-Host ""
