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

$currentTaskPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md"
$taskSessionPath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\task-session-strategy.md"
$agentsRulePath = Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\config\rules\agents.md"

$currentTask = Get-FileContentOrNull -Path $currentTaskPath
$taskSession = Get-FileContentOrNull -Path $taskSessionPath
$agentsRule = Get-FileContentOrNull -Path $agentsRulePath

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 @('本轮已完成内容', '本轮未完成内容 / 剩余问题', '本轮修改文件', '为什么这样改 / 关键决策', '已通过测试与证据', '未运行测试与原因', '下一个 Agent 建议起点', '本轮避免并行修改的文件 / 目录'))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" -Section "handoff-fields" -Message "CURRENT_TASK 模板缺少第19章要求的交接字段：完成项、剩余问题、修改文件、关键决策、测试结果和下一步。"
  }
}

if ($null -eq $taskSession) {
  $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/task-session-strategy.md" -Section "file" -Message "缺少 task-session-strategy 模板。"
}
else {
  if (-not (Test-ContainsAll -Content $taskSession -Terms @('一任务一会话', 'CURRENT_TASK.md', '已完成内容', '修改文件', '下一步', '不要让多个 Agent 同时改同一文件'))) {
    $issues += New-Issue -Severity "error" -File "docs/codex-harness-engineering/templates/docs/task-session-strategy.md" -Section "session-handoff" -Message "task-session-strategy 模板缺少第19章要求的一任务一会话、交接和同文件隔离规则。"
  }
}

if ($null -eq $agentsRule) {
  $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/config/rules/agents.md" -Section "file" -Message "缺少 always_on agents 规则模板。"
}
else {
  if (-not (Test-ContainsAll -Content $agentsRule -Terms @('不要让两个 agent 同时改同一个文件', 'CURRENT_TASK.md', '已完成内容', '未完成内容', '修改文件', '下一步'))) {
    $issues += New-Issue -Severity "warning" -File "docs/codex-harness-engineering/templates/config/rules/agents.md" -Section "always-on-collaboration-rules" -Message "agents 规则模板还没有完整承接第19章的固定协作规则和交接刷新要求。"
  }
}

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

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

exit $exitCode
