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 = @()

$frontendQualityPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\frontend-quality-rules.md"
$screenStatesPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\design\screen-states.md"
$designBriefPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\design\design-brief.md"
$imageSpecPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\design\image-to-frontend-spec.md"
$currentTaskPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md"

$frontendQuality = Get-FileContentOrNull -Path $frontendQualityPath
$screenStates = Get-FileContentOrNull -Path $screenStatesPath
$designBrief = Get-FileContentOrNull -Path $designBriefPath
$imageSpec = Get-FileContentOrNull -Path $imageSpecPath
$currentTask = Get-FileContentOrNull -Path $currentTaskPath

if ($null -eq $frontendQuality) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md" -Section "file" -Message "缺少 frontend-quality-rules 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $frontendQuality -Terms @("设计系统", "移动端和嵌入式 WebView 额外约束", "安全区策略", "键盘弹起后", "表单体验至少补充", "submitting", "retry", "offline"))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/frontend-quality-rules.md" -Section "ui-quality-rules" -Message "frontend-quality-rules 模板缺少第10章要求的设计系统、移动端安全区、表单体验或补充交互状态约束。"
  }
}

if ($null -eq $screenStates) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/design/screen-states.md" -Section "file" -Message "缺少 screen-states 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $screenStates -Terms @("### submitting", "### retry", "### offline", "安全区策略", "键盘遮挡处理"))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/design/screen-states.md" -Section "state-coverage" -Message "screen-states 模板缺少第10章要求的 submitting/retry/offline 或移动端安全区状态。"
  }
}

if ($null -eq $designBrief) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/design/design-brief.md" -Section "file" -Message "缺少 design-brief 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $designBrief -Terms @("submitting", "retry", "offline", "必须说明安全区、键盘弹起", "安全区 / 键盘遮挡策略"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/design/design-brief.md" -Section "mobile-form-rules" -Message "design-brief 模板还没有完整承接第10章的移动端安全区或表单提交体验。"
  }
}

if ($null -eq $imageSpec) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/design/image-to-frontend-spec.md" -Section "file" -Message "缺少 image-to-frontend-spec 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $imageSpec -Terms @("| disabled |", "| submitting |", "| retry |", "| offline |", "## 5.3 表单与安全区规格", "安全区 / 键盘遮挡处理"))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/design/image-to-frontend-spec.md" -Section "interaction-spec" -Message "image-to-frontend-spec 模板缺少第10章要求的状态、表单或安全区实现规格。"
  }
}

if ($null -eq $currentTask) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" -Section "file" -Message "缺少 CURRENT_TASK 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $currentTask -Terms @("设计系统 / token 约束", "移动端安全区 / 键盘遮挡处理", "表单校验与提交流程策略"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" -Section "ui-fields" -Message "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 "UI lint passed."
  exit 0
}

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

exit $exitCode
