package viral

import (
	"testing"

	"haixun-backend/internal/library/placement"
)

func TestBuildReferenceAccountsFromScan_filtersLowEngagement(t *testing.T) {
	got := BuildReferenceAccountsFromScan(ReferenceAccountInput{
		SeedQuery: "轉職",
		Label:     "轉職語錄",
		Posts: []placement.ScanCandidate{
			{Author: "weak_user", Text: "轉職心得", SearchTag: "轉職", LikeCount: 2, ReplyCount: 0, EngagementScore: 10},
			{Author: "hot_user", Text: "轉職面試技巧分享", SearchTag: "轉職", LikeCount: 30, ReplyCount: 5, EngagementScore: 85},
		},
		Limit: 5,
	})
	if len(got) != 1 {
		t.Fatalf("expected 1 account, got %d", len(got))
	}
	if got[0].Username != "hot_user" {
		t.Fatalf("unexpected username %q", got[0].Username)
	}
}

func TestBuildReferenceAccountsFromScan_prefersVerified(t *testing.T) {
	got := BuildReferenceAccountsFromScan(ReferenceAccountInput{
		SeedQuery: "轉職",
		Label:     "轉職",
		Posts: []placement.ScanCandidate{
			{Author: "plain_user", Text: "轉職技巧", SearchTag: "轉職", LikeCount: 22, ReplyCount: 3, EngagementScore: 55},
			{Author: "blue_user", Text: "轉職分享", SearchTag: "轉職", LikeCount: 14, ReplyCount: 2, EngagementScore: 42, AuthorVerified: true},
		},
		Limit: 5,
	})
	if len(got) < 2 {
		t.Fatalf("expected 2 accounts, got %d", len(got))
	}
	if got[0].Username != "blue_user" || !got[0].AuthorVerified {
		t.Fatalf("verified author should rank first, got %+v", got[0])
	}
}

func TestBuildReferenceAccountsFromScan_requiresTopicMatch(t *testing.T) {
	posts := []placement.ScanCandidate{
		{Author: "off_topic", Text: "今天天氣真好", SearchTag: "天氣", LikeCount: 40, ReplyCount: 8, EngagementScore: 90},
	}
	got := BuildReferenceAccountsFromScan(ReferenceAccountInput{
		SeedQuery: "轉職",
		Label:     "轉職",
		Posts:     posts,
		Limit:     5,
	})
	if len(got) != 0 {
		t.Fatalf("expected no accounts for off-topic post, got %d", len(got))
	}
}
