# REMOVED: use lib_provisioning * - causes circular import
use utils.nu *
use handlers.nu *
use ../lib_provisioning/config/accessor.nu *
use ../lib_provisioning/utils/error.nu [throw-error]

# > TaskServs Delete
export def "main delete" [
    task_name?: string   # Taskserv name to delete
    server?: string      # Server hostname (optional, all matching servers if omitted)
    ...args              # Additional args
    --infra (-i): string     # Infra directory
    --settings (-s): string  # Settings path
    --iptype: string = "public" # IP type to connect
    --yes (-y)           # Confirm delete without prompt
    --force              # Delete taskservs no longer in servers.ncl (reads from state file)
    --debug (-x)         # Use Debug mode
    --xm                 # Debug with PROVISIONING_METADATA
    --metadata           # Error with metadata (-xm)
    --notitles           # No titles
    --helpinfo (-h)      # For more details use options "help" (no dashes)
    --out: string        # Print Output format: json, yaml, text (default)
] {
    if ($out | is-not-empty) {
        set-provisioning-out $out
        set-provisioning-no-terminal true
    }
    provisioning_init $helpinfo "taskservs delete" ([($task_name | default "") ($server | default "")] | append $args)
    if $debug { set-debug-enabled true }
    if $metadata { set-metadata-enabled true }

    match $task_name {
        null | "h" => {
            ^$"((get-provisioning-name))" -mod taskserv delete --help --notitles
            return
        },
        "help" => {
            ^$"((get-provisioning-name))" -mod taskserv delete --help
            _print (provisioning_options "delete")
            return
        },
        _ => {},
    }

    let curr_settings = (find_get_settings --infra $infra --settings $settings)

    # Validate server exists in settings (server definitions are still needed for SSH even with --force)
    if $server != null and $server != "" {
        if ($curr_settings.data.servers | where hostname == $server | is-empty) {
            _print $"🛑 server (_ansi red_bold)($server)(_ansi reset) not found in settings"
            exit 1
        }
    }

    # Safety prompt
    let target_desc = if ($server | is-not-empty) {
        $"($task_name) on ($server)"
    } else {
        $"($task_name) on all matching servers"
    }
    let force_label = if $force { " (--force: reading from state)" } else { "" }
    if not $yes {
        _print $"Delete (_ansi red_bold)($target_desc)(_ansi reset)($force_label)? Type (_ansi green_bold)yes(_ansi reset): "
        let user_input = (input --numchar 3)
        if $user_input != "yes" and $user_input != "YES" {
            _print "Aborted."
            exit 1
        }
    }

    let run_delete = {
        let curr_settings = (settings_with_env $curr_settings)
        set-wk-cnprov $curr_settings.wk_path
        let match_task = $task_name | default ""
        let match_server = $server | default ""
        on_taskservs $curr_settings $match_task "" $match_server $iptype false false false "delete" $force
    }
    let result = desktop_run_notify $"((get-provisioning-name)) taskserv delete" "-> " $run_delete --timeout 11sec
    if not (is-debug-enabled) { end_run "" }
}
