#!/usr/bin/env nu

# Detect Running Platform Services
# Auto-detects which platform services are currently running
# Shows health status and connection info for each service
# Usage: nu detect-services.nu
# Usage: nu detect-services.nu --docker (detect in Docker)
# Usage: nu detect-services.nu --kubernetes (detect in Kubernetes)

use ansi.nu
use external.nu

export def main [
    --docker = false       # Detect services in Docker
    --kubernetes = false   # Detect services in Kubernetes
]: nothing -> nothing {
    print-info "Detecting platform services..."
    print ""

    # If no option specified, detect locally
    if not $docker and not $kubernetes {
        detect-local-services
    } else if $docker {
        detect-docker-services
    } else if $kubernetes {
        detect-kubernetes-services
    }
}

def detect-local-services []: nothing -> nothing {
    section "Local Services Detection"

    let services: list<record<name: string, port: int, cmd: string>> = [
        { name: "Orchestrator", port: 9090, cmd: "orchestrator" }
        { name: "Control Center", port: 8080, cmd: "control-center" }
        { name: "MCP Server", port: 8888, cmd: "mcp-server" }
        { name: "PostgreSQL", port: 5432, cmd: "postgres" }
        { name: "Gitea", port: 3000, cmd: "gitea" }
    ]

    for service in $services {
        detect-local-service $service.name $service.port $service.cmd
    }

    print ""
}

def detect-local-service [name: string, port: int, cmd: string]: nothing -> nothing {
    let port_check: record<exit_code: int, stdout: string, stderr: string> = do {
        ^lsof -i -P -n | grep LISTEN | grep $"(:($port)|port $port)"
    } | complete

    let process_check: record<exit_code: int, stdout: string, stderr: string> = do {
        ^pgrep -f $cmd
    } | complete

    if $port_check.exit_code == 0 or $process_check.exit_code == 0 {
        print-success $"($name) is RUNNING"
        print $"  Port: ($port)"

        if $port_check.exit_code == 0 {
            print "  Status: ✓ Listening"
        }
    } else {
        print-warning $"($name) is NOT running"
        print $"  Expected port: ($port)"
    }

    print ""
}

def detect-docker-services []: nothing -> nothing {
    section "Docker Services Detection"

    if not (external check-command-exists "docker") {
        print-error "Docker not installed"
        exit 1
    }

    let containers: record<exit_code: int, stdout: string, stderr: string> = (external run-cmd "docker" ["ps", "--filter", "label=app=provisioning", "--format", "table {{.Names}}\t{{.Ports}}\t{{.Status}}"])

    if $containers.exit_code != 0 {
        print-error "Failed to detect Docker services"
        print $containers.stderr
        exit 1
    }

    if ($containers.stdout | str length) == 0 {
        print-warning "No provisioning services found in Docker"
    } else {
        print $containers.stdout
        print-success "Found provisioning services in Docker"
    }

    print ""
}

def detect-kubernetes-services []: nothing -> nothing {
    section "Kubernetes Services Detection"

    if not (external check-command-exists "kubectl") {
        print-error "kubectl not installed"
        exit 1
    }

    let namespaces: list<string> = ["provisioning", "default"]

    for ns in $namespaces {
        print-working $"Checking namespace: ($ns)"

        let deployments: record<exit_code: int, stdout: string, stderr: string> = (external run-cmd "kubectl" ["get", "deployments", "-n", $ns, "-l", "app=provisioning", "-o", "wide"])

        if $deployments.exit_code == 0 and ($deployments.stdout | str length) > 0 {
            print-success $"Found deployments in namespace: ($ns)"
            print $deployments.stdout
            print ""
        }

        let services: record<exit_code: int, stdout: string, stderr: string> = (external run-cmd "kubectl" ["get", "svc", "-n", $ns, "-l", "app=provisioning", "-o", "wide"])

        if $services.exit_code == 0 and ($services.stdout | str length) > 0 {
            print-success $"Found services in namespace: ($ns)"
            print $services.stdout
            print ""
        }
    }

    print ""
}
