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

import {
  applyStorybookEnrichmentSeed,
  projectStoryEvidenceToEnrichmentSeed,
  type StorybookSeedableEnrichment,
} from "../index.js";
import type { StoryEvidence } from "../../ucf/types.js";

const evidence: StoryEvidence = {
  source: "storybook",
  title: "Primitives/Button",
  componentImport: { specifier: "./Button", name: "Button" },
  storyCount: 3,
  storyRefs: [
    "src/ui/Button.stories.tsx#Primary",
    "src/ui/Button.stories.tsx#Secondary",
    "src/ui/Button.stories.tsx#Disabled",
  ],
  storyNames: ["Primary", "Secondary", "Disabled"],
  storyFilePaths: ["src/ui/Button.stories.tsx"],
  tags: ["autodocs"],
  metaArgs: { size: "md" },
  autodocsDescription: "Primary action trigger.",
  stories: [
    {
      ref: "src/ui/Button.stories.tsx#Primary",
      name: "Primary",
      args: { variant: "primary" },
    },
    {
      ref: "src/ui/Button.stories.tsx#Secondary",
      name: "Secondary",
      args: { variant: "secondary" },
    },
    {
      ref: "src/ui/Button.stories.tsx#Disabled",
      name: "Disabled",
      args: { variant: "primary", disabled: true },
    },
  ],
};

describe("Storybook enrichment projection", () => {
  it("seeds story fields without overwriting human guidance and is idempotent", () => {
    const seed = projectStoryEvidenceToEnrichmentSeed({
      componentName: "Button",
      storyEvidence: [evidence],
    });
    const existing: StorybookSeedableEnrichment = {
      description: "",
      variants: [],
      dos: ["Keep the human guidance byte-identical."],
    };

    const first = applyStorybookEnrichmentSeed({ existing, seed });
    const second = applyStorybookEnrichmentSeed({
      existing: first.enrichment,
      seed,
    });

    expect(first.changed).toBe(true);
    expect(first.enrichment.description).toBe("Primary action trigger.");
    expect(first.enrichment.variants?.map((variant) => variant.name)).toEqual([
      "Primary",
      "Secondary",
      "Disabled",
    ]);
    expect(first.enrichment.usageExamples?.[0]).toEqual({
      code: '<Button variant="primary" size="md" />',
      provenance: "storybook",
    });
    expect(first.enrichment.provenance).toEqual({
      description: "storybook",
      variants: "storybook",
      usageExamples: "storybook",
    });
    expect(first.enrichment.dos).toEqual(existing.dos);
    expect(second.changed).toBe(false);
    expect(second.enrichment).toEqual(first.enrichment);
  });

  it("does not overwrite human-authored fields", () => {
    const seed = projectStoryEvidenceToEnrichmentSeed({
      componentName: "Button",
      storyEvidence: [evidence],
    });
    const existing: StorybookSeedableEnrichment = {
      description: "Human copy.",
      variants: [{ name: "Human Variant", provenance: "storybook" }],
      usageExamples: [{ code: "<Button />", provenance: "storybook" }],
      provenance: {
        description: "human",
        variants: "human",
        usageExamples: "human",
      },
      dos: ["Human do."],
    };

    const result = applyStorybookEnrichmentSeed({ existing, seed });

    expect(result.changed).toBe(false);
    expect(result.enrichment).toEqual(existing);
  });
});
