#!/usr/bin/env nu
def main [] {
  let docs_root = "provisioning/docs/src"
  mut check_count = 0
  mut pass_count = 0

  print "📚 DOCUMENTATION QUALITY VERIFICATION"
  print ""

  # Check 1: Stub Files
  print "Checking for stub files..."
  $check_count = ($check_count + 1)
  let stubs = (glob $"($docs_root)/**/*.md" | where {|| (stat $in).size < 100 } | length)
  if ($stubs > 0) {
    print ("  ✗ Found " + ($stubs | into string) + " stub files")
  } else {
    print "  ✓ No stub files found"
    $pass_count = ($pass_count + 1)
  }

  # Check 2: .claude/ cross-references
  print "Checking for .claude/ cross-references..."
  $check_count = ($check_count + 1)
  let claude_refs = (^grep -r '\.claude/' $docs_root out+err> /dev/null | wc -l | str trim | into int)
  let claude_result = if ($claude_refs > 0) { "found" } else { "pass" }
  if ($claude_result == "found") {
    print "  ✗ Found .claude/ references"
  } else {
    print "  ✓ No .claude/ cross-references"
    $pass_count = ($pass_count + 1)
  }

  # Check 3: ADR naming
  print "Checking ADR naming convention..."
  $check_count = ($check_count + 1)
  let bad_adrs = (ls $"($docs_root)/architecture/adr" | where name =~ "^ADR-" | length)
  if ($bad_adrs > 0) {
    print ("  ✗ Found " + ($bad_adrs | into string) + " UPPERCASE ADR files")
  } else {
    print "  ✓ All ADRs follow lowercase-kebab-case"
    $pass_count = ($pass_count + 1)
  }

  # Check 4: AI documentation
  print "Checking AI documentation..."
  $check_count = ($check_count + 1)
  let ai_count = (ls $"($docs_root)/ai" | where type == "file" and ($it.name | str ends-with ".md") | length)
  if ($ai_count >= 10) {
    print ("  ✓ AI documentation complete " + ($ai_count | into string) + " files")
    $pass_count = ($pass_count + 1)
  } else {
    print ("  ⚠ AI documentation incomplete " + ($ai_count | into string) + " files")
  }

  # Check 5: KCL references
  print "Checking for KCL references..."
  $check_count = ($check_count + 1)
  let kcl_count = (^grep -ri 'kcl' $docs_root out+err> /dev/null | wc -l | str trim | into int)
  let kcl_result = if ($kcl_count < 50) { "pass" } else { "fail" }
  if ($kcl_result == "pass") {
    print "  ✓ KCL references acceptable (historical docs)"
    $pass_count = ($pass_count + 1)
  } else {
    print "  ⚠ Found many KCL references"
  }

  # Check 6: markdownlint-cli2
  print "Running markdownlint-cli2 validation..."
  $check_count = ($check_count + 1)
  let lint_result = (
    if (which markdownlint-cli2 | length) > 0 {
      ^markdownlint-cli2 --config provisioning/.markdownlint-cli2.jsonc $"($docs_root)/**/*.md" out+err> /dev/null
      "pass"
    } else { "skip" }
  )
  if ($lint_result == "pass") {
    print "  ✓ All files pass markdown linting"
    $pass_count = ($pass_count + 1)
  } else if ($lint_result == "fail") {
    print "  ✗ Linting failures found"
  } else {
    print "  ⚠ markdownlint-cli2 not installed (skipping)"
  }

  # Summary
  print ""
  if ($pass_count == $check_count) {
    print ("✓ ALL QUALITY CHECKS PASSED (" + ($pass_count | into string) + "/" + ($check_count | into string) + ")")
    print ""
    print "Documentation is production-ready! 🎉"
    exit 0
  } else {
    print ("✗ QUALITY CHECKS FAILED (" + ($pass_count | into string) + "/" + ($check_count | into string) + " passed)")
    exit 1
  }
}

main
