# Platform Connection Metadata
# Manages connection metadata and status for platform services

# Selective imports (ADR-025 Phase 3 Layer 2).
use lib_provisioning/user/config.nu [get-active-workspace]

# Get platform connection metadata file path
def get-connection-metadata-path [] {
    let workspace = (get-active-workspace)
    ([
        $workspace
        ".platform"
        "connection.yaml"
    ] | path join)
}

# Load platform connection metadata
export def load-connection-metadata [] {
    let path = (get-connection-metadata-path)

    if not ($path | path exists) {
        return {
            activated_at: ""
            active_connections: {}
        }
    }

    open $path
}

# Store platform connection metadata
export def store-connection-metadata [metadata: record] {
    let path = (get-connection-metadata-path)
    let dir = ($path | path dirname)

    if not ($dir | path exists) {
        mkdir $dir
    }

    $metadata | to yaml | save -f $path
}

# Get active service connections
export def get-active-connections [] {
    let metadata = (load-connection-metadata)
    $metadata.active_connections
}

# Add service connection
export def add-service-connection [
    service_name: string
    endpoint: string
] {
    mut metadata = (load-connection-metadata)
    let now = (date now | format date "%Y-%m-%dT%H:%M:%SZ")

    $metadata.active_connections = (
        $metadata.active_connections
        | insert $service_name {
            endpoint: $endpoint
            connected_at: $now
            status: "connected"
        }
    )

    store-connection-metadata $metadata
}

# Remove service connection
export def remove-service-connection [service_name: string] {
    mut metadata = (load-connection-metadata)

    $metadata.active_connections = (
        $metadata.active_connections
        | reject $service_name
    )

    store-connection-metadata $metadata
}

# Update service connection status
export def update-service-status [
    service_name: string
    status: string
] {
    mut metadata = (load-connection-metadata)

    if ($service_name in $metadata.active_connections) {
        $metadata.active_connections | get $service_name | insert status $status
        store-connection-metadata $metadata
    }
}

# Get service connection status
export def get-service-status [service_name: string] {
    let connections = (get-active-connections)

    if ($service_name in $connections) {
        $connections | get $service_name
    } else {
        null
    }
}

# Initialize connection metadata for workspace
export def init-connection-metadata [] {
    let metadata = {
        activated_at: (date now | format date "%Y-%m-%dT%H:%M:%SZ")
        active_connections: {}
    }

    store-connection-metadata $metadata
}

# Display connection status
export def show-connection-status [] {
    let metadata = (load-connection-metadata)

    print ""
    print "Platform Connection Status:"
    print $"  Activated: ($metadata.activated_at)"
    print ""

    let connections = $metadata.active_connections

    if ($connections | is-empty) {
        print "  No active connections"
    } else {
        print "  Active Connections:"
        $connections | transpose name info | each {|item|
            print $"    • ($item.name)"
            print $"        Endpoint: ($item.info.endpoint)"
            print $"        Status: ($item.info.status)"
            print $"        Connected: ($item.info.connected_at)"
        }
    }

    print ""
}
