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 = @()
$runtimeAgents = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\runtime\AGENTS.md")
$projectAgentsTemplate = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\project-agents-template.md")
$newProjectUsage = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\new-project-usage.md")
$currentTask = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md")

if ($null -eq $runtimeAgents) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/runtime/AGENTS.md" "file" "缺少 runtime AGENTS 模板。" }
elseif (-not (Test-ContainsAll $runtimeAgents @("docs/ai/repo-map.md", "docs/ai/CURRENT_TASK.md", "docs/harness/rule-governance.md"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/runtime/AGENTS.md" "doc-index" "runtime AGENTS 模板缺少第20章要求的核心文档入口索引。" }
if ($null -eq $projectAgentsTemplate) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/project-agents-template.md" "file" "缺少 project-agents-template 模板。" }
elseif (-not (Test-ContainsAll $projectAgentsTemplate @("README.md", "docs/architecture.md", "docs/code-style.md", "docs/api-convention.md", "docs/component-map.md", "docs/test-strategy.md", "docs/current-task.md"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/project-agents-template.md" "core-doc-set" "project AGENTS 模板缺少第20章要求的最小核心文档集。" }
if ($null -eq $newProjectUsage) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/docs/new-project-usage.md" "file" "缺少 new-project-usage 模板。" }
elseif (-not (Test-ContainsAll $newProjectUsage @("repo-map.md", "CURRENT_TASK.md", "AGENTS.md", "README.md"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/docs/new-project-usage.md" "bootstrap-doc-set" "new-project-usage 模板还没有把第20章要求的项目文档最小集合讲清。" }
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" "doc-update-fields" "CURRENT_TASK 模板缺少文档更新和交付报告字段。" }

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