# Help renderer - Formats help content with consistent styling
# Converts structured help data into formatted output with ANSI colors

# Render header with title and color
export def render-header [title: string, color: string] {
    let color_code = (match $color {
        "cyan" => (_ansi cyan_bold)
        "purple" => (_ansi purple_bold)
        "blue" => (_ansi blue_bold)
        "green" => (_ansi green_bold)
        "red" => (_ansi red_bold)
        "magenta" => (_ansi magenta_bold)
        "yellow" => (_ansi yellow_bold)
        _ => (_ansi white_bold)
    })

    let reset = (_ansi reset)
    let line1 = $"($color_code)╔══════════════════════════════════════════════════╗($reset)\n"
    let line2 = $"($color_code)║($reset)  $title($color_code)   ║($reset)\n"
    let line3 = $"($color_code)╚══════════════════════════════════════════════════╝($reset)\n\n"

    $line1 + $line2 + $line3
}

# Render section header with category
export def render-section-header [name: string, subtitle: string] {
    let header = $"(_ansi green_bold)[$name](_ansi reset) "
    let sub = if ($subtitle | str length) > 0 { $subtitle } else { "" }
    $header + $sub + "\n"
}

# Render command line
export def render-command-line [cmd: string, desc: string] {
    let cmd_part = $"  (_ansi blue)$cmd(_ansi reset)"
    let desc_part = if ($desc | str length) > 0 {
        $" - $desc"
    } else {
        ""
    }
    $cmd_part + $desc_part + "\n"
}

# Render flag line (for flags section)
export def render-flag-line [flag: string, desc: string] {
    $"  (_ansi cyan)$flag(_ansi reset) - $desc\n"
}

# Render feature item (bullet point)
export def render-feature [feature: string] {
    $"    • (_ansi green)$feature(_ansi reset)\n"
}

# Render a complete section from structured data
export def render-section [section: record] {
    let name = $section.name? | default ""
    let subtitle = $section.subtitle? | default ""
    let items = $section.items? | default []
    let content = $section.content? | default ""
    let features = $section.features? | default []
    let note = $section.note? | default ""

    let header = if ($name | str length) > 0 {
        (render-section-header $name $subtitle)
    } else {
        ""
    }

    let items_output = if ($items | length) > 0 {
        $items
        | each { |item|
            if ("cmd" in $item) {
                (render-command-line $item.cmd ($item.desc? | default ""))
            } else if ("flag" in $item) {
                (render-flag-line $item.flag ($item.desc? | default ""))
            } else if ("step" in $item) {
                let step_prefix = $"  (_ansi cyan)($item.step)(_ansi reset) "
                let step_val = if ("cmd" in $item) { $item.cmd } else { $item.note? | default "" }
                $step_prefix + $step_val + "\n"
            } else if ("level" in $item) {
                let level_prefix = $"  (_ansi yellow)($item.level)(_ansi reset): "
                let level_val = $item.desc? | default ""
                $level_prefix + $level_val + "\n"
            } else {
                ""
            }
        }
        | str join ""
    } else {
        ""
    }

    let content_output = if ($content | str length) > 0 {
        $content + "\n\n"
    } else {
        ""
    }

    let features_output = if ($features | length) > 0 {
        $features
        | each { |feature| (render-feature $feature) }
        | str join ""
    } else {
        ""
    }

    let note_output = if ($note | str length) > 0 {
        $"(_ansi default_dimmed)Note: $note(_ansi reset)\n\n"
    } else {
        ""
    }

    $header + $items_output + $content_output + $features_output + $note_output
}

# Render complete help category with all sections
export def render-help-category [title: string, color: string, sections: list, examples: list = [], warning: string = "", tip: string = ""] {
    let header = (render-header $title $color)

    let sections_output = $sections
    | each { |section| (render-section $section) }
    | str join "\n"

    let warning_output = if ($warning | str length) > 0 {
        $"(_ansi yellow_bold)⚠️  ($warning)(_ansi reset)\n\n"
    } else {
        ""
    }

    let examples_output = if ($examples | length) > 0 {
        let ex_header = (render-section-header "Examples" "")
        let ex_items = ($examples
        | each { |ex| $"  (_ansi green)$ex(_ansi reset)\n" }
        | str join "")
        $ex_header + $ex_items + "\n"
    } else {
        ""
    }

    let tip_output = if ($tip | str length) > 0 {
        $"(_ansi default_dimmed)💡 $tip(_ansi reset)\n"
    } else {
        ""
    }

    let result1 = $header + $sections_output
    let result2 = $result1 + $warning_output
    let result3 = $result2 + $examples_output
    $result3 + $tip_output
}

# Quick reference rendering for main help (categories list)
export def render-main-help [] {
    let show_header = not ($env.PROVISIONING_NO_TITLES? | default false)
    if $show_header {
        let h1 = $"(_ansi yellow_bold)╔════════════════════════════════════════════════════════════════╗(_ansi reset)\n"
        let h2 = $"(_ansi yellow_bold)║ (_ansi reset)  (_ansi cyan_bold)PROVISIONING SYSTEM(_ansi reset) - Layered Infrastructure Automation  (_ansi yellow_bold)    ║(_ansi reset)\n"
        let h3 = $"(_ansi yellow_bold)╚════════════════════════════════════════════════════════════════╝(_ansi reset)\n\n"
        $h1 + $h2 + $h3
    } else {
        ""
    }
}

# Render command examples for guides
export def render-command-examples [examples: list] {
    if ($examples | length) == 0 {
        return ""
    }

    let header = $"(_ansi green_bold)EXAMPLES(_ansi reset)\n\n"
    let items = ($examples
    | each { |ex|
        if ($ex | str contains "  #") {
            $"  ($ex)\n"
        } else {
            $"  provisioning $ex\n"
        }
    }
    | str join "")

    $header + $items + "\n"
}
