package usecase

import (
	"testing"

	"apps/backend/internal/module/radar/domain"
)

func TestPrefilterNormalizesDedupesAndKeepsWeakDemandForReview(t *testing.T) {
	items, stats := PrefilterCandidates([]*domain.CandidatePost{
		{Permalink: "https://Threads.net/@a/post/1?utm_source=x", Text: "泛紅換季怎麼辦"},
		{Permalink: "https://threads.net/@a/post/1", Text: "重複"},
		{Permalink: "https://threads.net/@b/post/2", Text: "今天天氣很好"},
		{Permalink: "https://threads.net/@c/post/3", Text: "限時優惠立即購買"},
	}, &domain.RadarWatch{Terms: []string{"泛紅"}}, nil)
	if len(items) != 3 || stats.Deduped != 1 || stats.Rejected != 1 || stats.Pass != 1 || stats.Review != 1 {
		t.Fatalf("unexpected prefilter result: items=%d stats=%+v", len(items), stats)
	}
	if items[0].ExternalID != "https://threads.net/@a/post/1" {
		t.Fatalf("url was not canonicalized: %q", items[0].ExternalID)
	}
}

func TestPrefilterDoesNotUseProductNameAsDemandSignal(t *testing.T) {
	items, stats := PrefilterCandidates([]*domain.CandidatePost{{Permalink: "https://threads.net/@a/post/1", Text: "舒緩精華好漂亮"}}, nil, &ProductContextSnapshot{ProductLabel: "舒緩精華", PainPoints: []string{"泛紅"}})
	if len(items) != 1 || stats.Review != 1 || stats.Rejected != 0 {
		t.Fatalf("product name should remain review, got items=%d stats=%+v", len(items), stats)
	}
}
