#!/usr/bin/env nu
# Thin entry for server commands.
# Loads servers/create.nu directly — bypasses full dispatcher + run_module re-invocation.
# Cuts startup from ~46s to ~3-5s (single Nushell process, no exec re-spawn).

export-env {
    let lib_dirs_raw = ($env.NU_LIB_DIRS? | default "")
    let current_lib_dirs = if ($lib_dirs_raw | type) == "string" {
        if ($lib_dirs_raw | is-empty) { [] } else { ($lib_dirs_raw | split row ":") }
    } else {
        $lib_dirs_raw
    }
    let dynamic = ($env.PROVISIONING? | default "" | path join "core" "nulib")
    $env.NU_LIB_DIRS = ([
        "/opt/provisioning/core/nulib"
        "/usr/local/provisioning/core/nulib"
    ] | append $current_lib_dirs | append (if ($dynamic | is-not-empty) { [$dynamic] } else { [] }))

    # Strip leading "server"/"s" token so get-provisioning-args returns the sub-command
    # e.g. "server create --infra x" → "create --infra x"
    let args_raw = ($env.PROVISIONING_ARGS? | default "")
    $env.PROVISIONING_ARGS = ($args_raw | str replace --regex '^(server|s)\s+' '')

    # Bash exports booleans as strings — normalize before any module code runs
    let _coerce = {|raw| $raw == "true" or $raw == "1" }
    let raw_no_titles = ($env.PROVISIONING_NO_TITLES? | default "")
    if ($raw_no_titles | describe) == "string" and ($raw_no_titles | is-not-empty) {
        $env.PROVISIONING_NO_TITLES = (do $_coerce $raw_no_titles)
    }
    let raw_no_terminal = ($env.PROVISIONING_NO_TERMINAL? | default "")
    if ($raw_no_terminal | describe) == "string" and ($raw_no_terminal | is-not-empty) {
        $env.PROVISIONING_NO_TERMINAL = (do $_coerce $raw_no_terminal)
    }
    let raw_titles_shown = ($env.PROVISIONING_TITLES_SHOWN? | default "")
    if ($raw_titles_shown | describe) == "string" and ($raw_titles_shown | is-not-empty) {
        $env.PROVISIONING_TITLES_SHOWN = (do $_coerce $raw_titles_shown)
    }
    let raw_debug = ($env.PROVISIONING_DEBUG? | default "")
    if ($raw_debug | describe) == "string" and ($raw_debug | is-not-empty) {
        $env.PROVISIONING_DEBUG = (do $_coerce $raw_debug)
    }
}

use servers/create.nu *
use servers/delete.nu *
use servers/ssh.nu *
use servers/list.nu *
use servers/upgrade.nu *

def main [
    ...args: string
    --infra (-i): string    = ""
    --settings (-s): string = ""
    --outfile (-o): string  = ""
    --serverpos (-p): int
    --check (-c)
    --yes (-y)
    --del-volume              # (delete) also delete attached volumes
    --del-fip                 # (delete) also delete assigned floating IPs
    --run (-r)
    --wait (-w)
    --select: string        = ""
    --debug (-x)
    --xm
    --xc
    --xr
    --xld
    --metadata
    --notitles
    --orchestrated
    --orchestrator: string  = ""
    --out: string           = ""
    --helpinfo (-h)
]: nothing -> nothing {
    if $debug { $env.PROVISIONING_DEBUG = true }

    # CMD_ARGS from bash wrapper includes "server"/"s" as arg[0] — strip it.
    let first = ($args | get 0? | default "")
    let rest = if $first in ["server", "s"] { $args | skip 1 } else { $args }

    let subcmd = ($rest | get 0? | default "")
    let name   = ($rest | get 1? | default "")

    match $subcmd {
        "list" | "l" => {
            if ($infra | is-not-empty) {
                main list --infra $infra --debug=$debug --out=$out
            } else {
                main list --debug=$debug --out=$out
            }
        }
        "create" | "c" => {
            if ($infra | is-not-empty) {
                if ($name | is-not-empty) {
                    main create $name --infra $infra --wait=$wait --check=$check --outfile $outfile
                } else {
                    main create --infra $infra --wait=$wait --check=$check --outfile $outfile
                }
            } else {
                if ($name | is-not-empty) {
                    main create $name --wait=$wait --check=$check --outfile $outfile
                } else {
                    main create --wait=$wait --check=$check --outfile $outfile
                }
            }
        }
        "sync" => {
            if ($infra | is-not-empty) {
                main sync --infra $infra
            } else {
                main sync
            }
        }
        "upgrade" | "u" => {
            if ($name | is-not-empty) {
                main upgrade $name --infra $infra --settings $settings --check=$check --yes=$yes --debug=$debug
            } else {
                main upgrade --infra $infra --settings $settings --check=$check --yes=$yes --debug=$debug
            }
        }
        "delete" | "d" | "del" => {
            if ($name | is-not-empty) {
                if $del_volume and $del_fip {
                    main delete $name --infra $infra --yes=$yes --del-volume --del-fip
                } else if $del_volume {
                    main delete $name --infra $infra --yes=$yes --del-volume
                } else if $del_fip {
                    main delete $name --infra $infra --yes=$yes --del-fip
                } else {
                    main delete $name --infra $infra --yes=$yes
                }
            } else {
                if $del_volume and $del_fip {
                    main delete --all --infra $infra --yes=$yes --del-volume --del-fip
                } else if $del_volume {
                    main delete --all --infra $infra --yes=$yes --del-volume
                } else if $del_fip {
                    main delete --all --infra $infra --yes=$yes --del-fip
                } else {
                    main delete --all --infra $infra --yes=$yes
                }
            }
        }
        "ssh" => {
            # Only forward non-default flags to avoid polluting the sub-command signature
            let has_infra    = ($infra    | is-not-empty)
            let has_settings = ($settings | is-not-empty)
            let has_name     = ($name     | is-not-empty)
            if $run {
                match [$has_name, $has_infra, $has_settings, $debug] {
                    [true,  true,  true,  true ] => { main ssh $name --infra $infra --settings $settings --debug --run }
                    [true,  true,  true,  false] => { main ssh $name --infra $infra --settings $settings --run }
                    [true,  true,  false, true ] => { main ssh $name --infra $infra --debug --run }
                    [true,  true,  false, false] => { main ssh $name --infra $infra --run }
                    [true,  false, true,  true ] => { main ssh $name --settings $settings --debug --run }
                    [true,  false, true,  false] => { main ssh $name --settings $settings --run }
                    [true,  false, false, true ] => { main ssh $name --debug --run }
                    [true,  false, false, false] => { main ssh $name --run }
                    [false, true,  true,  true ] => { main ssh --infra $infra --settings $settings --debug --run }
                    [false, true,  true,  false] => { main ssh --infra $infra --settings $settings --run }
                    [false, true,  false, true ] => { main ssh --infra $infra --debug --run }
                    [false, true,  false, false] => { main ssh --infra $infra --run }
                    [false, false, true,  true ] => { main ssh --settings $settings --debug --run }
                    [false, false, true,  false] => { main ssh --settings $settings --run }
                    [false, false, false, true ] => { main ssh --debug --run }
                    _                            => { main ssh --run }
                }
            } else {
                match [$has_name, $has_infra, $has_settings, $debug] {
                    [true,  true,  true,  true ] => { main ssh $name --infra $infra --settings $settings --debug }
                    [true,  true,  true,  false] => { main ssh $name --infra $infra --settings $settings }
                    [true,  true,  false, true ] => { main ssh $name --infra $infra --debug }
                    [true,  true,  false, false] => { main ssh $name --infra $infra }
                    [true,  false, true,  true ] => { main ssh $name --settings $settings --debug }
                    [true,  false, true,  false] => { main ssh $name --settings $settings }
                    [true,  false, false, true ] => { main ssh $name --debug }
                    [true,  false, false, false] => { main ssh $name }
                    [false, true,  true,  true ] => { main ssh --infra $infra --settings $settings --debug }
                    [false, true,  true,  false] => { main ssh --infra $infra --settings $settings }
                    [false, true,  false, true ] => { main ssh --infra $infra --debug }
                    [false, true,  false, false] => { main ssh --infra $infra }
                    [false, false, true,  true ] => { main ssh --settings $settings --debug }
                    [false, false, true,  false] => { main ssh --settings $settings }
                    [false, false, false, true ] => { main ssh --debug }
                    _                            => { main ssh }
                }
            }
        }
        "volume" | "vol" => {
            use provisioning-volume.nu *
            let vol_subcmd = ($rest | get 1? | default "list")
            let vol_args   = if ($rest | length) > 2 { $rest | skip 2 } else { [] }
            match $vol_subcmd {
                "list"   | "l"  => { main list --infra $infra --out $out }
                "create" | "c"  => { main create ($vol_args | get 0? | default "") --yes=$yes }
                "attach" | "a"  => { main attach ($vol_args | get 0? | default "") --server ($vol_args | get 1? | default "") --yes=$yes }
                "detach" | "d"  => { main detach ($vol_args | get 0? | default "") --yes=$yes }
                "delete" | "rm" => { main delete ($vol_args | get 0? | default "") --yes=$yes }
                _ => { main list --infra $infra --out $out }
            }
        }
        _ => {
            error make { msg: $"Unknown server subcommand '($subcmd)'. Use: create, delete, list, ssh, sync, volume" }
        }
    }
}
