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
}

function Test-ContainsAny {
  param(
    [string]$Content,
    [string[]]$Terms
  )

  foreach ($term in $Terms) {
    if ($Content -match [regex]::Escape($term)) {
      return $true
    }
  }

  return $false
}

$issues = @()

$styleDocPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\code-style-and-naming.md"
$currentTaskPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md"
$projectAgentsTemplatePath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\project-agents-template.md"
$codingStyleRulesPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\config\rules\coding-style.md"

$styleDoc = Get-FileContentOrNull -Path $styleDocPath
$currentTask = Get-FileContentOrNull -Path $currentTaskPath
$projectAgentsTemplate = Get-FileContentOrNull -Path $projectAgentsTemplatePath
$codingStyleRules = Get-FileContentOrNull -Path $codingStyleRulesPath

if ($null -eq $styleDoc) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/code-style-and-naming.md" -Section "file" -Message "缺少代码风格与命名模板，无法承载第五章规则。"
}
else {
  if (-not (Test-ContainsAll -Content $styleDoc -Terms @("业务词典", "文件命名规则", "函数命名规则", "类型命名边界", "新增命名前必须确认"))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/code-style-and-naming.md" -Section "core-sections" -Message "代码风格模板缺少业务词典、文件命名规则、函数命名规则、类型命名边界或命名前确认项。"
  }
  if (-not (Test-ContainsAny -Content $styleDoc -Terms @("doAction", "processData", "dealData", "getInfo", "check"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/docs/code-style-and-naming.md" -Section "bad-examples" -Message "代码风格模板还没有列出典型的模糊命名反例。"
  }
}

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 @("本任务涉及的业务名词统一叫法", "禁止混用的同义词", "新增 / 修改文件命名模式", "新增类型命名边界"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" -Section "style-fields" -Message "CURRENT_TASK 模板缺少业务词典、同义词禁用、文件命名模式或类型边界记录项。"
  }
}

if ($null -eq $projectAgentsTemplate) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/project-agents-template.md" -Section "file" -Message "缺少 project AGENTS 模板。"
}
else {
  if (-not (Test-ContainsAny -Content $projectAgentsTemplate -Terms @("code-style-and-naming", "docs/code-style.md", "命名"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/docs/project-agents-template.md" -Section "naming-index" -Message "project AGENTS 模板还没有把命名规范文档纳入入口索引。"
  }
}

if ($null -eq $codingStyleRules) {
  $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/config/rules/coding-style.md" -Section "file" -Message "缺少 always-on coding-style 规则文件。"
}
else {
  if (-not (Test-ContainsAny -Content $codingStyleRules -Terms @("业务词典", "命名", "code-style-and-naming"))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/config/rules/coding-style.md" -Section "naming-bridge" -Message "always-on coding-style 规则还没有把项目级命名约束桥接到详细文档。"
  }
}

$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 "Style lint passed."
  exit 0
}

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

exit $exitCode
