package telegram

import (
	"reflect"
	"testing"

	"github.com/tech/sendico/pkg/model"
)

func TestNewContactRequestTemplate_SignupTopic(t *testing.T) {
	template := newContactRequestTemplate(&model.ContactRequest{
		Name:    "Alice Example",
		Email:   "alice@example.com",
		Company: "Acme Inc",
		Topic:   model.ContactRequestTopicSignupCompleted,
	})

	if template.title != "New signup completed" {
		t.Fatalf("unexpected title: %s", template.title)
	}
	if !reflect.DeepEqual(template.emphasize, []string{"signup completed"}) {
		t.Fatalf("unexpected emphasize words: %#v", template.emphasize)
	}

	expectedFields := []messageField{
		{label: "Organization", value: "Acme Inc"},
		{label: "Name", value: "Alice Example"},
		{label: "Email", value: "alice@example.com"},
	}
	if !reflect.DeepEqual(template.fields, expectedFields) {
		t.Fatalf("unexpected fields: %#v", template.fields)
	}
}

func TestNewContactRequestTemplate_LegacySignupTopic(t *testing.T) {
	template := newContactRequestTemplate(&model.ContactRequest{
		Name:    "Alice Example",
		Email:   "alice@example.com",
		Company: "Acme Inc",
		Topic:   "signup_request",
	})

	if template.title != "New signup completed" {
		t.Fatalf("unexpected title: %s", template.title)
	}
}

func TestNewContactRequestTemplate_DefaultTopic(t *testing.T) {
	template := newContactRequestTemplate(&model.ContactRequest{
		Name:    "Alice Example",
		Email:   "alice@example.com",
		Phone:   "+123456",
		Company: "Acme Inc",
		Topic:   "partnership",
		Message: "Hi there",
	})

	if template.title != "New site request received" {
		t.Fatalf("unexpected title: %s", template.title)
	}
	if !reflect.DeepEqual(template.emphasize, []string{"site request"}) {
		t.Fatalf("unexpected emphasize words: %#v", template.emphasize)
	}

	expectedFields := []messageField{
		{label: "Name", value: "Alice Example"},
		{label: "Email", value: "alice@example.com"},
		{label: "Phone", value: "+123456"},
		{label: "Company", value: "Acme Inc"},
		{label: "Topic", value: "partnership"},
		{label: "Message", value: "Hi there"},
	}
	if !reflect.DeepEqual(template.fields, expectedFields) {
		t.Fatalf("unexpected fields: %#v", template.fields)
	}
}

func TestNewContactRequestTemplate_AccountVerificationCompletedTopic(t *testing.T) {
	template := newContactRequestTemplate(&model.ContactRequest{
		Name:  "Alice Example",
		Email: "alice@example.com",
		Topic: model.ContactRequestTopicAccountVerificationCompleted,
	})

	if template.title != "Account verification completed" {
		t.Fatalf("unexpected title: %s", template.title)
	}
	if !reflect.DeepEqual(template.emphasize, []string{"verification completed"}) {
		t.Fatalf("unexpected emphasize words: %#v", template.emphasize)
	}

	expectedFields := []messageField{
		{label: "Name", value: "Alice Example"},
		{label: "Email", value: "alice@example.com"},
	}
	if !reflect.DeepEqual(template.fields, expectedFields) {
		t.Fatalf("unexpected fields: %#v", template.fields)
	}
}

func TestNewContactRequestTemplate_NilRequest(t *testing.T) {
	template := newContactRequestTemplate(nil)
	if template.title != "New site request received" {
		t.Fatalf("unexpected title: %s", template.title)
	}
}

func TestNewContactRequestTemplate_LegacySignupTopicCaseInsensitive(t *testing.T) {
	template := newContactRequestTemplate(&model.ContactRequest{
		Name:    "Alice Example",
		Email:   "alice@example.com",
		Company: "Acme Inc",
		Topic:   " SIGNUP_REQUEST ",
	})
	if template.title != "New signup completed" {
		t.Fatalf("unexpected title: %s", template.title)
	}
}
