package mailimp

import (
	"errors"
	"testing"

	mb "github.com/tech/sendico/notification/internal/server/notificationimp/mail/internal/builder"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger/factory"
)

func TestNewDummy_CreatesValidClient(t *testing.T) {
	logger := mlogger.NewLogger(true)
	dummy, err := NewDummy(logger)

	if err != nil {
		t.Fatalf("Unexpected error creating dummy client: %v", err)
	}
	if dummy == nil {
		t.Fatal("Expected non-nil dummy client")
	}
}

func TestDummy_Send_ReturnsNotImplementedError(t *testing.T) {
	logger := mlogger.NewLogger(true)
	dummy, err := NewDummy(logger)
	if err != nil {
		t.Fatalf("Failed to create dummy client: %v", err)
	}

	builder := mb.NewMessageBuilder()
	err = dummy.Send(builder)

	if err == nil {
		t.Fatal("Expected error when calling Send on dummy client")
	}

	if !errors.Is(err, merrors.ErrNotImplemented) {
		t.Errorf("Expected NotImplemented error, got: %v", err)
	}
}

func TestDummy_MailBuilder_ReturnsValidBuilder(t *testing.T) {
	logger := mlogger.NewLogger(true)
	dummy, err := NewDummy(logger)
	if err != nil {
		t.Fatalf("Failed to create dummy client: %v", err)
	}

	builder := dummy.MailBuilder()

	if builder == nil {
		t.Fatal("Expected non-nil mail builder")
	}
}

func TestDummy_MailBuilder_CanBuildMessage(t *testing.T) {
	logger := mlogger.NewLogger(true)
	dummy, err := NewDummy(logger)
	if err != nil {
		t.Fatalf("Failed to create dummy client: %v", err)
	}

	builder := dummy.MailBuilder()
	msg, err := builder.
		AddRecipient("Test User", "test@example.com").
		SetTemplateID("welcome").
		Build()

	if err != nil {
		t.Fatalf("Unexpected error building message: %v", err)
	}
	if msg == nil {
		t.Fatal("Expected non-nil message")
	}
}

func TestDummy_MultipleSendCalls_AllReturnError(t *testing.T) {
	logger := mlogger.NewLogger(true)
	dummy, err := NewDummy(logger)
	if err != nil {
		t.Fatalf("Failed to create dummy client: %v", err)
	}

	builder1 := dummy.MailBuilder()
	builder2 := dummy.MailBuilder()

	err1 := dummy.Send(builder1)
	err2 := dummy.Send(builder2)

	if err1 == nil || err2 == nil {
		t.Error("Expected all Send calls to return errors")
	}

	if !errors.Is(err1, merrors.ErrNotImplemented) || !errors.Is(err2, merrors.ErrNotImplemented) {
		t.Error("Expected all errors to be NotImplemented")
	}
}
