param(
  [string]$OutputDirectory = "dist\\video-haus-dropin",
  [string]$ZipFilePath = "dist\\video-haus-dropin.zip"
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
$resolvedOutputDirectory = Join-Path $projectRoot $OutputDirectory
$resolvedZipFilePath = Join-Path $projectRoot $ZipFilePath

Push-Location $projectRoot
try {
  Write-Host "Building production app..." -ForegroundColor Cyan
  npm run build

  if (Test-Path -LiteralPath $resolvedOutputDirectory) {
    Remove-Item -LiteralPath $resolvedOutputDirectory -Recurse -Force
  }
  New-Item -ItemType Directory -Path $resolvedOutputDirectory -Force | Out-Null

  Write-Host "Copying standalone runtime..." -ForegroundColor Cyan
  Copy-Item -Path ".next\\standalone\\*" -Destination $resolvedOutputDirectory -Recurse -Force

  # Keep the bundle lean even if standalone tracing accidentally includes the full repo.
  $allowedTopLevelEntries = @(".next", "node_modules", "public", "server.js", "package.json")
  Get-ChildItem -LiteralPath $resolvedOutputDirectory -Force | ForEach-Object {
    if ($allowedTopLevelEntries -notcontains $_.Name) {
      Remove-Item -LiteralPath $_.FullName -Recurse -Force
    }
  }

  $outputNextDirectory = Join-Path $resolvedOutputDirectory ".next"
  $outputStaticDirectory = Join-Path $outputNextDirectory "static"
  New-Item -ItemType Directory -Path $outputStaticDirectory -Force | Out-Null
  Copy-Item -Path ".next\\static\\*" -Destination $outputStaticDirectory -Recurse -Force

  Write-Host "Copying public assets..." -ForegroundColor Cyan
  if (Test-Path -LiteralPath (Join-Path $resolvedOutputDirectory "public")) {
    Remove-Item -LiteralPath (Join-Path $resolvedOutputDirectory "public") -Recurse -Force
  }
  Copy-Item -Path "public" -Destination (Join-Path $resolvedOutputDirectory "public") -Recurse -Force

  $envExamplePath = Join-Path $resolvedOutputDirectory ".env.example"
  $envTemplate = @"
# Required
NODE_ENV=production
PORT=3000
HOSTNAME=0.0.0.0

# Recommended for SEO
NEXT_PUBLIC_SITE_URL=https://video.haus
SITE_URL=https://video.haus

# Optional binary overrides
FFMPEG_PATH=
FFPROBE_PATH=
YT_DLP_PATH=
YT_DLP_COOKIES_PATH=
INSTAGRAM_COOKIES_PATH=
YT_DLP_PYTHON=

# Optional media processing tuning
VIDEO_HAUS_TEMP_ROOT=.tmp/media
VIDEO_HAUS_MAX_UPLOAD_SIZE_BYTES=1000000000
VIDEO_HAUS_COMMAND_TIMEOUT_MS=120000
VIDEO_HAUS_CLEANUP_MAX_AGE_MS=1800000
"@
  Set-Content -LiteralPath $envExamplePath -Encoding utf8 -Value $envTemplate

  # Ship a ready-to-run default .env so uploads can start without extra edits.
  $envPath = Join-Path $resolvedOutputDirectory ".env"
  Set-Content -LiteralPath $envPath -Encoding utf8 -Value $envTemplate

  # Provide common startup entry names expected by many shared hosts.
  $appJsPath = Join-Path $resolvedOutputDirectory "app.js"
  Set-Content -LiteralPath $appJsPath -Encoding ascii -Value "require('./server.js')`n"

  $indexJsPath = Join-Path $resolvedOutputDirectory "index.js"
  Set-Content -LiteralPath $indexJsPath -Encoding ascii -Value "require('./server.js')`n"

  $passengerPath = Join-Path $resolvedOutputDirectory "passenger_startup.js"
  Set-Content -LiteralPath $passengerPath -Encoding ascii -Value "require('./server.js')`n"

  $startCmdPath = Join-Path $resolvedOutputDirectory "start.cmd"
  Set-Content -LiteralPath $startCmdPath -Encoding ascii -Value "@echo off`r`nset NODE_ENV=production`r`nnode server.js`r`n"

  $startShPath = Join-Path $resolvedOutputDirectory "start.sh"
  Set-Content -LiteralPath $startShPath -Encoding ascii -Value "#!/usr/bin/env sh`nexport NODE_ENV=production`nnode server.js`n"

  $deployReadmePath = Join-Path $resolvedOutputDirectory "DEPLOY.txt"
  Set-Content -LiteralPath $deployReadmePath -Encoding utf8 -Value @"
video.haus drop-in bundle
=========================

1) Upload/extract this folder on your Node hosting.
2) Start with:
   - Windows: start.cmd
   - Linux: sh start.sh
   - Or directly: node server.js (or app.js/index.js/passenger_startup.js)

This bundle already contains:
- standalone Next.js server
- runtime dependencies
- ffmpeg/ffprobe/yt-dlp binaries
- a prefilled .env for immediate startup
"@

  if (Test-Path -LiteralPath $resolvedZipFilePath) {
    Remove-Item -LiteralPath $resolvedZipFilePath -Force
  }

  $itemsToZip = Get-ChildItem -LiteralPath $resolvedOutputDirectory -Force
  Compress-Archive -LiteralPath $itemsToZip.FullName -DestinationPath $resolvedZipFilePath -CompressionLevel Optimal -Force

  Write-Host "Bundle created successfully." -ForegroundColor Green
  Write-Host "Folder: $resolvedOutputDirectory" -ForegroundColor Green
  Write-Host "Zip:    $resolvedZipFilePath" -ForegroundColor Green
} finally {
  Pop-Location
}
