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 = @()
$frontendQuality = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\frontend-quality-rules.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 $frontendQuality) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md" "file" "缺少 frontend-quality-rules 模板。" }
elseif (-not (Test-ContainsAll $frontendQuality @("iOS / Android 差异处理", "权限申请", "生命周期 / 中断恢复策略", "真机 / 系统版本矩阵"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md" "mobile-app-rules" "frontend-quality-rules 模板缺少第23章要求的平台差异、权限、生命周期或真机矩阵规则。" }
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 @("平台差异（iOS / Android / H5 / 小程序）", "权限申请与拒绝回退", "生命周期 / 中断恢复策略", "设备 / 系统版本验证矩阵"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" "mobile-fields" "CURRENT_TASK 模板缺少第23章要求的移动端专项字段。" }
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" "mobile-verify" "verify-matrix 模板缺少第23章要求的移动端验证矩阵。" }

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