package bot

import "strings"

type Command string

const (
	CommandStart    Command = "start"
	CommandHelp     Command = "help"
	CommandFund     Command = "fund"
	CommandWithdraw Command = "withdraw"
	CommandConfirm  Command = "confirm"
	CommandCancel   Command = "cancel"
)

var supportedCommands = []Command{
	CommandStart,
	CommandHelp,
	CommandFund,
	CommandWithdraw,
	CommandConfirm,
	CommandCancel,
}

func (c Command) Slash() string {
	name := strings.TrimSpace(string(c))
	if name == "" {
		return ""
	}
	return "/" + name
}

func parseCommand(text string) Command {
	text = strings.TrimSpace(text)
	if !strings.HasPrefix(text, "/") {
		return ""
	}
	token := text
	if idx := strings.IndexAny(token, " \t\n\r"); idx >= 0 {
		token = token[:idx]
	}
	token = strings.TrimPrefix(token, "/")
	if idx := strings.Index(token, "@"); idx >= 0 {
		token = token[:idx]
	}
	return Command(strings.ToLower(strings.TrimSpace(token)))
}

func supportedCommandsMessage() string {
	lines := make([]string, 0, len(supportedCommands)+2)
	lines = append(lines, "*Supported Commands*")
	lines = append(lines, "")
	for _, cmd := range supportedCommands {
		lines = append(lines, markdownCommand(cmd))
	}
	return strings.Join(lines, "\n")
}

func confirmationCommandsMessage() string {
	return strings.Join([]string{
		"*Confirm Operation*",
		"",
		"Use " + markdownCommand(CommandConfirm) + " to execute.",
		"Use " + markdownCommand(CommandCancel) + " to abort.",
	}, "\n")
}

func helpMessage(accountCode string, currency string) string {
	accountCode = strings.TrimSpace(accountCode)
	currency = strings.ToUpper(strings.TrimSpace(currency))
	if accountCode == "" {
		accountCode = "N/A"
	}
	if currency == "" {
		currency = "N/A"
	}

	lines := []string{
		"*Treasury Bot Help*",
		"",
		"*Attached account:* " + markdownCode(accountCode) + " (" + markdownCode(currency) + ")",
		"",
		"*How to use*",
		"1. Start funding with " + markdownCommand(CommandFund) + " or withdrawal with " + markdownCommand(CommandWithdraw) + ".",
		"2. Enter amount as decimal with dot separator and no currency.",
		"   Example: " + markdownCode("1250.75"),
		"3. Confirm with " + markdownCommand(CommandConfirm) + " or abort with " + markdownCommand(CommandCancel) + ".",
		"",
		"*Cooldown*",
		"After confirmation there is a cooldown window. You can cancel during it with " + markdownCommand(CommandCancel) + ".",
		"You will receive a follow-up message with execution success or failure.",
	}
	return strings.Join(lines, "\n")
}
