package outreach

import (
	"fmt"
	"strings"

	outreachusecase "haixun-backend/internal/model/outreach_draft/domain/usecase"
)

func FormatPreviousDraftsHint(latest *outreachusecase.DraftSummary) string {
	if latest == nil || len(latest.Drafts) == 0 {
		return ""
	}
	lines := []string{"上一版草稿（請避開相同寫法）："}
	for i, item := range latest.Drafts {
		angle := strings.TrimSpace(item.Angle)
		if angle == "" {
			angle = fmt.Sprintf("版本%d", i+1)
		}
		lines = append(lines, fmt.Sprintf("- %s：%s", angle, truncateRunes(item.Text, 140)))
	}
	return strings.Join(lines, "\n")
}

func truncateRunes(text string, max int) string {
	text = strings.TrimSpace(text)
	runes := []rune(text)
	if len(runes) <= max {
		return text
	}
	return string(runes[:max]) + "…"
}
