param(
  [string]$SmokeOutput
)

$ErrorActionPreference = 'Stop'

$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
$sourcePath = Join-Path $repoRoot 'build\scripts\project-bundle-reconcile-smoke.ts'
$tempRoot = Join-Path $repoRoot '.tmp\project-bundle-reconcile-smoke'
$compileRoot = Join-Path $tempRoot 'compiled'
$entryPath = Join-Path $compileRoot 'build\scripts\project-bundle-reconcile-smoke.js'
$compilePackagePath = Join-Path $compileRoot 'package.json'
$bundleRoot = Join-Path $tempRoot 'bundle-src'
$bundleZipPath = Join-Path $tempRoot 'bundle.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 reconcile 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 reconcile smoke with local TypeScript'
corepack @compileArgs
if ($LASTEXITCODE -ne 0) {
  exit $LASTEXITCODE
}

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

if (Test-Path $bundleRoot) {
  Remove-Item $bundleRoot -Recurse -Force
}
if (Test-Path $bundleZipPath) {
  Remove-Item $bundleZipPath -Force
}
New-Item -ItemType Directory -Path (Join-Path $bundleRoot 'skills\bundle-reconcile-skill') -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $bundleRoot 'memory') -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $bundleRoot 'cron') -Force | Out-Null
Write-Utf8File -FilePath (Join-Path $bundleRoot 'project.json') -Content (@{
  id = 'bundle-reconcile-smoke'
  name = 'Bundle Reconcile Smoke'
  version = '1.0.0'
  description = 'Verifies stale bundle cleanup and reconciliation.'
} | ConvertTo-Json -Depth 5)
Write-Utf8File -FilePath (Join-Path $bundleRoot 'README.md') -Content '# Bundle Reconcile Smoke'
Write-Utf8File -FilePath (Join-Path $bundleRoot 'memory\summary.md') -Content 'Bundle reconcile smoke memory marker.'
Write-Utf8File -FilePath (Join-Path $bundleRoot 'skills\bundle-reconcile-skill\SKILL.md') -Content '# Bundle Reconcile Skill'
Write-Utf8File -FilePath (Join-Path $bundleRoot 'cron\bundle-reconcile-task.txt') -Content 'bundle cron smoke'
Compress-Archive -Path (Join-Path $bundleRoot '*') -DestinationPath $bundleZipPath -Force

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

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

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