package viral

import "testing"

func TestFormatAnalysisForReplicateUsesNarrativeFields(t *testing.T) {
	got := FormatAnalysisForReplicate(ViralAnalysis{
		StoryHook:        "先丟一個具體窘況",
		NarrativeBeat:    "從失敗經驗拐到頓悟",
		EmotionalCore:    "鬆了一口氣",
		SceneSeed:        "週日早晨的廚房",
		AuthenticityCues: []string{"坦白搞砸", "口語碎碎念"},
		WhatToAvoid:      "不要照抄三段式",
	})
	for _, part := range []string{"故事開場力量", "敘事推進", "核心情緒", "場景種子", "真人質感線索", "避免變成複製文", "週日早晨的廚房"} {
		if got == "" || !contains(got, part) {
			t.Fatalf("FormatAnalysisForReplicate() missing %q: %q", part, got)
		}
	}
}

func TestFormatAnalysisForReplicateFallsBackToLegacyFields(t *testing.T) {
	got := FormatAnalysisForReplicate(ViralAnalysis{
		HookPattern:      "痛點開場",
		StructurePattern: "三段式",
		KeyTakeaways:     []string{"具體細節"},
	})
	if !contains(got, "故事開場力量") || !contains(got, "痛點開場") {
		t.Fatalf("legacy fallback missing: %q", got)
	}
}

func contains(s, part string) bool {
	return len(s) >= len(part) && (s == part || len(part) == 0 || indexOf(s, part) >= 0)
}

func indexOf(s, part string) int {
	for i := 0; i+len(part) <= len(s); i++ {
		if s[i:i+len(part)] == part {
			return i
		}
	}
	return -1
}
