param(
  [string]$SmokeOutput
)

$ErrorActionPreference = 'Stop'

$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
$sourcePath = Join-Path $repoRoot 'build\scripts\project-bundle-freshness-smoke.ts'
$tempRoot = Join-Path $repoRoot '.tmp\project-bundle-freshness-smoke'
$compileRoot = Join-Path $tempRoot 'compiled'
$entryPath = Join-Path $compileRoot 'build\scripts\project-bundle-freshness-smoke.js'
$compilePackagePath = Join-Path $compileRoot 'package.json'
$bundleRootA = Join-Path $tempRoot 'bundle-src-a'
$bundleRootB = Join-Path $tempRoot 'bundle-src-b'
$bundleZipPathA = Join-Path $tempRoot 'bundle-a.zip'
$bundleZipPathB = Join-Path $tempRoot 'bundle-b.zip'
$resolvedResultPath = if ($SmokeOutput) { [System.IO.Path]::GetFullPath($SmokeOutput) } else { Join-Path $tempRoot 'result.json' }

function Write-Utf8File {
  param([string]$FilePath, [string]$Content)
  $encoding = New-Object System.Text.UTF8Encoding $false
  [System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($FilePath)) | Out-Null
  [System.IO.File]::WriteAllText($FilePath, $Content, $encoding)
}

if (-not (Test-Path $sourcePath)) {
  throw "Project bundle freshness smoke source was not found: $sourcePath"
}

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

$compileArgs = @(
  'pnpm',
  '--dir', (Join-Path $repoRoot 'apps\desktop'),
  'exec',
  'tsc',
  '--module', 'ES2022',
  '--moduleResolution', 'node',
  '--target', 'ES2022',
  '--lib', 'ES2022',
  '--types', 'node',
  '--esModuleInterop',
  '--allowSyntheticDefaultImports',
  '--skipLibCheck',
  '--outDir', $compileRoot,
  $sourcePath
)

Write-Host 'Compiling project-bundle freshness smoke with local TypeScript'
corepack @compileArgs
if ($LASTEXITCODE -ne 0) {
  exit $LASTEXITCODE
}

if (-not (Test-Path $entryPath)) {
  throw "Project bundle freshness smoke entry was not emitted: $entryPath"
}

foreach ($path in @($bundleRootA, $bundleRootB)) {
  if (Test-Path $path) {
    Remove-Item $path -Recurse -Force
  }
}
foreach ($path in @($bundleZipPathA, $bundleZipPathB)) {
  if (Test-Path $path) {
    Remove-Item $path -Force
  }
}

foreach ($root in @($bundleRootA, $bundleRootB)) {
  New-Item -ItemType Directory -Path (Join-Path $root 'memory') -Force | Out-Null
}

Write-Utf8File -FilePath (Join-Path $bundleRootA 'project.json') -Content (@{
  id = 'bundle-freshness-smoke'
  name = 'Bundle Freshness Smoke'
  version = '1.0.0'
  description = 'Verifies bundle freshness detection for same URL and config version.'
} | ConvertTo-Json -Depth 5)
Write-Utf8File -FilePath (Join-Path $bundleRootA 'README.md') -Content '# Bundle Freshness Smoke`n`nFreshness variant A'
Write-Utf8File -FilePath (Join-Path $bundleRootA 'memory\summary.md') -Content 'bundle freshness variant a'

Write-Utf8File -FilePath (Join-Path $bundleRootB 'project.json') -Content (@{
  id = 'bundle-freshness-smoke'
  name = 'Bundle Freshness Smoke'
  version = '1.0.0'
  description = 'Verifies bundle freshness detection for same URL and config version.'
} | ConvertTo-Json -Depth 5)
Write-Utf8File -FilePath (Join-Path $bundleRootB 'README.md') -Content '# Bundle Freshness Smoke`n`nFreshness variant B'
Write-Utf8File -FilePath (Join-Path $bundleRootB 'memory\summary.md') -Content 'bundle freshness variant b'

Compress-Archive -Path (Join-Path $bundleRootA '*') -DestinationPath $bundleZipPathA -Force
Compress-Archive -Path (Join-Path $bundleRootB '*') -DestinationPath $bundleZipPathB -Force

Write-Utf8File -FilePath $compilePackagePath -Content '{"type":"module"}'

Write-Host 'Running project-bundle freshness smoke'
node $entryPath $resolvedResultPath $bundleZipPathA $bundleZipPathB
if ($LASTEXITCODE -ne 0) {
  exit $LASTEXITCODE
}

if (-not (Test-Path $resolvedResultPath)) {
  throw "Project bundle freshness smoke did not produce a result file: $resolvedResultPath"
}
