#!/usr/bin/env nu

# Platform Configuration Validation Script
# Validates all platform service configuration files

def check-config [path: string] {
    if ($path | path exists) {
        try {
            open $path | ignore
            true
        } catch {
            false
        }
    } else {
        false
    }
}

def main [] {
    print "🔍 Validating Platform Service Configurations...\n"

    let services = [
        { name: "Orchestrator", path: "orchestrator", configs: ["config.defaults.toml", "config.schema.toml"], backup: null }
        { name: "Control Center", path: "control-center", configs: ["config.defaults.toml", "config.schema.toml"], backup: "config.toml.backup" }
        { name: "MCP Server", path: "mcp-server", configs: ["config.defaults.toml", "config.schema.toml"], backup: null }
    ]

    let config_checks = $services | each { |service|
        print $"📦 Checking ($service.name)..."

        let base_path = $"($env.PWD)/($service.path)"

        let config_results = $service.configs | each { |config|
            let config_path = $"($base_path)/($config)"
            let exists = ($config_path | path exists)
            let valid = (check-config $config_path)

            if $exists {
                if $valid {
                    print $"  ✅ ($config) exists and is valid TOML"
                } else {
                    print $"  ❌ ($config) has invalid TOML syntax"
                }
            } else {
                print $"  ❌ ($config) NOT FOUND"
            }

            {config: $config, exists: $exists, valid: $valid}
        }

        let backup_ok = if $service.backup != null {
            let backup_path = $"($base_path)/($service.backup)"
            if ($backup_path | path exists) {
                print $"  ✅ Backup ($service.backup) exists"
                true
            } else {
                print $"  ❌ Backup ($service.backup) NOT FOUND"
                false
            }
        } else {
            true
        }

        print ""

        let all_configs_ok = ($config_results | all { |x| $x.exists and $x.valid })
        {service: $service.name, configs_ok: $all_configs_ok, backup_ok: $backup_ok, valid: ($all_configs_ok and $backup_ok)}
    }

    # Check orchestrator Rust integration
    print "🔧 Checking Orchestrator Rust Integration..."

    let config_rs_path = "orchestrator/src/config.rs"
    let config_rs_exists = ($config_rs_path | path exists)

    if $config_rs_exists {
        let lines = (open $config_rs_path | lines | length)
        print $"  ✅ src/config.rs exists \(($lines) lines\)"
    } else {
        print $"  ❌ src/config.rs NOT FOUND"
    }

    let cargo_toml_path = "orchestrator/Cargo.toml"
    let has_toml_dep = (open --raw $cargo_toml_path | str contains "toml = {")

    if $has_toml_dep {
        print "  ✅ toml dependency added to Cargo.toml"
    } else {
        print "  ❌ toml dependency NOT in Cargo.toml"
    }

    let lib_rs_path = "orchestrator/src/lib.rs"
    let has_config_mod = (open --raw $lib_rs_path | str contains "pub mod config;")

    if $has_config_mod {
        print "  ✅ config module exported in lib.rs"
    } else {
        print "  ❌ config module NOT exported in lib.rs"
    }

    print ""

    let all_services_ok = ($config_checks | all { |x| $x.valid })
    let rust_integration_ok = ($config_rs_exists and $has_toml_dep and $has_config_mod)
    let all_valid = ($all_services_ok and $rust_integration_ok)

    # Final result
    if $all_valid {
        print "✨ All platform configurations are valid!"
        print ""
        print "📋 Summary:"
        print "  - 3 services configured (Orchestrator, Control Center, MCP Server)"
        print "  - 6 config files created (2 per service)"
        print "  - 1 backup created (control-center/config.toml.backup)"
        print "  - Rust config module integrated (orchestrator/src/config.rs)"
        print ""
        print "🚀 Next Steps:"
        print "  1. Update orchestrator/src/main.rs to use OrchestratorConfig::load()"
        print "  2. Test config loading with: cd orchestrator && cargo test config::"
        print "  3. Create config.user.toml.example files for user overrides"
    } else {
        print "❌ Configuration validation failed!"
        print "   Review errors above and fix missing/invalid files."
        print ""
        print "Failed checks:"
        for check in $config_checks {
            if not $check.valid {
                print $"  - ($check.service): configs_ok=($check.configs_ok), backup_ok=($check.backup_ok)"
            }
        }
        if not $rust_integration_ok {
            print $"  - Rust integration: config.rs=($config_rs_exists), toml_dep=($has_toml_dep), module_export=($has_config_mod)"
        }
    }
}
