package placement

import (
	"testing"

	libkg "haixun-backend/internal/library/knowledge"
	"haixun-backend/internal/library/websearch"
)

func TestCollectPatrolTagQueriesManualOnly(t *testing.T) {
	queries := CollectPatrolTagQueries([]string{"化療 沐浴乳"}, nil, websearch.ProviderBrave)
	if len(queries) < 2 {
		t.Fatalf("expected relevance + recency queries, got %d", len(queries))
	}
	if queries[0].Tag != "化療 沐浴乳" || queries[0].Dimension != QueryRelevance {
		t.Fatalf("unexpected first query: %+v", queries[0])
	}
	if queries[0].ProductFitScore != defaultPatrolProductFit {
		t.Fatalf("expected default fit %d, got %d", defaultPatrolProductFit, queries[0].ProductFitScore)
	}
}

func TestCollectPatrolTagQueriesUsesGraphFit(t *testing.T) {
	nodes := []libkg.Node{
		{
			ID:              "n1",
			Label:           "化療 沐浴乳",
			ProductFitScore: 92,
			DerivedTags: libkg.DerivedTags{
				Relevance: []string{"化療 沐浴乳"},
			},
		},
	}
	queries := CollectPatrolTagQueries([]string{"化療 沐浴乳"}, nodes, websearch.ProviderBrave)
	if len(queries) == 0 || queries[0].ProductFitScore != 92 {
		t.Fatalf("expected graph fit 92, got %+v", queries)
	}
}

func TestResolveTagQueriesPrefersPatrolKeywords(t *testing.T) {
	nodes := []libkg.Node{
		{ID: "n1", Label: "ignored", SelectedForScan: true, DerivedTags: libkg.DerivedTags{Relevance: []string{"ignored"}}},
	}
	queries := ResolveTagQueries(nodes, []string{"手動 關鍵字"}, websearch.ProviderBrave)
	if len(queries) == 0 || queries[0].Tag != "手動 關鍵字" {
		t.Fatalf("expected patrol keyword query, got %+v", queries)
	}
}
