package telegram

import (
	"strings"

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

const legacyContactRequestTopicSignup = "signup_request"

func newContactRequestTemplate(request *model.ContactRequest) messageTemplate {
	if request == nil {
		request = &model.ContactRequest{}
	}

	switch normalizeContactRequestTopic(request.Topic) {
	case model.ContactRequestTopicAccountVerificationCompleted:
		return newAccountVerificationCompletedTemplate(request)
	case model.ContactRequestTopicSignupCompleted:
		return newSignupCompletedTemplate(request)
	default:
		return newSiteContactTemplate(request)
	}
}

func normalizeContactRequestTopic(topic string) string {
	normalized := strings.ToLower(strings.TrimSpace(topic))
	if normalized == legacyContactRequestTopicSignup {
		return model.ContactRequestTopicSignupCompleted
	}
	return normalized
}

func newAccountVerificationCompletedTemplate(request *model.ContactRequest) messageTemplate {
	return messageTemplate{
		title:     "Account verification completed",
		emphasize: []string{"verification completed"},
		fields: []messageField{
			{label: "Name", value: request.Name},
			{label: "Email", value: request.Email},
		},
	}
}

func newSignupCompletedTemplate(request *model.ContactRequest) messageTemplate {
	return messageTemplate{
		title:     "New signup completed",
		emphasize: []string{"signup completed"},
		fields: []messageField{
			{label: "Organization", value: request.Company},
			{label: "Name", value: request.Name},
			{label: "Email", value: request.Email},
		},
	}
}

func newSiteContactTemplate(request *model.ContactRequest) messageTemplate {
	return messageTemplate{
		title:     "New site request received",
		emphasize: []string{"site request"},
		fields: []messageField{
			{label: "Name", value: request.Name},
			{label: "Email", value: request.Email},
			{label: "Phone", value: request.Phone},
			{label: "Company", value: request.Company},
			{label: "Topic", value: request.Topic},
			{label: "Message", value: request.Message},
		},
	}
}
