package mcp

import (
	"fmt"
	"strings"
)

// ErrorWithSuggestions represents an error with tool suggestions
type ErrorWithSuggestions struct {
	Message     string
	Suggestions []string
}

// Error returns the error message with suggestions
func (e *ErrorWithSuggestions) Error() string {
	if len(e.Suggestions) == 0 {
		return e.Message
	}

	var sb strings.Builder
	sb.WriteString(e.Message)
	sb.WriteString("\n\nDid you mean to use one of these tools instead?\n")
	for _, suggestion := range e.Suggestions {
		sb.WriteString("  - ")
		sb.WriteString(suggestion)
		sb.WriteString("\n")
	}
	return sb.String()
}

// NewErrorWithSuggestions creates a new error with tool suggestions
func NewErrorWithSuggestions(message string, suggestions ...string) error {
	return &ErrorWithSuggestions{
		Message:     message,
		Suggestions: suggestions,
	}
}

// WorktreeNotFoundError returns an error with suggestions for when a worktree is not found
func WorktreeNotFoundError(identifier string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("worktree not found: %s", identifier),
		"resource_worktree_list - List all available worktrees",
		"worktree_add - Create a new worktree",
	)
}

// SessionNotFoundError returns an error with suggestions for when a session is not found
func SessionNotFoundError(identifier string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("session not found: %s", identifier),
		"resource_session_list - List all active sessions",
		"session_run - Start a new session",
	)
}

// NoWorktreesError returns an error with suggestions for when no worktrees exist
func NoWorktreesError() error {
	return NewErrorWithSuggestions(
		"no worktrees found",
		"worktree_add - Create your first worktree",
		"resource_worktree_list - Verify worktree status",
	)
}

// FileNotFoundError returns an error with suggestions for when a file is not found
func FileNotFoundError(path string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("file not found: %s", path),
		"resource_worktree_browse - Browse worktree files",
	)
}

// DirectoryNotFoundError returns an error with suggestions for when a directory is not found
func DirectoryNotFoundError(path string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("directory not found: %s", path),
		"resource_worktree_browse - Browse worktree structure",
	)
}

// SessionNotRunningError returns an error with suggestions for when a session is not running
func SessionNotRunningError(identifier string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("session %s is not running", identifier),
		"session_run - Start a new session",
		"resource_session_list - Check session status",
		"resource_session_output - View session output",
	)
}

// InvalidParameterError returns an error with suggestions for invalid parameters
func InvalidParameterError(param string, expected string) error {
	return NewErrorWithSuggestions(
		fmt.Sprintf("invalid %s: expected %s", param, expected),
		"Use the tool descriptions to understand parameter requirements",
		"Check examples in the tool description",
	)
}
