#!/usr/bin/env nu
# Minimal Help System - Fast Path with Fluent i18n Support
# This bypasses the full config system for instant help display
# Uses Mozilla Fluent (.ftl) format for multilingual support



# Format alias: brackets in gray, inner text in category color
def format-alias [alias: string, color: string] {
    if ($alias | is-empty) {
        ""
    } else if ($alias | str starts-with "[") and ($alias | str ends-with "]") {
        # Extract content between brackets (exclusive end range)
        let inner = ($alias | str substring 1..<(-1))
        (ansi d) + "[" + (ansi rst) + $color + $inner + (ansi rst) + (ansi d) + "]" + (ansi rst)
    } else {
        (ansi d) + $alias + (ansi rst)
    }
}

# Format categories with tab-separated columns and colors
def format-categories [rows: list<list<string>>] {
    let header = "  Category\t\tAlias\t Description"
    let separator = "  ════════════════════════════════════════════════════════════════════"

    let formatted_rows = (
        $rows | each { |row|
            let emoji = $row.0
            let name = $row.1
            let alias = $row.2
            let desc = $row.3

            # Assign color based on category name
            let color = (match $name {
                "infrastructure" => (ansi cyan)
                "orchestration" => (ansi magenta)
                "development" => (ansi green)
                "workspace" => (ansi green)
                "setup" => (ansi magenta)
                "platform" => (ansi red)
                "authentication" => (ansi yellow)
                "plugins" => (ansi cyan)
                "utilities" => (ansi green)
                "tools" => (ansi yellow)
                "vm" => (ansi white)
                "diagnostics" => (ansi magenta)
                "concepts" => (ansi yellow)
                "guides" => (ansi blue)
                "integrations" => (ansi cyan)
                _ => ""
            })

            # Calculate tabs based on name length: 3 tabs for 6-10 char names, 2 tabs otherwise
            let name_len = ($name | str length)
            let name_tabs = match true {
               _ if $name_len <= 11 => "\t\t"
               _ => "\t"
            }

            # Format alias with brackets in gray and inner text in category color
            let alias_formatted = (format-alias $alias $color)
            let alias_len = ($alias | str length)
            let alias_tabs = match true {
               _ if ($alias_len == 8) =>  ""
               _ if ($name_len <= 3) => "\t\t"
               _ =>  "\t"
            }
            # Format: emoji + colored_name + tabs + colored_alias + tabs + description
            $"  ($emoji)($color)($name)((ansi rst))($name_tabs)($alias_formatted)($alias_tabs) ($desc)"
        }
    )

    ([$header, $separator] | append $formatted_rows | str join "\n")
}

# Get active locale from LANG environment variable
def get-active-locale [] {
    let lang_env = ($env.LANG? | default "en_US")
    let dot_idx = ($lang_env | str index-of ".")
    let lang_part = (
        if $dot_idx >= 0 {
            $lang_env | str substring 0..<$dot_idx
        } else {
            $lang_env
        }
    )
    let locale = ($lang_part | str replace "_" "-")
    $locale
}

# Parse simple Fluent format and return record of strings
def parse-fluent [content: string] {
    let lines = ($content | lines)

    $lines | reduce -f {} { |line, strings|
        if ($line | str starts-with "#") or ($line | str trim | is-empty) {
            $strings
        } else if ($line | str contains " = ") {
            let idx = ($line | str index-of " = ")
            if $idx != null {
                let key = ($line | str substring 0..$idx | str trim)
                let value = ($line | str substring ($idx + 3).. | str trim | str trim -c "\"")
                $strings | insert $key $value
            } else {
                $strings
            }
        } else {
            $strings
        }
    }
}

# Get a help string with fallback to English
def get-help-string [key: string] {
    let locale = (get-active-locale)
    # Use environment variable PROVISIONING as base path
    let prov_path = ($env.PROVISIONING? | default "/usr/local/provisioning/provisioning")
    let base_path = $"($prov_path)/locales"

    let locale_file = $"($base_path)/($locale)/help.ftl"
    let fallback_file = $"($base_path)/en-US/help.ftl"

    let content = (
        if ($locale_file | path exists) {
            open $locale_file
        } else {
            open $fallback_file
        }
    )

    let strings = (parse-fluent $content)
    $strings | get $key | default "[$key]"
}

# Main help dispatcher
def provisioning-help [category?: string = ""] {
    if ($category == "") {
        return (help-main)
    }

    let cat_lower = ($category | str downcase)
    let result = (match $cat_lower {
        "infrastructure" | "infra" => "infrastructure"
        "orchestration" | "orch" => "orchestration"
        "development" | "dev" => "development"
        "workspace" | "ws" => "workspace"
        "setup" | "st" => "setup"
        "platform" | "plat" => "platform"
        "authentication" | "auth" => "authentication"
        "mfa" => "mfa"
        "plugins" | "plugin" => "plugins"
        "utilities" | "utils" | "cache" => "utilities"
        "tools" => "tools"
        "vm" => "vm"
        "diagnostics" | "diag" | "status" | "health" => "diagnostics"
        "concepts" | "concept" => "concepts"
        "guides" | "guide" | "howto" => "guides"
        "integrations" | "integration" | "int" => "integrations"
        "build" | "bi" | "build-image" => "build"
        _ => "unknown"
    })

    if $result == "unknown" {
        print $"❌ Unknown help category: \"($category)\"\n"
        print "Available help categories: infrastructure, orchestration, development, workspace, setup, platform,"
        print "authentication, mfa, plugins, utilities, tools, vm, diagnostics, concepts, guides, integrations, build"
        return ""
    }

    match $result {
        "infrastructure" => (help-infrastructure)
        "orchestration" => (help-orchestration)
        "development" => (help-development)
        "workspace" => (help-workspace)
        "setup" => (help-setup)
        "platform" => (help-platform)
        "authentication" => (help-authentication)
        "mfa" => (help-mfa)
        "plugins" => (help-plugins)
        "utilities" => (help-utilities)
        "tools" => (help-tools)
        "vm" => (help-vm)
        "diagnostics" => (help-diagnostics)
        "concepts" => (help-concepts)
        "guides" => (help-guides)
        "integrations" => (help-integrations)
        "build" => (help-build)
        _ => (help-main)
    }
}

# Main help overview
def help-main [] {
    let title = (get-help-string "help-main-title")
    let subtitle = (get-help-string "help-main-subtitle")
    let categories = (get-help-string "help-main-categories")
    let hint = (get-help-string "help-main-categories-hint")

    let infra_desc = (get-help-string "help-main-infrastructure-desc")
    let orch_desc = (get-help-string "help-main-orchestration-desc")
    let dev_desc = (get-help-string "help-main-development-desc")
    let ws_desc = (get-help-string "help-main-workspace-desc")
    let plat_desc = (get-help-string "help-main-platform-desc")
    let setup_desc = (get-help-string "help-main-setup-desc")
    let auth_desc = (get-help-string "help-main-authentication-desc")
    let plugins_desc = (get-help-string "help-main-plugins-desc")
    let utils_desc = (get-help-string "help-main-utilities-desc")
    let tools_desc = (get-help-string "help-main-tools-desc")
    let vm_desc = (get-help-string "help-main-vm-desc")
    let diag_desc = (get-help-string "help-main-diagnostics-desc")
    let concepts_desc = (get-help-string "help-main-concepts-desc")
    let guides_desc = (get-help-string "help-main-guides-desc")
    let int_desc = (get-help-string "help-main-integrations-desc")

    # Build output string
    let header = (
        (ansi yellow) + "════════════════════════════════════════════════════════════════════════════" + (ansi rst) + "\n" +
        "  " + (ansi cyan) + (ansi bo) + ($title) + (ansi rst) + " - " + ($subtitle) + "\n" +
        (ansi yellow) + "════════════════════════════════════════════════════════════════════════════" + (ansi rst) + "\n\n"
    )

    let categories_header = (
        (ansi green) + (ansi bo) + "📚 " + ($categories) + (ansi rst) + " " + (ansi d) + "- " + ($hint) + (ansi rst) + "\n\n"
    )

    # Build category rows: [emoji, name, alias, description]
    let rows = [
        ["🏗️", "infrastructure", "[infra]", $infra_desc],
        ["⚡", "orchestration", "[orch]", $orch_desc],
        ["🧩", "development", "[dev]", $dev_desc],
        ["📁", "workspace", "[ws]", $ws_desc],
        ["⚙️", "setup", "[st]", $setup_desc],
        ["🖥️", "platform", "[plat]", $plat_desc],
        ["🔐", "authentication", "[auth]", $auth_desc],
        ["🔌", "plugins", "[plugin]", $plugins_desc],
        ["🛠️", "utilities", "[utils]", $utils_desc],
        ["🌉", "tools", "", $tools_desc],
        ["🔍", "vm", "", $vm_desc],
        ["📚", "diagnostics", "[diag]", $diag_desc],
        ["💡", "concepts", "", $concepts_desc],
        ["📖", "guides", "[guide]", $guides_desc],
        ["🌐", "integrations", "[int]", $int_desc],
        ["📦", "build", "[bi]", "Role image build, state, and watch"],
    ]

    let categories_table = (format-categories $rows)

    print ($header + $categories_header + $categories_table)
}

# Infrastructure help
def help-infrastructure [] {
    let title = (get-help-string "help-infrastructure-title")
    let intro = (get-help-string "help-infra-intro")
    let server_header = (get-help-string "help-infra-server-header")
    let server_create = (get-help-string "help-infra-server-create")
    let server_list = (get-help-string "help-infra-server-list")
    let server_delete = (get-help-string "help-infra-server-delete")
    let server_ssh = (get-help-string "help-infra-server-ssh")
    let server_price = (get-help-string "help-infra-server-price")
    let taskserv_header = (get-help-string "help-infra-taskserv-header")
    let taskserv_create = (get-help-string "help-infra-taskserv-create")
    let taskserv_delete = (get-help-string "help-infra-taskserv-delete")
    let taskserv_list = (get-help-string "help-infra-taskserv-list")
    let taskserv_generate = (get-help-string "help-infra-taskserv-generate")
    let taskserv_updates = (get-help-string "help-infra-taskserv-updates")
    let cluster_header = (get-help-string "help-infra-cluster-header")
    let cluster_create = (get-help-string "help-infra-cluster-create")
    let cluster_delete = (get-help-string "help-infra-cluster-delete")
    let cluster_list = (get-help-string "help-infra-cluster-list")

    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +

        (ansi green) + (ansi bo) + ($server_header) + (ansi rst) + "\n" +
        $"  provisioning server create --infra <name>  - ($server_create)\n" +
        $"  provisioning server list                    - ($server_list)\n" +
        $"  provisioning server delete <server>         - ($server_delete)\n" +
        $"  provisioning server ssh <server>            - ($server_ssh)\n" +
        $"  provisioning server price                   - ($server_price)\n\n" +

        (ansi green) + (ansi bo) + ($taskserv_header) + (ansi rst) + "\n" +
        $"  provisioning taskserv create <type>         - ($taskserv_create)\n" +
        $"  provisioning taskserv delete <type>         - ($taskserv_delete)\n" +
        $"  provisioning taskserv list                  - ($taskserv_list)\n" +
        $"  provisioning taskserv generate <type>       - ($taskserv_generate)\n" +
        $"  provisioning taskserv check-updates         - ($taskserv_updates)\n\n" +

        (ansi green) + (ansi bo) + ($cluster_header) + (ansi rst) + "\n" +
        $"  provisioning cluster create <name>          - ($cluster_create)\n" +
        $"  provisioning cluster delete <name>          - ($cluster_delete)\n" +
        $"  provisioning cluster list                   - ($cluster_list)\n"
    )
}

# Orchestration help
def help-orchestration [] {
    let title = (get-help-string "help-orchestration-title")
    let intro = (get-help-string "help-orch-intro")
    let workflows_header = (get-help-string "help-orch-workflows-header")
    let workflow_list = (get-help-string "help-orch-workflow-list")
    let workflow_status = (get-help-string "help-orch-workflow-status")
    let workflow_monitor = (get-help-string "help-orch-workflow-monitor")
    let workflow_stats = (get-help-string "help-orch-workflow-stats")
    let batch_header = (get-help-string "help-orch-batch-header")
    let batch_submit = (get-help-string "help-orch-batch-submit")
    let batch_list = (get-help-string "help-orch-batch-list")
    let batch_status = (get-help-string "help-orch-batch-status")
    let control_header = (get-help-string "help-orch-control-header")
    let orch_start = (get-help-string "help-orch-start")
    let orch_stop = (get-help-string "help-orch-stop")

    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +

        (ansi green) + (ansi bo) + ($workflows_header) + (ansi rst) + "\n" +
        $"  provisioning workflow list                  - ($workflow_list)\n" +
        $"  provisioning workflow status <id>           - ($workflow_status)\n" +
        $"  provisioning workflow monitor <id>          - ($workflow_monitor)\n" +
        $"  provisioning workflow stats                 - ($workflow_stats)\n\n" +

        (ansi green) + (ansi bo) + ($batch_header) + (ansi rst) + "\n" +
        $"  provisioning batch submit <file>            - ($batch_submit)\n" +
        $"  provisioning batch list                     - ($batch_list)\n" +
        $"  provisioning batch status <id>              - ($batch_status)\n\n" +

        (ansi green) + (ansi bo) + ($control_header) + (ansi rst) + "\n" +
        $"  provisioning orchestrator start             - ($orch_start)\n" +
        $"  provisioning orchestrator stop              - ($orch_stop)\n"
    )
}

# Setup help with full Fluent support
def help-setup [] {
    let title = (get-help-string "help-setup-title")
    let intro = (get-help-string "help-setup-intro")
    let initial = (get-help-string "help-setup-initial")
    let system = (get-help-string "help-setup-system")
    let system_desc = (get-help-string "help-setup-system-desc")
    let workspace_header = (get-help-string "help-setup-workspace-header")
    let workspace_cmd = (get-help-string "help-setup-workspace-cmd")
    let workspace_desc = (get-help-string "help-setup-workspace-desc")
    let workspace_init = (get-help-string "help-setup-workspace-init")
    let provider_header = (get-help-string "help-setup-provider-header")
    let provider_cmd = (get-help-string "help-setup-provider-cmd")
    let provider_desc = (get-help-string "help-setup-provider-desc")
    let provider_support = (get-help-string "help-setup-provider-support")
    let platform_header = (get-help-string "help-setup-platform-header")
    let platform_cmd = (get-help-string "help-setup-platform-cmd")
    let platform_desc = (get-help-string "help-setup-platform-desc")
    let platform_services = (get-help-string "help-setup-platform-services")
    let modes = (get-help-string "help-setup-modes")
    let interactive = (get-help-string "help-setup-interactive")
    let config = (get-help-string "help-setup-config")
    let defaults = (get-help-string "help-setup-defaults")
    let phases = (get-help-string "help-setup-phases")
    let phase_1 = (get-help-string "help-setup-phase-1")
    let phase_2 = (get-help-string "help-setup-phase-2")
    let phase_3 = (get-help-string "help-setup-phase-3")
    let phase_4 = (get-help-string "help-setup-phase-4")
    let phase_5 = (get-help-string "help-setup-phase-5")
    let security = (get-help-string "help-setup-security")
    let security_vault = (get-help-string "help-setup-security-vault")
    let security_sops = (get-help-string "help-setup-security-sops")
    let security_cedar = (get-help-string "help-setup-security-cedar")
    let examples = (get-help-string "help-setup-examples")
    let example_system = (get-help-string "help-setup-example-system")
    let example_workspace = (get-help-string "help-setup-example-workspace")
    let example_provider = (get-help-string "help-setup-example-provider")
    let example_platform = (get-help-string "help-setup-example-platform")

    (
        (ansi magenta) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +

        (ansi green) + (ansi bo) + ($initial) + (ansi rst) + "\n" +
        "  provisioning setup system                   - " + ($system) + "\n" +
        "    " + ($system_desc) + "\n\n" +

        (ansi green) + (ansi bo) + ($workspace_header) + (ansi rst) + "\n" +
        "  " + ($workspace_cmd) + " - " + ($workspace_desc) + "\n" +
        "    " + ($workspace_init) + "\n\n" +

        (ansi green) + (ansi bo) + ($provider_header) + (ansi rst) + "\n" +
        "  " + ($provider_cmd) + " - " + ($provider_desc) + "\n" +
        "    " + ($provider_support) + "\n\n" +

        (ansi green) + (ansi bo) + ($platform_header) + (ansi rst) + "\n" +
        "  " + ($platform_cmd) + " - " + ($platform_desc) + "\n" +
        "    " + ($platform_services) + "\n\n" +

        (ansi green) + (ansi bo) + ($modes) + (ansi rst) + "\n" +
        "  " + ($interactive) + "\n" +
        "  " + ($config) + "\n" +
        "  " + ($defaults) + "\n\n" +

        (ansi cyan) + ($phases) + (ansi rst) + "\n" +
        "  " + ($phase_1) + "\n" +
        "  " + ($phase_2) + "\n" +
        "  " + ($phase_3) + "\n" +
        "  " + ($phase_4) + "\n" +
        "  " + ($phase_5) + "\n\n" +

        (ansi cyan) + ($security) + (ansi rst) + "\n" +
        "  " + ($security_vault) + "\n" +
        "  " + ($security_sops) + "\n" +
        "  " + ($security_cedar) + "\n\n" +

        (ansi green) + (ansi bo) + ($examples) + (ansi rst) + "\n" +
        "  " + ($example_system) + "\n" +
        "  " + ($example_workspace) + "\n" +
        "  " + ($example_provider) + "\n" +
        "  " + ($example_platform) + "\n"
    )
}

# Development help
def help-development [] {
    let title = (get-help-string "help-development-title")
    let intro = (get-help-string "help-development-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi blue) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Workspace help
def help-workspace [] {
    let title = (get-help-string "help-workspace-title")
    let intro = (get-help-string "help-workspace-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi green) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Platform help
def help-platform [] {
    (
        (ansi red) + (ansi bo) + "🖥️  PLATFORM SERVICES" + (ansi rst) + "\n\n" +

        (ansi green) + (ansi bo) + "[Control Center]" + (ansi rst) + " " + (ansi cyan) + (ansi bo) + "🌐 Web UI + Policy Engine" + (ansi rst) + "\n" +
        "  " + (ansi blue) + "control-center server" + (ansi rst) + "\t\t\t - Start Cedar policy engine " + (ansi cyan) + "--port 8080" + (ansi rst) + "\n" +
        "  " + (ansi blue) + "control-center policy validate" + (ansi rst) + "\t - Validate Cedar policies\n" +
        "  " + (ansi blue) + "control-center policy test" + (ansi rst) + "\t\t - Test policies with data\n" +
        "  " + (ansi blue) + "control-center compliance soc2" + (ansi rst) + "\t - SOC2 compliance check\n" +
        "  " + (ansi blue) + "control-center compliance hipaa" + (ansi rst) + "\t - HIPAA compliance check\n\n" +

        (ansi cyan) + (ansi bo) + "  🎨 Features:" + (ansi rst) + "\n" +
        "    • " + (ansi green) + "Web-based UI" + (ansi rst) + "\t - WASM-powered control center interface\n" +
        "    • " + (ansi green) + "Policy Engine" + (ansi rst) + "\t - Cedar policy evaluation and versioning\n" +
        "    • " + (ansi green) + "Compliance" + (ansi rst) + "\t - SOC2 Type II and HIPAA validation\n" +
        "    • " + (ansi green) + "Security" + (ansi rst) + "\t\t - JWT auth, MFA, RBAC, anomaly detection\n" +
        "    • " + (ansi green) + "Audit Trail" + (ansi rst) + "\t - Complete compliance audit logging\n\n" +

        (ansi green) + (ansi bo) + "[Orchestrator]" + (ansi rst) + " Hybrid Rust/Nushell Coordination\n" +
        "  " + (ansi blue) + "orchestrator start" + (ansi rst) + "    - Start orchestrator [--background]\n" +
        "  " + (ansi blue) + "orchestrator stop" + (ansi rst) + "     - Stop orchestrator\n" +
        "  " + (ansi blue) + "orchestrator status" + (ansi rst) + "   - Check if running\n" +
        "  " + (ansi blue) + "orchestrator health" + (ansi rst) + "   - Health check with diagnostics\n" +
        "  " + (ansi blue) + "orchestrator logs" + (ansi rst) + "     - View logs [--follow]\n\n" +

        (ansi green) + (ansi bo) + "[MCP Server]" + (ansi rst) + " AI-Assisted DevOps Integration\n" +
        "  " + (ansi blue) + "mcp-server start" + (ansi rst) + "      - Start MCP server [--debug]\n" +
        "  " + (ansi blue) + "mcp-server status" + (ansi rst) + "     - Check server status\n\n" +

        (ansi cyan) + (ansi bo) + "  🤖 Features:" + (ansi rst) + "\n" +
        "    • " + (ansi green) + "AI-Powered Parsing" + (ansi rst) + " - Natural language to infrastructure\n" +
        "    • " + (ansi green) + "Multi-Provider" + (ansi rst) + "\t - AWS, UpCloud, Local support\n" +
        "    • " + (ansi green) + "Ultra-Fast" + (ansi rst) + "\t - Microsecond latency, 1000x faster than Python\n" +
        "    • " + (ansi green) + "Type Safe" + (ansi rst) + "\t\t - Compile-time guarantees with zero runtime errors\n\n" +

        (ansi green) + (ansi bo) + "🌐 REST API ENDPOINTS" + (ansi rst) + "\n\n" +
        (ansi yellow) + "Control Center" + (ansi rst) + " - " + (ansi d) + "http://localhost:8080" + (ansi rst) + "\n" +
        "  • POST /policies/evaluate  - Evaluate policy decisions\n" +
        "  • GET /policies            - List all policies\n" +
        "  • GET /compliance/soc2     - SOC2 compliance check\n" +
        "  • GET /anomalies           - List detected anomalies\n\n" +

        (ansi yellow) + "Orchestrator" + (ansi rst) + " - " + (ansi d) + "http://localhost:8080" + (ansi rst) + "\n" +
        "  • GET /health                    - Health check\n" +
        "  • GET /tasks                     - List all tasks\n" +
        "  • POST /workflows/servers/create - Server workflow\n" +
        "  • POST /workflows/batch/submit   - Batch workflow\n\n" +

        (ansi d) + "💡 Control Center provides a " + (ansi cyan) + (ansi bo) + "web-based UI" + (ansi rst) + (ansi d) + " for managing policies!\n" +
        "   Access at: " + (ansi cyan) + "http://localhost:8080" + (ansi rst) + (ansi d) + " after starting the server\n" +
        "   Example: provisioning control-center server --port 8080" + (ansi rst) + "\n"
    )
}

# Authentication help
def help-authentication [] {
    let title = (get-help-string "help-authentication-title")
    let intro = (get-help-string "help-authentication-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# MFA help
def help-mfa [] {
    let title = (get-help-string "help-mfa-title")
    let intro = (get-help-string "help-mfa-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Plugins help
def help-plugins [] {
    let title = (get-help-string "help-plugins-title")
    let intro = (get-help-string "help-plugins-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi cyan) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Utilities help
def help-utilities [] {
    let title = (get-help-string "help-utilities-title")
    let intro = (get-help-string "help-utilities-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi green) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Tools help
def help-tools [] {
    let title = (get-help-string "help-tools-title")
    let intro = (get-help-string "help-tools-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# VM help
def help-vm [] {
    let title = (get-help-string "help-vm-title")
    let intro = (get-help-string "help-vm-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi green) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Diagnostics help
def help-diagnostics [] {
    let title = (get-help-string "help-diagnostics-title")
    let intro = (get-help-string "help-diagnostics-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi magenta) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Concepts help
def help-concepts [] {
    let title = (get-help-string "help-concepts-title")
    let intro = (get-help-string "help-concepts-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi yellow) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Guides help
def help-guides [] {
    let title = (get-help-string "help-guides-title")
    let intro = (get-help-string "help-guides-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi blue) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Integrations help
def help-integrations [] {
    let title = (get-help-string "help-integrations-title")
    let intro = (get-help-string "help-integrations-intro")
    let more_info = (get-help-string "help-more-info")
    (
        (ansi cyan) + (ansi bo) + ($title) + (ansi rst) + "\n\n" +
        ($intro) + "\n\n" +
        ($more_info) + "\n"
    )
}

# Build help — role image management
def help-build [] {
    (
        (ansi yellow) + (ansi bo) + "🏗️  BUILD — Role Image Management" + (ansi rst) + "\n\n" +

        (ansi d) + "Pre-built provider snapshots (nixos-generators → Hetzner snapshot).\n" +
        "Snapshot IDs and freshness tracked in ~/.config/provisioning/images/.\n" +
        "Server creation runs a pre-flight check before rendering templates." + (ansi rst) + "\n\n" +

        (ansi green) + (ansi bo) + "[Image Lifecycle]" + (ansi rst) + "\n" +
        "  " + (ansi blue) + "build image create <role>" + (ansi rst) + "   - Build snapshot for role, save state\n" +
        "                                    Options: --infra <path>  --check  --provider <p>\n" +
        "  " + (ansi blue) + "build image list" + (ansi rst) + "            - Show all role states (provider, snapshot_id, fresh)\n" +
        "                                    Options: --provider <p>\n" +
        "  " + (ansi blue) + "build image update <role>" + (ansi rst) + "  - Delete stale snapshot and rebuild\n" +
        "                                    Options: --infra <path>  --provider <p>  --check\n" +
        "  " + (ansi blue) + "build image delete <role>" + (ansi rst) + "  - Remove snapshot from provider + local state\n" +
        "                                    Options: --provider <p>  --yes\n\n" +

        (ansi green) + (ansi bo) + "[Monitoring]" + (ansi rst) + "\n" +
        "  " + (ansi blue) + "build image watch" + (ansi rst) + "          - Poll freshness of all role images (loop)\n" +
        "                                    Options: --interval <min>  --auto-build  --notify-only\n" +
        "                                             --provider <p>    --infra <path>\n\n" +

        (ansi green) + (ansi bo) + "[Shortcuts]" + (ansi rst) + "\n" +
        "  " + (ansi d) + "b, build" + (ansi rst) + "         → build domain\n" +
        "  " + (ansi d) + "bi, build-image" + (ansi rst) + "  → build image\n\n" +

        (ansi green) + (ansi bo) + "[Examples]" + (ansi rst) + "\n" +
        "  provisioning build image list\n" +
        "  provisioning build image create cp --infra workspaces/librecloud_hetzner/infra/wuji --check\n" +
        "  provisioning build image create cp --infra workspaces/librecloud_hetzner/infra/wuji\n" +
        "  provisioning build image update worker --infra workspaces/librecloud_hetzner/infra/wuji\n" +
        "  provisioning build image delete storage --yes\n" +
        "  provisioning build image watch --interval 30 --auto-build\n\n" +

        (ansi green) + (ansi bo) + "[State Files]" + (ansi rst) + "\n" +
        "  Location: ~/.config/provisioning/images/<provider>-<role>.ncl\n" +
        "  Schema:   provisioning/schemas/infrastructure/images/\n" +
        "  Workspace roles: workspaces/librecloud_hetzner/infra/wuji/images.ncl\n"
    )
}

# Main entry point
def main [...args: string] {
    let category = if ($args | length) > 0 { ($args | get 0) } else { "" }
    let help_text = (provisioning-help $category)
    print $help_text
}
