package notificationimp

import (
	"context"
	"strings"

	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/model"
	"go.uber.org/zap"
)

func (a *NotificationAPI) onTelegramReaction(ctx context.Context, request *model.TelegramReactionRequest) error {
	if request == nil {
		return merrors.InvalidArgument("telegram reaction request is nil", "request")
	}
	if a.tg == nil {
		return merrors.Internal("telegram client is not configured")
	}
	chatID := strings.TrimSpace(request.ChatID)
	messageID := strings.TrimSpace(request.MessageID)
	if chatID == "" {
		return merrors.InvalidArgument("telegram chat_id is required", "chat_id")
	}
	if messageID == "" {
		return merrors.InvalidArgument("telegram message_id is required", "message_id")
	}
	emoji := strings.TrimSpace(request.Emoji)
	if emoji == "" {
		return merrors.InvalidArgument("telegram emoji is required", "emoji")
	}
	if err := a.tg.SetMessageReaction(ctx, chatID, messageID, emoji); err != nil {
		a.logger.Warn("Failed to send telegram reaction",
			zap.Error(err),
			zap.String("request_id", strings.TrimSpace(request.RequestID)),
			zap.String("chat_id", chatID),
			zap.String("message_id", messageID),
			zap.String("emoji", emoji))
		return err
	}
	a.logger.Info("Telegram reaction sent",
		zap.String("request_id", strings.TrimSpace(request.RequestID)),
		zap.String("chat_id", chatID),
		zap.String("message_id", messageID),
		zap.String("emoji", emoji))
	return nil
}
