param(
  [string]$ProjectRoot = "",
  [switch]$AsJson,
  [switch]$AllowWarningsOnly
)

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

if ([string]::IsNullOrWhiteSpace($ProjectRoot)) {
  $ProjectRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "..\..\..\.."))
}

function New-Issue { param([string]$Severity,[string]$File,[string]$Section,[string]$Message) [PSCustomObject]@{ severity=$Severity; file=$File; section=$Section; message=$Message } }
function Get-FileContentOrNull { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { return $null }; Get-Content -LiteralPath $Path -Raw }
function Test-ContainsAll { param([string]$Content,[string[]]$Terms) foreach ($term in $Terms) { if ($Content -notmatch [regex]::Escape($term)) { return $false } }; return $true }

$issues = @()
$serviceMatrix = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\service-dependency-matrix.md")
$currentTask = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md")
$demoReadme = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\demo-readme.md")

if ($null -eq $serviceMatrix) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/service-dependency-matrix.md" "file" "缺少 service-dependency-matrix 模板。" }
elseif (-not (Test-ContainsAll $serviceMatrix @("callback_webhook", "credential_source", "retry_idempotency", "rate_limit_or_quota", "failure_fallback"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/service-dependency-matrix.md" "integration-fields" "service-dependency-matrix 模板缺少第22章要求的第三方集成字段。" }
if ($null -eq $currentTask) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" "file" "缺少 CURRENT_TASK 模板。" }
elseif (-not (Test-ContainsAll $currentTask @("第三方服务 / SDK 依赖", "第三方回调 / Webhook / 验签策略", "凭据 / Secret 注入位置", "上传约束（大小 / 类型 / 扫描 / 权限 / CDN）"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" "integration-task-fields" "CURRENT_TASK 模板缺少第22章要求的第三方集成记录字段。" }
if ($null -eq $demoReadme) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/docs/demo-readme.md" "file" "缺少 demo-readme 模板。" }
elseif (-not (Test-ContainsAll $demoReadme @("支付 / 订单", "推送 / 短信", "接真实支付、签名、回调、失败重试"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/docs/demo-readme.md" "demo-boundary" "demo-readme 模板还没有完整标出第22章要求的真实第三方边界。" }

$errors = @($issues | Where-Object severity -eq "error")
$warnings = @($issues | Where-Object severity -eq "warning")
$exitCode = if ($errors.Count -gt 0) { 1 } elseif ($warnings.Count -gt 0 -and -not $AllowWarningsOnly) { 2 } else { 0 }
if ($AsJson) { [PSCustomObject]@{ ok = ($exitCode -eq 0); errors = $errors; warnings = $warnings } | ConvertTo-Json -Depth 6; exit $exitCode }
if ($errors.Count -eq 0 -and $warnings.Count -eq 0) { Write-Output "Integration lint passed."; exit 0 }
if ($errors.Count -gt 0) { Write-Output "Integration lint failed."; foreach ($issue in $errors) { Write-Output ("- [ERROR] {0} :: {1} :: {2}" -f $issue.file, $issue.section, $issue.message) } }
if ($warnings.Count -gt 0) { if ($errors.Count -eq 0) { Write-Output "Integration lint passed with warnings." }; foreach ($issue in $warnings) { Write-Output ("- [WARNING] {0} :: {1} :: {2}" -f $issue.file, $issue.section, $issue.message) } }
exit $exitCode
