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

import {
  buildIngestRequest,
  ingestTable,
  isSingleObjectIngest,
} from "./ingest";

describe("ingestTable", () => {
  it("maps ingestable kinds to their target table", () => {
    expect(ingestTable("GoldenFingerResult")).toBe("world_entities");
    expect(ingestTable("GlossaryResult")).toBe("world_entities");
    expect(ingestTable("DetailedOutlineResult")).toBe("outline");
    expect(ingestTable("BookTeardownResult")).toBe("rules");
  });

  it("returns null for non-ingestable kinds", () => {
    expect(ingestTable("IdeaListResult")).toBeNull();
    expect(ingestTable("unknown")).toBeNull();
  });
});

describe("isSingleObjectIngest", () => {
  it("flags teardown as single-object ingest", () => {
    expect(isSingleObjectIngest("BookTeardownResult")).toBe(true);
  });

  it("treats row-based kinds as not single-object", () => {
    expect(isSingleObjectIngest("GoldenFingerResult")).toBe(false);
    expect(isSingleObjectIngest("DetailedOutlineResult")).toBe(false);
    expect(isSingleObjectIngest("unknown")).toBe(false);
  });
});

describe("buildIngestRequest", () => {
  it("maps golden finger rows into world_entities with merged rules", () => {
    const body = buildIngestRequest({
      outputKind: "GoldenFingerResult",
      rows: [
        { name: "吞噬系统", mechanism: "吞噬获取", growth: "等级提升", limits: "需进食" },
      ],
      acknowledgeConflicts: true,
    });
    expect(body).toEqual({
      world_entities: [
        {
          type: "力量体系",
          name: "吞噬系统",
          rules: ["吞噬获取", "等级提升", "需进食"],
        },
      ],
      acknowledge_conflicts: true,
    });
  });

  it("drops blank rule parts for golden finger", () => {
    const body = buildIngestRequest({
      outputKind: "GoldenFingerResult",
      rows: [{ name: "x", mechanism: "m", growth: "", limits: "" }],
    });
    expect(body?.world_entities?.[0]?.rules).toEqual(["m"]);
  });

  it("maps glossary rows preserving type and rules array", () => {
    const body = buildIngestRequest({
      outputKind: "GlossaryResult",
      rows: [{ name: "灵气", type: "概念", rules: ["不可逆"] }],
    });
    expect(body?.world_entities?.[0]).toEqual({
      type: "概念",
      name: "灵气",
      rules: ["不可逆"],
    });
  });

  it("defaults glossary type to 概念 when missing", () => {
    const body = buildIngestRequest({
      outputKind: "GlossaryResult",
      rows: [{ name: "灵气" }],
    });
    expect(body?.world_entities?.[0]?.type).toBe("概念");
  });

  it("maps detailed outline rows to scenes with chapter_no", () => {
    const body = buildIngestRequest({
      outputKind: "DetailedOutlineResult",
      rows: [
        { idx: 2, beat: "对峙", purpose: "立威", conflict: "正反", hook: "悬念" },
        { beat: "收束" },
      ],
      chapterNo: 7,
    });
    expect(body?.chapter_no).toBe(7);
    expect(body?.scenes).toEqual([
      { idx: 2, beat: "对峙", purpose: "立威", conflict: "正反", hook: "悬念" },
      { idx: 1, beat: "收束", purpose: "", conflict: "", hook: "" },
    ]);
    expect(body?.acknowledge_conflicts).toBe(false);
  });

  it("maps teardown single object to a teardown body", () => {
    const body = buildIngestRequest({
      outputKind: "BookTeardownResult",
      rows: [
        {
          themes: ["逆袭", "成长"],
          archetypes: ["废柴主角"],
          structure: "三幕式",
          hooks: ["开局打脸"],
        },
      ],
      acknowledgeConflicts: true,
    });
    expect(body).toEqual({
      teardown: {
        themes: ["逆袭", "成长"],
        archetypes: ["废柴主角"],
        structure: "三幕式",
        hooks: ["开局打脸"],
      },
      acknowledge_conflicts: true,
    });
  });

  it("returns null for teardown with no rows", () => {
    expect(
      buildIngestRequest({ outputKind: "BookTeardownResult", rows: [] }),
    ).toBeNull();
  });

  it("returns null for non-ingestable output kind", () => {
    expect(
      buildIngestRequest({ outputKind: "IdeaListResult", rows: [] }),
    ).toBeNull();
  });
});
