# Platform Services CLI Commands
# User-facing commands for managing platform services

use target.nu *
use discovery.nu *
use health.nu *
use autostart.nu *
use connection.nu *

# Show platform status
export def platform-status [] {
    print ""
    print "Platform Services Status"
    print "========================"
    print ""

    let statuses = (check-all-services)

    if ($statuses | is-empty) {
        print "No platform services configured"
    } else {
        for item in $statuses {
            let icon = (if $item.status == "healthy" { "✓" } else { "✗" })
            print $"  ($icon) ($item.name): ($item.status)"
        }
    }

    print ""
}

# Show platform configuration
export def platform-config [] {
    print ""
    print "Platform Configuration"
    print "====================="
    print ""

    let platform = (load-platform-target)

    print $"Name: ($platform.platform.name)"
    print $"Type: ($platform.platform.type)"
    print $"Mode: ($platform.platform.mode)"
    print ""

    print "Configured Services:"
    let services = $platform.platform.services
    let svc_names = ($services | columns)

    for svc in $svc_names {
        let config = ($services | get $svc)
        let status = (if ($config.enabled | default true) { "enabled" } else { "disabled" })
        let required = (if ($config.required | default false) { "required" } else { "optional" })
        let mode_str = ($config.deployment_mode | default "binary")
        let endpoint_str = ($config.endpoint | default "N/A")
        print $"  • ($svc) [($status), ($required)]"
        print $"      Endpoint: ($endpoint_str)"
        print $"      Mode: ($mode_str)"
    }

    print ""
}

# List platform services
export def platform-list [] {
    print ""
    print "Platform Services"
    print "================="
    print ""

    let services = (list-services)

    if ($services | is-empty) {
        print "No services available"
    } else {
        print "Enabled Services:"
        for svc in $services {
            print $"  • ($svc)"
        }
    }

    print ""
}

# Show platform health
export def platform-health [] {
    print ""
    print "Platform Health Check"
    print "===================="
    print ""

    let required = (check-required-services)
    let all = (check-all-services)

    print "Required Services:"
    for item in $required {
        let icon = (if $item.status == "healthy" { "✓" } else { "✗" })
        print $"  ($icon) ($item.name): ($item.status)"
    }

    print ""
    print "Optional Services:"

    # Filter optional services (those not in required list)
    mut optional = []
    for item in $all {
        let is_req = (
            $required
            | any {|r| $r.name == $item.name }
        )
        if not $is_req {
            $optional = ($optional | append $item)
        }
    }

    if ($optional | is-empty) {
        print "  (none)"
    } else {
        for item in $optional {
            let icon = (if $item.status == "healthy" { "✓" } else { "✗" })
            print $"  ($icon) ($item.name): ($item.status)"
        }
    }

    print ""
}

# Start platform services
export def platform-start [] {
    print ""
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print "Starting Platform Services"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print ""

    start-required-services

    print ""
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print "Platform Health Status"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print ""

    platform-health
}

# Show platform connection status
export def platform-connections [] {
    show-connection-status
}

# Initialize platform for workspace
export def platform-init [] {
    print "Initializing platform for workspace..."
    init-connection-metadata
    print "✓ Platform initialized"
}
