# List all clusters in active workspace
# This file is sourced by bash after lib_minimal.nu is loaded
# Not meant to be run standalone

# Get active workspace
let active_ws = (workspace-active)
if ($active_ws | is-empty) {
    print 'No active workspace'
    exit 1
}

# Get workspace path from config
let user_config_path = (
    $env.HOME
    | path join 'Library'
    | path join 'Application Support'
    | path join 'provisioning'
    | path join 'user_config.yaml'
)

if not ($user_config_path | path exists) {
    print 'Config not found'
    exit 1
}

let config = (open $user_config_path)
let workspaces = ($config | get --optional workspaces | default [])
let ws = ($workspaces | where { $in.name == $active_ws } | first)

if ($ws | is-empty) {
    print 'Workspace not found'
    exit 1
}

let ws_path = $ws.path

# List all clusters from workspace
let clusters = (
    if (($ws_path | path join '.clusters') | path exists) {
        let clusters_path = ($ws_path | path join '.clusters')
        ls $clusters_path
        | where type == 'dir'
        | each {|cl|
            let cl_name = ($cl.name | path basename)
            {
                name: $cl_name
                path: $cl.name
            }
        }
    } else {
        []
    }
)

if ($clusters | length) == 0 {
    print '🗂️ Available Clusters: (none found)'
} else {
    print '🗂️ Available Clusters:'
    print ''
    $clusters | each {|cl|
        print $"  • ($cl.name)"
    } | ignore
}
