package matrix

import (
	"fmt"
	"strings"

	libprompt "haixun-backend/internal/library/prompt"
)

type CopyGenerateInput struct {
	Count        int
	TopicLabel   string
	TopicBrief   string
	ResearchMap  string
	SelectedTags []string
	ViralSamples string
	PersonaBlock string
}

func BuildCopyUserPrompt(in CopyGenerateInput) (string, error) {
	count := in.Count
	if count <= 0 {
		count = 5
	}
	if count > 12 {
		count = 12
	}
	return libprompt.MatrixCopyUser(map[string]string{
		"count":               fmt.Sprintf("%d", count),
		"topic_label":         strings.TrimSpace(in.TopicLabel),
		"topic_brief":         strings.TrimSpace(in.TopicBrief),
		"research_map_block":  strings.TrimSpace(in.ResearchMap),
		"selected_tags_block": formatTagList(in.SelectedTags),
		"viral_samples_block": strings.TrimSpace(in.ViralSamples),
		"persona_block":       strings.TrimSpace(in.PersonaBlock),
	})
}

func formatTagList(tags []string) string {
	if len(tags) == 0 {
		return "（尚未選擇）"
	}
	lines := make([]string, 0, len(tags))
	for _, tag := range tags {
		tag = strings.TrimSpace(tag)
		if tag == "" {
			continue
		}
		lines = append(lines, "- "+tag)
	}
	if len(lines) == 0 {
		return "（尚未選擇）"
	}
	return strings.Join(lines, "\n")
}

func FormatCopyResearchMapBlock(audience, goal string, questions, pillars, exclusions []string) string {
	var b strings.Builder
	if audience = strings.TrimSpace(audience); audience != "" {
		b.WriteString("受眾：")
		b.WriteString(audience)
		b.WriteString("\n")
	}
	if goal = strings.TrimSpace(goal); goal != "" {
		b.WriteString("內容目標：")
		b.WriteString(goal)
		b.WriteString("\n")
	}
	if len(pillars) > 0 {
		b.WriteString("支柱：")
		b.WriteString(strings.Join(pillars, "、"))
		b.WriteString("\n")
	}
	if len(questions) > 0 {
		b.WriteString("受眾問題：")
		b.WriteString(strings.Join(questions, "；"))
		b.WriteString("\n")
	}
	if len(exclusions) > 0 {
		b.WriteString("排除：")
		b.WriteString(strings.Join(exclusions, "；"))
	}
	return strings.TrimSpace(b.String())
}
