# Intelligent Hints and Next-Step Guidance System
# Provides contextual hints, documentation links, and next-step suggestions

use interface.nu [_ansi]

# Show next step suggestion after successful operation
export def show-next-step [
    operation: string   # Operation that just completed
    context?: record    # Optional context (infra, taskserv name, etc)
] {
    let ctx = ($context | default {})

    match $operation {
        "server_create" => {
            print $"\n(_ansi green_bold)✓ Servers created successfully!(_ansi reset)\n"
            print $"(_ansi cyan_bold)Next steps:(_ansi reset)"
            print $"  1. (_ansi blue)Install task services:(_ansi reset)  provisioning taskserv create <service>"
            print $"     (_ansi default_dimmed)Popular services: kubernetes, redis, postgres, containerd(_ansi reset)"
            print $"  2. (_ansi blue)SSH into servers:(_ansi reset)       provisioning server ssh <hostname>"
            print $"  3. (_ansi blue)List servers:(_ansi reset)           provisioning server list"
            print $"\n(_ansi yellow)💡 Quick guide:(_ansi reset) provisioning guide from-scratch"
            print $"(_ansi yellow)💡 Documentation:(_ansi reset) provisioning help infrastructure\n"
        }

        "taskserv_create" => {
            let service_name = ($ctx | get name? | default "service")
            print $"\n(_ansi green_bold)✓ Taskserv '($service_name)' installed successfully!(_ansi reset)\n"
            print $"(_ansi cyan_bold)Next steps:(_ansi reset)"
            print $"  1. (_ansi blue)Dry-run check:(_ansi reset)          provisioning taskserv create ($service_name) --check"
            print $"  2. (_ansi blue)Install more services:(_ansi reset)  provisioning taskserv create <service>"
            print $"  3. (_ansi blue)Create cluster:(_ansi reset)         provisioning cluster create <cluster-name>"
            print $"\n(_ansi yellow)💡 Quick guide:(_ansi reset) provisioning guide from-scratch"
            print $"(_ansi yellow)💡 Documentation:(_ansi reset) provisioning help infrastructure\n"
        }

        "workspace_init" => {
            let ws_name = ($ctx | get name? | default "workspace")
            print $"\n(_ansi green_bold)✓ Workspace '($ws_name)' initialized successfully!(_ansi reset)\n"
            print $"(_ansi cyan_bold)Next steps:(_ansi reset)"
            print $"  1. (_ansi blue)Discover modules:(_ansi reset)       provisioning module discover taskservs"
            print $"  2. (_ansi blue)Configure environment:(_ansi reset)  provisioning workspace config edit main"
            print $"  3. (_ansi blue)Create servers:(_ansi reset)         provisioning server create --infra ($ws_name)"
            print $"\n(_ansi yellow)💡 Quick start:(_ansi reset) provisioning sc"
            print $"(_ansi yellow)💡 Complete guide:(_ansi reset) provisioning guide from-scratch"
            print $"(_ansi yellow)💡 Documentation:(_ansi reset) provisioning help workspace\n"
        }

        "cluster_create" => {
            let cluster_name = ($ctx | get name? | default "cluster")
            print $"\n(_ansi green_bold)✓ Cluster '($cluster_name)' created successfully!(_ansi reset)\n"
            print $"(_ansi cyan_bold)Next steps:(_ansi reset)"
            print $"  1. (_ansi blue)Verify deployment:(_ansi reset)      provisioning cluster list"
            print $"  2. (_ansi blue)Check workflows:(_ansi reset)        provisioning workflow list"
            print $"  3. (_ansi blue)Monitor status:(_ansi reset)         provisioning workflow monitor <id>"
            print $"\n(_ansi yellow)💡 Documentation:(_ansi reset) provisioning help orchestration\n"
        }

        "workspace_activate" => {
            let ws_name = ($ctx | get name? | default "workspace")
            print $"\n(_ansi green_bold)✓ Workspace '($ws_name)' activated!(_ansi reset)\n"
            print $"(_ansi cyan_bold)All provisioning commands will now use this workspace.(_ansi reset)\n"
            print $"(_ansi cyan_bold)Next steps:(_ansi reset)"
            print $"  1. (_ansi blue)View workspace info:(_ansi reset)    provisioning workspace info"
            print $"  2. (_ansi blue)List servers:(_ansi reset)           provisioning server list"
            print $"  3. (_ansi blue)Check configuration:(_ansi reset)    provisioning workspace config show"
            print $"\n(_ansi yellow)💡 Documentation:(_ansi reset) provisioning help workspace\n"
        }

        _ => {
            print $"\n(_ansi green_bold)✓ Operation completed successfully!(_ansi reset)\n"
        }
    }
}

# Show documentation link reference
export def show-doc-link [
    topic: string       # Documentation topic
    --compact (-c)      # Compact format (no newlines)
] {
    let link = match $topic {
        "infrastructure" | "infra" => "provisioning help infrastructure"
        "orchestration" | "orch" => "provisioning help orchestration"
        "workspace" | "ws" => "provisioning help workspace"
        "development" | "dev" => "provisioning help development"
        "authentication" | "auth" => "provisioning help authentication"
        "guides" | "guide" => "provisioning help guides"
        "quickstart" => "provisioning guide quickstart"
        "from-scratch" => "provisioning guide from-scratch"
        "concepts" => "provisioning help concepts"
        _ => $"provisioning help ($topic)"
    }

    if $compact {
        $"(_ansi yellow)💡 See:(_ansi reset) ($link)"
    } else {
        $"\n(_ansi yellow)💡 Documentation:(_ansi reset) ($link)\n"
    }
}

# Show contextual example based on operation
export def show-example [
    operation: string   # Operation type
    context?: record    # Optional context
] {
    let ctx = ($context | default {})

    match $operation {
        "server_create" => {
            print $"\n(_ansi cyan_bold)Examples:(_ansi reset)"
            print $"  # Create servers with check mode"
            print $"  provisioning server create --check"
            print $""
            print $"  # Create servers and wait for completion"
            print $"  provisioning server create --wait"
            print $""
            print $"  # Create for specific infrastructure"
            print $"  provisioning server create --infra my-project\n"
        }

        "taskserv_create" => {
            print $"\n(_ansi cyan_bold)Examples:(_ansi reset)"
            print $"  # Install Kubernetes"
            print $"  provisioning taskserv create kubernetes"
            print $""
            print $"  # Install Redis with check mode"
            print $"  provisioning taskserv create redis --check"
            print $""
            print $"  # Install Postgres on specific infrastructure"
            print $"  provisioning taskserv create postgres --infra my-project\n"
        }

        "workspace_init" => {
            print $"\n(_ansi cyan_bold)Examples:(_ansi reset)"
            print $"  # Initialize and activate workspace"
            print $"  provisioning workspace init my-project --activate"
            print $""
            print $"  # Interactive workspace creation"
            print $"  provisioning workspace init --interactive"
            print $""
            print $"  # Initialize specific path"
            print $"  provisioning workspace init /path/to/workspace --activate\n"
        }

        "module_discover" => {
            print $"\n(_ansi cyan_bold)Examples:(_ansi reset)"
            print $"  # Discover all taskservs"
            print $"  provisioning module discover taskservs"
            print $""
            print $"  # Discover providers"
            print $"  provisioning module discover providers"
            print $""
            print $"  # Discover clusters"
            print $"  provisioning module discover clusters\n"
        }

        _ => {
            print $"\n(_ansi yellow)💡 Use --help for command options(_ansi reset)\n"
        }
    }
}

# Enhanced error message with suggestion and documentation link
export def show-error [
    error_type: string  # Type of error
    message: string     # Error message
    suggestion?: string # Suggestion to fix
    doc_topic?: string  # Documentation topic
] {
    print $"(_ansi red_bold)❌ Error:(_ansi reset) ($message)"

    if ($suggestion | is-not-empty) {
        print $"(_ansi yellow_bold)💡 Suggestion:(_ansi reset) ($suggestion)"
    }

    if ($doc_topic | is-not-empty) {
        print (show-doc-link $doc_topic --compact)
    }

    # Provide common error-specific help
    match $error_type {
        "workspace_not_found" => {
            print $"(_ansi cyan)Try:(_ansi reset) provisioning workspace init <name> --activate"
        }

        "no_servers" => {
            print $"(_ansi cyan)Try:(_ansi reset) provisioning server create --infra <infrastructure>"
        }

        "taskserv_not_found" => {
            print $"(_ansi cyan)Available:(_ansi reset) provisioning taskserv list"
            print $"(_ansi cyan)Discover:(_ansi reset) provisioning module discover taskservs"
        }

        "invalid_config" => {
            print $"(_ansi cyan)Validate:(_ansi reset) provisioning validate config"
            print $"(_ansi cyan)Show:(_ansi reset) provisioning workspace config show"
        }

        "orchestrator_not_running" => {
            print $"(_ansi cyan)Start:(_ansi reset) provisioning orchestrator start --background"
            print $"(_ansi cyan)Status:(_ansi reset) provisioning orchestrator status"
        }

        _ => {}
    }

    print ""
}

# Show contextual hint based on system state
export def show-contextual-hint [
    state: record       # System state from diagnostics
] {
    # Check various conditions and provide hints
    if ($state | get workspace? | default null | is-empty) {
        print $"(_ansi yellow_bold)💡 Hint:(_ansi reset) No workspace detected. Initialize one with:"
        print $"   provisioning workspace init my-project --activate\n"
        return
    }

    if (($state | get servers? | default [] | length) == 0) {
        print $"(_ansi yellow_bold)💡 Hint:(_ansi reset) No servers found. Create some with:"
        print $"   provisioning server create\n"
        return
    }

    if (($state | get taskservs? | default [] | length) == 0) {
        print $"(_ansi yellow_bold)💡 Hint:(_ansi reset) No taskservs installed. Install services with:"
        print $"   provisioning taskserv create <service>\n"
        print $"   Available: kubernetes, redis, postgres, containerd\n"
        return
    }
}

# Format success message with icon
export def success-message [
    message: string
    --icon: string = "✓"
] {
    print $"(_ansi green_bold)($icon) ($message)(_ansi reset)"
}

# Format warning message with icon
export def warning-message [
    message: string
    --icon: string = "⚠️"
] {
    print $"(_ansi yellow_bold)($icon) ($message)(_ansi reset)"
}

# Format info message with icon
export def info-message [
    message: string
    --icon: string = "ℹ️"
] {
    print $"(_ansi cyan_bold)($icon) ($message)(_ansi reset)"
}

# Show progress indicator
export def show-progress [
    step: int           # Current step
    total: int          # Total steps
    message: string     # Progress message
] {
    let percentage = (($step / $total) * 100 | math round)
    print $"(_ansi cyan)[($step)/($total) - ($percentage)%](_ansi reset) ($message)"
}

# Show quick tip
export def show-tip [
    message: string
    --compact (-c)
] {
    if $compact {
        print $"(_ansi yellow)💡 Tip:(_ansi reset) ($message)"
    } else {
        print $"\n(_ansi yellow_bold)💡 Tip:(_ansi reset) ($message)\n"
    }
}
