#!/usr/bin/env nu
# Documentation Audit Script
# Identifies quality issues in 201 documentation files

def main [] {
  let docs_root = "provisioning/docs/src"

  # Find all markdown files
  let all_files = (glob $"($docs_root)/**/*.md")

  let audit_results = (
    $all_files | par-each {|file|
      let content = (open $file | str trim)
      let size = ($file | stat | get size)
      let basename = ($file | path basename)

      # Detect issues
      let is_stub = ($size < 100)
      let kcl_refs = ($content | grep -i "kcl" | length)
      let claude_refs = ($content | grep '.claude/' | length)
      let adr_uppercase = ($basename =~ r'^ADR-')
      let bare_urls = ($content | grep -E r'https?://[^\[]*$' | length)

      {
        file: $file,
        basename: $basename,
        size: $size,
        status: (
          if $is_stub { "STUB" }
          else if ($claude_refs > 0) { "CROSS-REF-VIOLATION" }
          else if ($kcl_refs > 0 and ($file !~ "adr-010|adr-011")) { "KCL-DEPRECATED" }
          else if $adr_uppercase { "ADR-NAMING" }
          else if ($bare_urls > 0) { "BARE-URLS" }
          else { "OK" }
        ),
        kcl_count: $kcl_refs,
        claude_refs: $claude_refs,
        bare_urls: $bare_urls
      }
    }
  )

  # Output results
  $audit_results | to json
}

main
