package gtm

import (
	"context"
	"fmt"
	"strings"

	"github.com/go-logr/logr"
	"github.com/tidbcloud/devops/entity/userbehavior"
	"github.com/tidbcloud/devops/pkg/infra/clients/llm_cli"
	"github.com/tidbcloud/devops/pkg/infra/utils/pointer"
)

type EmailComposer struct {
	logger logr.Logger
}

func NewEmailComposer(logger logr.Logger, _ *llm_cli.LLMCli) *EmailComposer {
	return &EmailComposer{
		logger: logger,
	}
}

func (c *EmailComposer) ComposeEmail(
	ctx context.Context,
	lead *userbehavior.GTMLead,
	match MatchResult,
) (*EmailContent, error) {
	// Generate subject
	subject := fmt.Sprintf("%s + TiDB Cloud",
		pointer.Deref(lead.CompanyName, "Company"))

	emailBody := c.renderEmailBody(lead, match)

	// Wrap in HTML template
	htmlBody := c.wrapInHTMLTemplate(emailBody)

	return &EmailContent{
		Subject:  subject,
		HTMLBody: htmlBody,
	}, nil
}

func (c *EmailComposer) renderEmailBody(lead *userbehavior.GTMLead, match MatchResult) string {
	contactName := pointer.Deref(lead.ContactName, "there")
	salutationName := pointer.Deref(lead.ContactFirstName, contactName)
	companyName := pointer.Deref(lead.CompanyName, "your company")

	return fmt.Sprintf(`Hi %s,

I saw that you recently created clusters in TiDB Cloud - wanted to reach out and learn what you're building at %s.

%s

If you're in the middle of building something, I'd be happy to share what we're seeing work well and where teams typically run into issues.

Would it be worth a quick 15-minute conversation this week?

Manus AI - Agents at Scale: https://www.youtube.com/watch?v=PP4Ht_JWA7k
Dify Consolidation: https://www.pingcap.com/case-study/dify-consolidates-massive-database-containers-into-one-unified-system-with-tidb/
Medium.com - TiDB: https://medium.com/@siddontang/when-every-ai-agent-needs-its-own-database-why-tidb-cloud-is-the-answer-d3ff37580284
TiDB Community Slack: https://slack.tidb.io/invite?team=tidb-community&channel=everyone&ref=pingcap-tidb
TiDB Community Discord: https://discord.com/invite/KVRZBR2DrG

Best,
Chris
TiDB Cloud Team`,
		salutationName,
		companyName,
		c.profileSnippet(match.UseCaseProfile),
	)
}

func (c *EmailComposer) profileSnippet(useCaseProfile string) string {
	switch useCaseProfile {
	case "ai-llm-platform":
		return "We're currently working with teams building AI/LLM-driven products and helping them scale early (credits, architecture guidance, and POC support)."
	default:
		return "We're currently working with product teams building on TiDB Cloud and helping them scale early (credits, architecture guidance, and POC support)."
	}
}

func (c *EmailComposer) wrapInHTMLTemplate(body string) string {
	// Simple HTML wrapper
	// Replace newlines with <br> tags
	htmlBody := strings.ReplaceAll(body, "\n", "<br>\n")

	// Wrap in basic HTML structure
	return fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
        a { color: #0066cc; text-decoration: none; }
    </style>
</head>
<body>
    %s
</body>
</html>`, htmlBody)
}
