package notifications

import (
	"context"
	"encoding/json"

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

type ConfirmationRequestProcessor struct {
	logger  mlogger.Logger
	handler ch.ConfirmationRequestHandler
	event   model.NotificationEvent
}

func (crp *ConfirmationRequestProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.ConfirmationRequest
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		crp.logger.Warn("Failed to decode confirmation request envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
		return err
	}
	if crp.handler == nil {
		crp.logger.Warn("Confirmation request handler is not configured", zap.String("topic", crp.event.ToString()))
		return nil
	}
	return crp.handler(ctx, &msg)
}

func (crp *ConfirmationRequestProcessor) GetSubject() model.NotificationEvent {
	return crp.event
}

type ConfirmationResultProcessor struct {
	logger  mlogger.Logger
	handler ch.ConfirmationResultHandler
	event   model.NotificationEvent
}

func (crp *ConfirmationResultProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.ConfirmationResult
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		crp.logger.Warn("Failed to decode confirmation result envelope", zap.Error(err), zap.String("topic", crp.event.ToString()))
		return err
	}
	if crp.handler == nil {
		crp.logger.Warn("Confirmation result handler is not configured", zap.String("topic", crp.event.ToString()))
		return nil
	}
	return crp.handler(ctx, &msg)
}

func (crp *ConfirmationResultProcessor) GetSubject() model.NotificationEvent {
	return crp.event
}

type ConfirmationDispatchProcessor struct {
	logger  mlogger.Logger
	handler ch.ConfirmationDispatchHandler
	event   model.NotificationEvent
}

func (cdp *ConfirmationDispatchProcessor) Process(ctx context.Context, envelope me.Envelope) error {
	var msg model.ConfirmationRequestDispatch
	if err := json.Unmarshal(envelope.GetData(), &msg); err != nil {
		cdp.logger.Warn("Failed to decode confirmation dispatch envelope", zap.Error(err), zap.String("topic", cdp.event.ToString()))
		return err
	}
	if cdp.handler == nil {
		cdp.logger.Warn("Confirmation dispatch handler is not configured", zap.String("topic", cdp.event.ToString()))
		return nil
	}
	return cdp.handler(ctx, &msg)
}

func (cdp *ConfirmationDispatchProcessor) GetSubject() model.NotificationEvent {
	return cdp.event
}

func NewConfirmationRequestProcessor(logger mlogger.Logger, handler ch.ConfirmationRequestHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("confirmation_request_processor")
	}
	return &ConfirmationRequestProcessor{
		logger:  logger,
		handler: handler,
		event:   confirmationRequestEvent(),
	}
}

func NewConfirmationResultProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationResultHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("confirmation_result_processor")
	}
	return &ConfirmationResultProcessor{
		logger:  logger,
		handler: handler,
		event:   confirmationResultEvent(sourceService, rail),
	}
}

func NewConfirmationDispatchProcessor(logger mlogger.Logger, sourceService, rail string, handler ch.ConfirmationDispatchHandler) np.EnvelopeProcessor {
	if logger != nil {
		logger = logger.Named("confirmation_dispatch_processor")
	}
	return &ConfirmationDispatchProcessor{
		logger:  logger,
		handler: handler,
		event:   confirmationDispatchEvent(sourceService, rail),
	}
}
