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

import type { UniversalComponentFact } from "../../ucf/types.js";
import { mergeStoryFactsIntoUcfs, parseStoryFacts } from "../index.js";

function ucf(overrides: Partial<UniversalComponentFact>): UniversalComponentFact {
  return {
    id: "ucf:button",
    filePath: "src/ui/Button.tsx",
    componentName: "Button",
    framework: "react",
    sourceCommit: "abc123",
    capturedAt: "2026-06-10T00:00:00.000Z",
    adapterVersion: "test",
    definitionKind: "function",
    wrappedBy: [],
    imports: [],
    exports: [
      {
        exportedAs: "Button",
        localName: "Button",
        isPrimary: true,
      },
    ],
    exportedFromBarrel: false,
    rootElements: [],
    rootElementsTruncated: false,
    ariaRoles: [],
    ariaAttributes: {},
    props: [],
    events: [],
    slots: [],
    hasInternalState: false,
    hasEffects: false,
    isControlled: "unknown",
    compoundChildren: [],
    styleSystem: "unknown",
    classNamesUsed: [],
    ...overrides,
  };
}

const BUTTON_STORY = `
  import type { Meta, StoryObj } from "@storybook/react";
  import { Button } from "./Button";

  const meta: Meta<typeof Button> = {
    title: "Primitives/Button",
    component: Button,
    tags: ["autodocs"],
    args: { size: "md" },
  };
  export default meta;
  type Story = StoryObj<typeof Button>;

  export const Primary: Story = { args: { variant: "primary" } };
  export const Secondary: Story = { args: { variant: "secondary" } };
  export const Disabled: Story = {
    args: { variant: "primary", disabled: true },
  };
`;

describe("mergeStoryFactsIntoUcfs", () => {
  it("attaches Storybook evidence to the existing UCF identity", () => {
    const sourceUcf = ucf({});
    const facts = parseStoryFacts(BUTTON_STORY, "src/ui/Button.stories.tsx");
    const result = mergeStoryFactsIntoUcfs({
      ucfs: [sourceUcf],
      stories: [facts],
    });

    expect(result.unresolved).toEqual([]);
    expect(result.ucfs).toHaveLength(1);
    expect(result.ucfs[0].id).toBe(sourceUcf.id);
    expect(result.ucfs[0].storyEvidence?.[0]).toMatchObject({
      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",
      ],
    });
  });

  it("returns unresolved evidence for third-party imports without adding UCFs", () => {
    const sourceUcf = ucf({ id: "ucf:other", componentName: "Other" });
    const facts = parseStoryFacts(
      `
        import { DatePicker } from "some-external-lib";
        const meta = {
          title: "Vendored/DatePicker",
          component: DatePicker,
        };
        export default meta;
        export const Default = {};
      `,
      "src/DatePicker.stories.tsx"
    );
    const result = mergeStoryFactsIntoUcfs({
      ucfs: [sourceUcf],
      stories: [facts],
    });

    expect(result.ucfs).toHaveLength(1);
    expect(result.ucfs[0].storyEvidence).toBeUndefined();
    expect(result.unresolved).toEqual([
      {
        file: "src/DatePicker.stories.tsx",
        importSpecifier: "some-external-lib",
        importName: "DatePicker",
        reason: "external-import",
      },
    ]);
  });
});
