#!/usr/bin/env nu
# Comprehensive system validation script
# Validates all components: Nickel schemas, Rust crates, TOML configs, documentation
# Usage: nu scripts/validate-system.nu

def main [] {
    print "═══════════════════════════════════════════════════════════════"
    print "PROVISIONING PLATFORM - COMPREHENSIVE SYSTEM VALIDATION"
    print "═══════════════════════════════════════════════════════════════"
    print ""

    let start_time = (date now)

    # Phase 1: Nickel Schema Files Exist
    print "PHASE 1: NICKEL SCHEMA FILES"
    print "──────────────────────────────────────────────────────────────"
    let schema_files = [
        "vault-service.ncl",
        "extension-registry.ncl",
        "rag.ncl",
        "ai-service.ncl",
        "provisioning-daemon.ncl",
        "orchestrator.ncl",
        "control-center.ncl",
        "mcp-server.ncl",
        "installer.ncl",
    ]

    let schemas_passed = (
        $schema_files
        | each { |schema|
            let schema_path = $"provisioning/.typedialog/provisioning/platform/schemas/($schema)"
            if ($schema_path | path exists) {
                print $"✓ ($schema)"
                1
            } else {
                print $"✗ ($schema) - NOT FOUND"
                0
            }
        }
        | math sum
    )
    let schemas_failed = ($schema_files | length) - $schemas_passed

    print ""
    print "PHASE 2: RUST CRATE DEPENDENCIES"
    print "──────────────────────────────────────────────────────────────"
    let crates = [
        ["vault-service", "provisioning/platform/crates/vault-service/Cargo.toml"],
        ["extension-registry", "provisioning/platform/crates/extension-registry/Cargo.toml"],
        ["provisioning-rag", "provisioning/platform/crates/rag/Cargo.toml"],
        ["ai-service", "provisioning/platform/crates/ai-service/Cargo.toml"],
        ["provisioning-daemon", "provisioning/platform/crates/provisioning-daemon/Cargo.toml"],
        ["orchestrator", "provisioning/platform/crates/orchestrator/Cargo.toml"],
        ["control-center", "provisioning/platform/crates/control-center/Cargo.toml"],
        ["mcp-server", "provisioning/platform/crates/mcp-server/Cargo.toml"],
    ]

    let crates_passed = (
        $crates
        | each { |crate|
            let crate_name = $crate.0
            let crate_file = $crate.1
            if ($crate_file | path exists) {
                print $"✓ ($crate_name)"
                1
            } else {
                print $"✗ ($crate_name) - Cargo.toml NOT FOUND"
                0
            }
        }
        | math sum
    )
    let crates_failed = ($crates | length) - $crates_passed

    print ""
    print "PHASE 3: GENERATED TOML CONFIG FILES"
    print "──────────────────────────────────────────────────────────────"
    let config_path = "provisioning/platform/config"
    let config_count = (
        if ($config_path | path exists) {
            (ls $config_path | length)
        } else {
            0
        }
    )

    let config_passed = (
        if $config_count > 0 {
            print $"✓ Found ($config_count) TOML config files"
            if $config_count == 36 {
                print "✓ Config count matches expected: 36 (9 services × 4 modes)"
                1
            } else {
                print $"⚠ Config count mismatch: expected 36, found ($config_count)"
                1
            }
        } else {
            print "✗ No TOML config files found"
            0
        }
    )

    print ""
    print "PHASE 4: TYPEDIALOG FORM FILES"
    print "──────────────────────────────────────────────────────────────"
    let forms_path = "provisioning/.typedialog/provisioning/platform/forms"
    let form_count = (
        if ($forms_path | path exists) {
            (ls $forms_path | length)
        } else {
            0
        }
    )

    let form_passed = (
        if $form_count > 0 {
            print $"✓ Found ($form_count) TypeDialog form files"
            1
        } else {
            print "✗ No TypeDialog form files found"
            0
        }
    )

    let fragment_count = (
        if ("provisioning/.typedialog/provisioning/platform/forms/fragments" | path exists) {
            (glob "provisioning/.typedialog/provisioning/platform/forms/fragments/**/*.toml" | length)
        } else {
            0
        }
    )

    let fragment_passed = (
        if $fragment_count > 0 {
            print $"✓ Found ($fragment_count) form fragment files"
            1
        } else {
            print "⚠ No form fragment files found"
            1
        }
    )

    print ""
    print "PHASE 5: OPERATOR DOCUMENTATION"
    print "──────────────────────────────────────────────────────────────"
    let doc_files = [
        "provisioning/docs/src/operations/deployment-guide.md",
        "provisioning/docs/src/quick-reference/platform-operations-cheatsheet.md",
        "provisioning/docs/src/operations/monitoring-alerting-setup.md",
        "provisioning/docs/src/operations/incident-response-runbooks.md",
    ]

    let docs_passed = (
        $doc_files
        | each { |doc|
            if ($doc | path exists) {
                let lines = (open $doc | split row "\n" | length)
                print $"✓ ($doc) - ($lines) lines"
                1
            } else {
                print $"✗ ($doc) - NOT FOUND"
                0
            }
        }
        | math sum
    )
    let docs_failed = ($doc_files | length) - $docs_passed

    print ""
    print "PHASE 6: ENVIRONMENT VARIABLE OVERRIDE CAPABILITY"
    print "──────────────────────────────────────────────────────────────"
    let override_patterns = [
        "VAULT_",
        "REGISTRY_",
        "RAG_",
        "AI_SERVICE_",
        "DAEMON_",
    ]

    print "✓ Service env var patterns defined:"
    for pattern in $override_patterns {
        print $"  - ($pattern){{SECTION}}_{{KEY}}"
    }

    let env_passed = 1

    print ""
    print "PHASE 7: CONSTRAINT SYSTEM"
    print "──────────────────────────────────────────────────────────────"
    let constraints_file = "provisioning/.typedialog/provisioning/platform/constraints/constraints.toml"

    let constraint_passed = (
        if ($constraints_file | path exists) {
            print $"✓ Constraints file exists"
            1
        } else {
            print "✗ Constraints file not found"
            0
        }
    )

    # Aggregate results
    let total_passed = $schemas_passed + $crates_passed + $config_passed + $form_passed + $fragment_passed + $docs_passed + $env_passed + $constraint_passed
    let total_failed = $schemas_failed + $crates_failed + (if $config_passed == 1 { 0 } else { 1 }) + (if $form_passed == 1 { 0 } else { 1 }) + (if $docs_failed == 0 { 0 } else { $docs_failed }) + (if $constraint_passed == 1 { 0 } else { 1 })

    print ""
    print "═══════════════════════════════════════════════════════════════"
    print "VALIDATION SUMMARY"
    print "═══════════════════════════════════════════════════════════════"

    let total = $total_passed + $total_failed
    let pass_rate = (
        if $total == 0 { 0 } else { ($total_passed / $total * 100 | math round --precision 1) }
    )

    print $"Total Checks: ($total)"
    print $"Passed: ($total_passed)"
    print $"Failed: ($total_failed)"
    print $"Pass Rate: ($pass_rate)%"

    let end_time = (date now)
    let duration = ($end_time - $start_time)
    print $"Duration: ($duration | into duration)"

    print ""
    if $total_failed == 0 {
        print "✅ ALL VALIDATION CHECKS PASSED"
        print ""
        print "System is ready for:"
        print "  • Production deployment"
        print "  • Integration testing"
        print "  • Operator runbook execution"
        exit 0
    } else {
        print "❌ VALIDATION FAILED - CHECK ITEMS ABOVE"
        exit 1
    }
}

main
