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 = @()
$testReport = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\testing\test-report.md")
$currentTask = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md")
$verifyMatrix = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\testing\verify-matrix.md")

if ($null -eq $testReport) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/testing/test-report.md" "file" "缺少 test-report 模板。" }
elseif (-not (Test-ContainsAll $testReport @("需求完成情况", "修改文件清单", "核心实现说明", "影响范围", "未测试内容", "已知风险", "回滚方式", "逐条对照需求"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/testing/test-report.md" "acceptance-report" "test-report 模板缺少第24章要求的验收报告字段。" }
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 @("交付验收报告路径", "逐条需求完成情况"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" "acceptance-fields" "CURRENT_TASK 模板缺少第24章要求的交付报告追踪字段。" }
if ($null -eq $verifyMatrix) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/testing/verify-matrix.md" "file" "缺少 verify-matrix 模板。" }
elseif (-not (Test-ContainsAll $verifyMatrix @("交付验收报告", "用户验收链路", "失败态验收"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/testing/verify-matrix.md" "delivery-verification" "verify-matrix 模板缺少第24章要求的交付验收闭环。" }

$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 "Acceptance lint passed."; exit 0 }
if ($errors.Count -gt 0) { Write-Output "Acceptance 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 "Acceptance lint passed with warnings." }; foreach ($issue in $warnings) { Write-Output ("- [WARNING] {0} :: {1} :: {2}" -f $issue.file, $issue.section, $issue.message) } }
exit $exitCode
