#!/usr/bin/env nu
# test-catalog.nu - Validate service catalog TOML structure

print "═══════════════════════════════════════════════════════════════"
print "SERVICE CATALOG VALIDATION TEST"
print "═══════════════════════════════════════════════════════════════"
print ""

# Load catalog
print "1. Loading configs/services-catalog.toml..."
let catalog = (open configs/services-catalog.toml)
print "   ✓ Loaded successfully"
print ""

# Check version
print "2. Checking catalog version..."
let version = ($catalog.version.catalog_version)
print $"   Version: ($version)"
print "   ✓ Catalog version found"
print ""

# Count services
print "3. Counting services..."
let service_count = ($catalog.service | length)
print $"   Total services: ($service_count)"
print ""

# List services
print "4. Service inventory:"
let services = ($catalog.service | transpose)
$services | each { |svc|
    let name = ($svc.key)
    let display = ($svc.value.display_name? // $name)
    print $"   - ($display)"
}
print ""

# Check ports
print "5. Port assignments:"
$services | each { |svc|
    let name = ($svc.key)
    let server = ($svc.value.server? // null)
    if ($server != null) {
        let port = ($server.default_port?)
        print $"   ($name): port ($port)"
    }
}
print ""

# Check service groups
print "6. Service groups:"
let groups = ($catalog.groups | transpose)
$groups | each { |grp|
    let gname = ($grp.key)
    let gcount = (($grp.value.services | length))
    print $"   ($gname): ($gcount) services"
}
print ""

# Check patterns
print "7. Deployment patterns:"
let patterns = ($catalog.pattern | transpose)
$patterns | each { |ptn|
    let pname = ($ptn.key)
    let pcount = (($ptn.value.services | length))
    print $"   ($pname): ($pcount) services"
}
print ""

print "═══════════════════════════════════════════════════════════════"
print "✅ All validations passed!"
print "═══════════════════════════════════════════════════════════════"
