#!/usr/bin/env nu
# service-catalog.nu - Service discovery and catalog management (v0.1.0)
#
# Lightweight service discovery tool for exploring syntaxis services
# and exporting configurations for project-provisioning integration.
#

# Load the service catalog from TOML
def load_catalog [] {
    try {
        open configs/services-catalog.toml
    } catch {
        print "❌ Error: Could not load configs/services-catalog.toml"
        null
    }
}

# List all available services
def list_services [] {
    let catalog = load_catalog
    if ($catalog == null) { return }

    print (ansi cyan_bold)
    print "═══════════════════════════════════════════════════════════════"
    print "SYNTAXIS SERVICES CATALOG"
    print "═══════════════════════════════════════════════════════════════"
    print (ansi reset)
    print ""

    let services = ($catalog | get service  | columns | sort)

    print (ansi yellow_bold)"Available Services:"(ansi reset)
    print ""

    $services | each { |svc|
        let info = ($catalog.service | get $svc)
        let display_name = ($info.display_name? // $svc)
        let description = ($info.description? // "")
        let type = ($info.type? // "unknown")

        print $"  (ansi cyan)($display_name)(ansi reset)"
        print $"    Type: (ansi blue)($type)(ansi reset)"
        print $"    $(ansi dimmed)($description)(ansi reset)"

        # Show port if applicable
        if ($info.server? != null) {
            let port = ($info.server.default_port?)
            if ($port != null) {
                print $"    Port: (ansi green)($port)(ansi reset)"
            }
        }

        print ""
    }

    print (ansi yellow_bold)"Usage:"(ansi reset)
    print "  nu scripts/provisioning/service-catalog.nu info <service-name>"
    print "  nu scripts/provisioning/service-catalog.nu usage <service-name>"
    print "  nu scripts/provisioning/service-catalog.nu preset <preset-name>"
    print "  nu scripts/provisioning/service-catalog.nu ports"
    print ""
}

# Show ports
def show_ports [] {
    let catalog = load_catalog
    if ($catalog == null) { return }

    print (ansi cyan_bold)
    print "═══════════════════════════════════════════════════════════════"
    print "SYNTAXIS SERVICES - PORT MAPPING"
    print "═══════════════════════════════════════════════════════════════"
    print (ansi reset)
    print ""

    print (ansi yellow_bold)"Service Ports:"(ansi reset)
    print ""

    let services = ($catalog | get service  | columns | sort)
    $services | each { |svc|
        let info = ($catalog.service | get $svc)
        if ($info.server? != null) {
            let display_name = ($info.display_name? // $svc)
            let port = ($info.server.default_port?)
            let scheme = ($info.server.scheme? // "http")
            let host = ($info.server.default_host? // "127.0.0.1")

            print $"  (ansi cyan)($display_name)(ansi reset): (ansi green)($scheme)://($host):($port)(ansi reset)"
        }
    }
    print ""
}

# List service groups
def list_groups [] {
    let catalog = load_catalog
    if ($catalog == null) { return }

    print (ansi cyan_bold)
    print "═══════════════════════════════════════════════════════════════"
    print "SERVICE GROUPS (Logical groupings)"
    print "═══════════════════════════════════════════════════════════════"
    print (ansi reset)
    print ""

    $catalog.groups | transpose | each { |item|
        let name = $item.key
        let group = $item.value
        let services = ($group.services | str join ", ")

        print (ansi yellow_bold)$name(ansi reset)
        print $"  ($group.description?)"
        print $"  Services: ($services)"
        print ""
    }
}

# List deployment patterns
def list_patterns [] {
    let catalog = load_catalog
    if ($catalog == null) { return }

    print (ansi cyan_bold)
    print "═══════════════════════════════════════════════════════════════"
    print "DEPLOYMENT PATTERNS"
    print "═══════════════════════════════════════════════════════════════"
    print (ansi reset)
    print ""

    $catalog.pattern | transpose | each { |item|
        let name = $item.key
        let pattern = $item.value
        let services = ($pattern.services | str join ", ")

        print (ansi yellow_bold)($pattern.name?)(ansi reset)
        print $"  ($pattern.description?)"
        print $"  Services: (ansi dimmed)($services)(ansi reset)"
        print $"  Use cases: ($pattern.use_cases | str join ", ")"
        print ""
    }
}

# Print help
def print_help [] {
    print (ansi cyan_bold)
    print "SERVICE CATALOG - Service Discovery & Management"
    print (ansi reset)
    print ""
    print "Commands:"
    print ""
    print "  list                    List all available services"
    print "  ports                   Show port mappings"
    print "  groups                  List service groups"
    print "  patterns                List deployment patterns"
    print "  help                    Show this help"
    print ""
    print "Examples:"
    print ""
    print "  nu scripts/provisioning/service-catalog.nu list"
    print "  nu scripts/provisioning/service-catalog.nu ports"
    print "  nu scripts/provisioning/service-catalog.nu groups"
    print "  nu scripts/provisioning/service-catalog.nu patterns"
    print ""
}

# Main entry point
def main [command: string = "help"] {
    match $command {
        "list" => list_services,
        "ports" => show_ports,
        "groups" => list_groups,
        "patterns" => list_patterns,
        "help" | _ => print_help,
    }
}

# Run main
main
