package usecase

import (
	"strings"
	"testing"

	"apps/backend/internal/module/studio/domain"

	"github.com/stretchr/testify/require"
)

func TestPersonaReplyFingerprintUsesExpressionWithoutContentTemplate(t *testing.T) {
	p := &domain.Persona{
		Brief: "產品設計師，說話直接",
		Style: domain.PersonaStyle{
			Draft: domain.PersonaDraft{
				Tone:                "輕鬆但具體",
				Hooks:               "真心建議開頭",
				LanguageFingerprint: "短句、偶爾吐槽",
				Rhythm:              "長短句交錯",
				Punctuation:         "少用驚嘆號",
				ContentPatterns:     "痛點接三點建議",
				CtaStyle:            "最後一定問大家",
				Avoid:               "官腔",
			},
			SamplePreviews: []string{"這功能我昨天也卡了一下，後來發現入口藏得有點深。"},
		},
		Guard: domain.PersonaGuard{Avoid: []string{"像客服"}},
	}

	fp := personaExpressionFingerprintBlock(p)
	require.Contains(t, fp, "輕鬆但具體")
	require.Contains(t, fp, "短句、偶爾吐槽")
	require.Contains(t, fp, "長短句交錯")
	require.Contains(t, fp, "語感樣本")
	require.NotContains(t, fp, "真心建議開頭")
	require.NotContains(t, fp, "痛點接三點建議")
	require.NotContains(t, fp, "最後一定問大家")
}

func TestReplyPromptsPrioritizeSourceContent(t *testing.T) {
	ownPost := buildOwnPostReplyPrompt("語氣：自然", "最近把通知分類重做了", "ann", "靜音可以只套用一個品牌嗎？")
	require.Contains(t, ownPost, "回應原文或留言裡至少一個具體資訊")
	require.Contains(t, ownPost, "靜音可以只套用一個品牌嗎？")
	require.Contains(t, ownPost, "不預設任何開頭")
	require.NotContains(t, ownPost, "開場邀互動")

	mention := buildMentionReplyPrompt("語氣：自然", "ann", "你這個通知分類很好用", "昨天剛上線")
	require.Contains(t, mention, "直接接住對方至少一個具體資訊")
	require.Contains(t, mention, "你這個通知分類很好用")
	require.Contains(t, mention, "不預設任何開頭")
	require.False(t, strings.Contains(mention, "嚴格遵守指紋"))
}

func TestMimicPromptPlansContentBeforeApplyingFingerprint(t *testing.T) {
	prompt := buildMimicPrompt(
		"語氣：直接但溫柔",
		"我原本以為改版只是換顏色，真的用過才發現操作路徑少了一半。",
		"談談功能刪減如何降低學習成本",
		"",
	)

	require.Contains(t, prompt, "只抽取可借用的敘事骨架、資訊密度、轉折方式與情緒曲線")
	require.Contains(t, prompt, "談談功能刪減如何降低學習成本")
	require.Contains(t, prompt, "參考文只供學習結構，不是內容來源")
	require.Contains(t, prompt, "不得沿用其人物、事件、品牌、例子、數字、觀點句、首句或結論")
	require.Contains(t, prompt, "我原本以為改版只是換顏色")
	require.Contains(t, prompt, "把文章完整講完")
	require.NotContains(t, prompt, "約 30～100 字")
	require.NotContains(t, prompt, "有問句就留好回的小問題")
}

func TestMainPostCleanerDoesNotTruncate(t *testing.T) {
	long := strings.Repeat("這段內容需要完整保留。", 120)
	require.Equal(t, long, cleanGeneratedPostText(long))
	require.Less(t, len([]rune(cleanGeneratedTextMax(long, 120))), len([]rune(long)))
}
