package notifications

import (
	"context"
	"encoding/json"

	me "github.com/tech/sendico/pkg/messaging/envelope"
	np "github.com/tech/sendico/pkg/messaging/notifications/processor"
	ch "github.com/tech/sendico/pkg/messaging/notifications/telegram/handler"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/model"
	"go.uber.org/zap"
)

type TelegramReactionProcessor struct {
	logger  mlogger.Logger
	handler ch.TelegramReactionHandler
	event   model.NotificationEvent
}

func (trp *TelegramReactionProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.TelegramReactionRequest
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		trp.logger.Warn("Failed to decode telegram reaction envelope", zap.Error(err), zap.String("topic", trp.event.ToString()))
		return err
	}
	if trp.handler == nil {
		trp.logger.Warn("Telegram reaction handler is not configured", zap.String("topic", trp.event.ToString()))
		return nil
	}
	return trp.handler(ctx, &msg)
}

func (trp *TelegramReactionProcessor) GetSubject() model.NotificationEvent {
	return trp.event
}

func NewTelegramReactionProcessor(logger mlogger.Logger, handler ch.TelegramReactionHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("telegram_reaction_processor")
	}
	return &TelegramReactionProcessor{
		logger:  logger,
		handler: handler,
		event:   telegramReactionEvent(),
	}
}

type TelegramTextProcessor struct {
	logger  mlogger.Logger
	handler ch.TelegramTextHandler
	event   model.NotificationEvent
}

func (ttp *TelegramTextProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.TelegramTextRequest
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		ttp.logger.Warn("Failed to decode telegram text envelope", zap.Error(err), zap.String("topic", ttp.event.ToString()))
		return err
	}
	if ttp.handler == nil {
		ttp.logger.Warn("Telegram text handler is not configured", zap.String("topic", ttp.event.ToString()))
		return nil
	}
	return ttp.handler(ctx, &msg)
}

func (ttp *TelegramTextProcessor) GetSubject() model.NotificationEvent {
	return ttp.event
}

func NewTelegramTextProcessor(logger mlogger.Logger, handler ch.TelegramTextHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("telegram_text_processor")
	}
	return &TelegramTextProcessor{
		logger:  logger,
		handler: handler,
		event:   telegramTextEvent(),
	}
}

type TelegramUpdateProcessor struct {
	logger  mlogger.Logger
	handler ch.TelegramUpdateHandler
	event   model.NotificationEvent
}

func (tup *TelegramUpdateProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.TelegramWebhookUpdate
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		tup.logger.Warn("Failed to decode telegram webhook update envelope", zap.Error(err), zap.String("topic", tup.event.ToString()))
		return err
	}
	if tup.handler == nil {
		tup.logger.Warn("Telegram update handler is not configured", zap.String("topic", tup.event.ToString()))
		return nil
	}
	return tup.handler(ctx, &msg)
}

func (tup *TelegramUpdateProcessor) GetSubject() model.NotificationEvent {
	return tup.event
}

func NewTelegramUpdateProcessor(logger mlogger.Logger, handler ch.TelegramUpdateHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("telegram_update_processor")
	}
	return &TelegramUpdateProcessor{
		logger:  logger,
		handler: handler,
		event:   telegramUpdateEvent(),
	}
}
