# Guide Command Handlers
# Domain: Interactive guide system for step-by-step instructions

# Guide command handler - Show interactive guides
export def handle_guide [ops: string, flags: record] {
    let guide_topic = if ($ops | is-not-empty) {
        ($ops | split row " " | get 0)
    } else {
        ""
    }

    # Define guide topics and their paths
    let guides = {
        "quickstart": "docs/guides/quickstart-cheatsheet.md",
        "from-scratch": "docs/guides/from-scratch.md",
        "scratch": "docs/guides/from-scratch.md",
        "start": "docs/guides/from-scratch.md",
        "deploy": "docs/guides/from-scratch.md",
        "list": "list_guides"
    }

    # Get docs directory
    let docs_dir = ($env.PROVISIONING_PATH | path join "docs" "guides")

    match $guide_topic {
        "" => {
            # Show guide list
            show_guide_list $docs_dir
        }

        "list" => {
            show_guide_list $docs_dir
        }

        _ => {
            # Try to find and display guide
            let guide_path = if ($guide_topic in ($guides | columns)) { $guides | get $guide_topic } else { null }

            if ($guide_path == null or $guide_path == "list_guides") {
                print $"(_ansi red)❌ Unknown guide:(_ansi reset) ($guide_topic)"
                print ""
                show_guide_list $docs_dir
                exit 1
            }

            let full_path = ($env.PROVISIONING_PATH | path join $guide_path)

            if not ($full_path | path exists) {
                print $"(_ansi red)❌ Guide file not found:(_ansi reset) ($full_path)"
                exit 1
            }

            # Display guide using best available viewer
            display_guide $full_path $guide_topic
        }
    }
}

# Display guide using best available markdown viewer
def display_guide [
    guide_path: path
    topic: string
] {
    print $"\n(_ansi cyan_bold)📖 Guide:(_ansi reset) ($topic)\n"

    # Check for viewers in order of preference: glow, bat, less, cat
    if (which glow | length) > 0 {
        ^glow $guide_path
    } else if (which bat | length) > 0 {
        ^bat --style=plain --paging=always $guide_path
    } else if (which less | length) > 0 {
        ^less $guide_path
    } else {
        open $guide_path
    }
}

# Show list of available guides
def show_guide_list [docs_dir: path] {
    print $"
(_ansi magenta_bold)╔══════════════════════════════════════════════════╗(_ansi reset)
(_ansi magenta_bold)║(_ansi reset)  📚 AVAILABLE GUIDES                             (_ansi magenta_bold)║(_ansi reset)
(_ansi magenta_bold)╚══════════════════════════════════════════════════╝(_ansi reset)

(_ansi green_bold)[Step-by-Step Guides](_ansi reset)

  (_ansi blue)provisioning guide from-scratch(_ansi reset)
    Complete deployment from zero to production
    (_ansi default_dimmed)Shortcuts: scratch, start, deploy(_ansi reset)

(_ansi green_bold)[Quick References](_ansi reset)

  (_ansi blue)provisioning guide quickstart(_ansi reset)
    Command shortcuts and quick reference
    (_ansi default_dimmed)Shortcuts: shortcuts, quick(_ansi reset)

(_ansi green_bold)USAGE(_ansi reset)

  # View guide
  provisioning guide <topic>

  # List all guides
  provisioning guide list
  provisioning howto        (_ansi default_dimmed)# shortcut(_ansi reset)

(_ansi green_bold)EXAMPLES(_ansi reset)

  # Complete deployment guide
  provisioning guide from-scratch

  # Quick command reference
  provisioning guide quickstart

(_ansi green_bold)VIEWING TIPS(_ansi reset)

  • (_ansi cyan)Best experience:(_ansi reset) Install glow for beautiful rendering
    (_ansi default_dimmed)brew install glow  # macOS(_ansi reset)

  • (_ansi cyan)Alternative:(_ansi reset) bat provides syntax highlighting
    (_ansi default_dimmed)brew install bat   # macOS(_ansi reset)

  • (_ansi cyan)Fallback:(_ansi reset) less/cat work on all systems

(_ansi default_dimmed)💡 All guides provide copy-paste ready commands
   Perfect for quick start and reference!(_ansi reset)
"
}
