$ErrorActionPreference = "Stop"

Write-Host "RepoKey customer package self test"
Write-Host ""

$root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
$failed = $false
$warned = $false

function Ok($msg) { Write-Host "[OK] $msg" }
function Warn($msg) { Write-Host "[WARN] $msg"; $script:warned = $true }
function Fail($msg) { Write-Host "[FAIL] $msg"; $script:failed = $true }
function CheckPath($label, $path, [bool]$required = $true) {
    if (Test-Path -LiteralPath $path) { Ok $label }
    elseif ($required) { Fail "$label missing: $path" }
    else { Warn "$label missing: $path" }
}

CheckPath "client exe" (Join-Path $root "bin\repokey\repokey.exe") $true
CheckPath "repokey-client.cmd" (Join-Path $root "repokey-client.cmd") $true
CheckPath "license file" (Join-Path $root "licenses\repokey-license.txt") $false
CheckPath "AI template" (Join-Path $root "docs\AI_AGENT_START_HERE.template.md") $true
CheckPath "AI local file" (Join-Path $root "AI_AGENT_START_HERE.local.md") $false
CheckPath "template variables doc" (Join-Path $root "docs\TEMPLATE_VARIABLES.md") $true
CheckPath "customer install flow" (Join-Path $root "docs\CUSTOMER_INSTALL_FLOW.md") $true
CheckPath "project init script" (Join-Path $root "install\init_project.ps1") $true
CheckPath "render script" (Join-Path $root "install\render_ai_start.ps1") $true

$aiStack = Join-Path $root "tools\ai_vitis_vivado_stack"
CheckPath "AI stack root" $aiStack $false
CheckPath "PL-PS Handoff package" (Join-Path $aiStack "pl_ps_handoff") $false
CheckPath "Vitis MCP package" (Join-Path $aiStack "vitis_mcp") $false
CheckPath "PL QoR MCP package" (Join-Path $aiStack "pl_qor_mcp") $false
CheckPath "PL optimizer compat tools" (Join-Path $aiStack "pl_optimizer_compat") $false
CheckPath "Vivado Debug MCP package" (Join-Path $aiStack "vivado_debug_mcp") $false
CheckPath "Vivado Context MCP package" (Join-Path $aiStack "vivado_context_mcp") $false
CheckPath "AMD Doc MCP registration template" (Join-Path $aiStack "MCP_REGISTRATION_TEMPLATE.json") $false

$tempRoot = Join-Path $env:TEMP ("repokey_selftest_" + [Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $tempRoot | Out-Null
try {
    $rendered = Join-Path $tempRoot "AI_AGENT_START_HERE.local.md"
    & (Join-Path $root "install\render_ai_start.ps1") -ProjectRoot $tempRoot -OutputPath $rendered | Out-Null
    if (Test-Path -LiteralPath $rendered) {
        $content = Get-Content -LiteralPath $rendered -Raw
        if ($content -match [regex]::Escape($tempRoot) -and $content -notmatch "\{\{PROJECT_ROOT\}\}") {
            Ok "AI start template renders"
        } else {
            Fail "AI start template rendered but placeholders remain or project root is missing"
        }
    } else {
        Fail "AI start template did not render"
    }
} finally {
    Remove-Item -LiteralPath $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
}

$client = Join-Path $root "repokey-client.cmd"
if (Test-Path -LiteralPath $client) {
    try {
        & $client build-info | Out-Null
        Ok "repokey-client build-info starts"
    } catch {
        Warn "repokey-client build-info did not complete: $($_.Exception.Message)"
    }
}

Write-Host ""
if ($failed) {
    Write-Host "Self test failed"
    exit 1
}
if ($warned) {
    Write-Host "Self test finished with warnings"
    exit 0
}
Write-Host "Self test finished"
