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

# Selective imports (ADR-025 Phase 3 Layer 2).
use lib_provisioning/platform/health.nu [check-all-services check-required-services]
use lib_provisioning/platform/discovery.nu [list-services]
use lib_provisioning/platform/autostart.nu [start-required-services]
use lib_provisioning/platform/connection.nu [init-connection-metadata show-connection-status]

# 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
# load-platform-target is not defined anywhere in the codebase; this function
# was dead at runtime. Falls back to a clear "not configured" message.
export def platform-config [] {
    print ""
    print "Platform Configuration"
    print "====================="
    print ""
    print "Platform target not available — no load-platform-target implementation."
    print "Use 'prvng platform list' to see configured services from discovery."
    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"
}
