import { describe, expect, it } from "vitest";

import {
  buildKeywordHitRateFallbackSampleBlocks,
  collectKeywordHitRateFallbackSamples,
  deriveContactEnrichmentSkipReason,
  shouldRunKeywordHitRateLlmFallback,
} from "./search-hit-rate-fallback.js";

describe("collectKeywordHitRateFallbackSamples", () => {
  it("deduplicates samples and keeps semantic evidence", () => {
    const samples = collectKeywordHitRateFallbackSamples([], [
      {
        title: "球墨铸铁井盖",
        rawCardText: "厂家直供 球墨铸铁井盖 重型",
        highlightTexts: ["球墨铸铁井盖"],
      },
      {
        title: "球墨铸铁井盖",
        rawCardText: "厂家直供 球墨铸铁井盖 重型",
        highlightTexts: ["球墨铸铁井盖"],
      },
      {
        title: "排水沟盖板",
        rawCardText: "下水道沟盖板",
        highlightTexts: [],
      },
    ], 5);

    expect(samples).toHaveLength(2);
    expect(samples[0]?.highlightTexts).toEqual(["球墨铸铁井盖"]);
  });
});

describe("shouldRunKeywordHitRateLlmFallback", () => {
  it("allows LLM fallback only when local hit rate is zero and evidence is sufficient", () => {
    const decision = shouldRunKeywordHitRateLlmFallback({
      enabled: true,
      client: { ready: true },
      totalCount: 18,
      localHitCount: 0,
      localHitRate: 0,
      minSampleCount: 3,
      samples: [
        { title: "结果1", rawCardText: "球墨铸铁井盖 重型", highlightTexts: [] },
        { title: "结果2", rawCardText: "球墨铸铁井盖 市政", highlightTexts: [] },
        { title: "结果3", rawCardText: "球墨铸铁井盖 污水", highlightTexts: [] },
      ],
    });

    expect(decision.shouldRun).toBe(true);
    expect(decision.reason).toBe("eligible_for_llm_fallback");
  });

  it("skips LLM fallback when local matching already found hits", () => {
    const decision = shouldRunKeywordHitRateLlmFallback({
      enabled: true,
      client: { ready: true },
      totalCount: 18,
      localHitCount: 2,
      localHitRate: 0.11,
      minSampleCount: 3,
      samples: [
        { title: "结果1", rawCardText: "球墨铸铁井盖 重型", highlightTexts: [] },
        { title: "结果2", rawCardText: "球墨铸铁井盖 市政", highlightTexts: [] },
        { title: "结果3", rawCardText: "球墨铸铁井盖 污水", highlightTexts: [] },
      ],
    });

    expect(decision.shouldRun).toBe(false);
    expect(decision.reason).toBe("local_hits_present");
  });
});

describe("buildKeywordHitRateFallbackSampleBlocks", () => {
  it("builds numbered sample blocks", () => {
    const blocks = buildKeywordHitRateFallbackSampleBlocks([
      {
        title: "球墨铸铁井盖",
        rawCardText: "厂家直供 球墨铸铁井盖 重型",
        highlightTexts: ["球墨铸铁井盖"],
      },
    ]);

    expect(blocks).toContain("样本1");
    expect(blocks).toContain("标题: 球墨铸铁井盖");
    expect(blocks).toContain("高亮词: 球墨铸铁井盖");
  });
});

describe("deriveContactEnrichmentSkipReason", () => {
  it("prefers explicit brand stop over other states", () => {
    expect(deriveContactEnrichmentSkipReason({
      brandZeroHitStop: true,
      resultCount: 10,
      isValidSearch: true,
      shouldForceContactEnrichment: true,
    })).toBe("brand_zero_hit_stop");
  });

  it("marks invalid search pages as skipped when not forced", () => {
    expect(deriveContactEnrichmentSkipReason({
      brandZeroHitStop: false,
      resultCount: 5,
      isValidSearch: false,
      shouldForceContactEnrichment: false,
    })).toBe("visual_invalid_skip");
  });

  it("returns empty reason when contact enrichment should run", () => {
    expect(deriveContactEnrichmentSkipReason({
      brandZeroHitStop: false,
      resultCount: 5,
      isValidSearch: true,
      shouldForceContactEnrichment: false,
    })).toBe("");
  });
});