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 = @()
$dataModel = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\spec\10-data-model.md")
$architecture = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\docs\architecture-constraints.md")
$currentTask = Get-FileContentOrNull (Join-Path $ProjectRoot "docs\codex-harness-engineering\templates\context\CURRENT_TASK.md")

if ($null -eq $dataModel) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/spec/10-data-model.md" "file" "缺少 10-data-model 模板。" }
elseif (-not (Test-ContainsAll $dataModel @("schema_or_migration_required", "transaction_sensitive_writes", "migration_plan", "historical_data_compatibility", "rollback_strategy"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/spec/10-data-model.md" "migration-fields" "10-data-model 模板缺少第21章要求的 migration、历史数据和回滚字段。" }
if ($null -eq $architecture) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/architecture-constraints.md" "file" "缺少 architecture-constraints 模板。" }
elseif (-not (Test-ContainsAll $architecture @("schema_or_migration_required", "historical_data_strategy", "migration_rollback_strategy", "consistency_requirements"))) { $issues += New-Issue "error" "docs/codex-harness-engineering/templates/docs/architecture-constraints.md" "data-boundary" "architecture-constraints 模板缺少第21章要求的数据迁移和历史数据约束。" }
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 @("schema / migration 是否变更", "migration 文件 / 目录", "历史数据兼容 / 回填策略"))) { $issues += New-Issue "warning" "docs/codex-harness-engineering/templates/context/CURRENT_TASK.md" "data-change-fields" "CURRENT_TASK 模板缺少第21章要求的数据变更记录字段。" }

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