import { describe, expect, it } from "vitest";
import {
  communityRuleEntryFromMethodChip,
  sectionFromDecision,
  withoutUnpublishedDecisionConsensus,
} from "../../lib/create/ruleSectionsFromMethodSelections";

const emptyDecisionSections = {
  corePrinciple: "",
  applicableScope: [] as string[],
  selectedApplicableScope: [] as string[],
  stepByStepInstructions: "",
  consensusLevel: 75,
  objectionsDeadlocks: "",
};

describe("withoutUnpublishedDecisionConsensus", () => {
  it("keeps catalog consensus when facet copy is present", () => {
    const next = withoutUnpublishedDecisionConsensus({
      corePrinciple: "Momentum.",
      consensusLevel: 100,
    });
    expect(next.consensusLevel).toBe(100);
  });

  it("drops seeded consensus on empty custom facet copy", () => {
    const next = withoutUnpublishedDecisionConsensus({ ...emptyDecisionSections });
    expect(next.consensusLevel).toBeUndefined();
  });

  it("drops keyed consensus when wizard field blocks exist", () => {
    const next = withoutUnpublishedDecisionConsensus(
      { corePrinciple: "Momentum.", consensusLevel: 75 },
      [
        {
          kind: "proportion",
          id: "p1",
          blockTitle: "Quorum",
          defaultPercent: 60,
        },
      ],
    );
    expect(next.consensusLevel).toBeUndefined();
  });
});

describe("communityRuleEntryFromMethodChip", () => {
  it("publishes supportText as body when there are no labeled blocks", () => {
    expect(
      communityRuleEntryFromMethodChip(
        "Our process",
        { corePrinciple: "" },
        { corePrinciple: "Core Principle" },
        { supportText: "  Members propose in the forum.  " },
      ),
    ).toEqual({
      title: "Our process",
      body: "Members propose in the forum.",
    });
  });

  it("keeps supportText alongside wizard blocks", () => {
    const entry = communityRuleEntryFromMethodChip(
      "Our process",
      { corePrinciple: "" },
      { corePrinciple: "Core Principle" },
      {
        supportText: "How we decide.",
        customFieldBlocks: [
          {
            kind: "text",
            id: "b1",
            blockTitle: "Notes",
            placeholderText: "Post in #governance.",
          },
        ],
      },
    );
    expect(entry).toEqual({
      title: "Our process",
      body: "How we decide.",
      blocks: [{ label: "Notes", body: "Post in #governance." }],
    });
  });
});

describe("sectionFromDecision", () => {
  it("omits seeded 75% and shows the policy description on a custom card", () => {
    const section = sectionFromDecision([
      {
        id: "00000000-0000-4000-8000-000000000001",
        label: "Forum proposals",
        supportText: "Anyone can start a thread; silence after a week is consent.",
        sections: { ...emptyDecisionSections },
      },
    ]);
    expect(section?.categoryName).toBe("Decision-making");
    expect(section?.entries).toEqual([
      {
        title: "Forum proposals",
        body: "Anyone can start a thread; silence after a week is consent.",
      },
    ]);
  });

  it("still emits catalog consensus when facet copy is present", () => {
    const section = sectionFromDecision([
      {
        id: "lazy-consensus",
        label: "Lazy Consensus",
        sections: {
          corePrinciple: "Silence is consent.",
          applicableScope: [],
          selectedApplicableScope: [],
          stepByStepInstructions: "Post a deadline.",
          consensusLevel: 100,
          objectionsDeadlocks: "Any member can block.",
        },
      },
    ]);
    const blocks = section?.entries[0]?.blocks ?? [];
    expect(blocks).toContainEqual({
      label: "Consensus Level",
      body: "100%",
    });
  });

  it("does not duplicate consensus when a wizard proportion block exists", () => {
    const section = sectionFromDecision(
      [
        {
          id: "00000000-0000-4000-8000-000000000002",
          label: "Custom vote",
          supportText: "We vote in person.",
          sections: {
            ...emptyDecisionSections,
            consensusLevel: 75,
          },
        },
      ],
      {
        "00000000-0000-4000-8000-000000000002": [
          {
            kind: "proportion",
            id: "q",
            blockTitle: "Quorum",
            defaultPercent: 60,
          },
        ],
      },
    );
    const blocks = section?.entries[0]?.blocks ?? [];
    expect(blocks).toEqual([{ label: "Quorum", body: "60%" }]);
    expect(section?.entries[0]?.body).toBe("We vote in person.");
  });
});
