package bot

import (
	"context"
	"strings"

	tgbot "github.com/go-telegram/bot"
	"github.com/go-telegram/bot/models"
	"go.uber.org/zap"
)

// The /support command replies with a fixed support-desk info message — the operators'
// working hours and what to include (a description and, when possible, screenshots). It
// is a dedicated command handler (registered like /start), so it intercepts "/support"
// before the support relay: the command line itself is not forwarded into an operator
// topic, while the user's following description still is.

// handleSupport replies to /support with the localized support-desk info message. Like
// handleStart it answers only in a private chat — in the moderated group the bot never
// chats — and it sends a plain text message with no launch button.
func (t *Bot) handleSupport(ctx context.Context, api *tgbot.Bot, update *models.Update) {
	if update.Message == nil {
		return
	}
	if update.Message.Chat.Type != models.ChatTypePrivate {
		return
	}
	// The sender's Telegram language rides on the message itself (Message.from.language_code);
	// fall back to English when it is absent, matching startText.
	lang := ""
	if update.Message.From != nil {
		lang = update.Message.From.LanguageCode
	}
	if _, err := api.SendMessage(ctx, &tgbot.SendMessageParams{
		ChatID: update.Message.Chat.ID,
		Text:   supportText(lang),
	}); err != nil {
		t.log.Warn("reply to support failed", zap.Error(err))
	}
}

// supportText returns the localized /support reply. Russian is used when lang (the IETF
// language tag the Telegram client reports on the message's sender) starts with "ru",
// English otherwise and when it is absent — mirroring startText's language choice.
func supportText(lang string) string {
	if strings.HasPrefix(strings.ToLower(lang), "ru") {
		return ruSupport
	}
	return enSupport
}

// ruSupport and enSupport are the Russian and English /support replies; the English one
// is the fallback for any non-Russian or missing sender language.
const (
	ruSupport = "Поддержка «Эрудита» на связи с 08:00 до 18:00 (по времени UTC). " +
		"Если Вы столкнулись с проблемой, подробно опишите её и добавьте, по возможности, скриншоты. " +
		"Также будем рады выслушать Ваши пожелания и предложения по функционалу игры. " +
		"Ответим в самое ближайшее время."
	enSupport = "“Erudite” support is available from 08:00 to 18:00 (UTC). " +
		"If you have run into a problem, please describe it in detail and attach screenshots if you can. " +
		"We would also be glad to hear your wishes and suggestions about the game's features. " +
		"We will reply as soon as possible."
)
