import { render, screen } from "@solidjs/testing-library";
import { describe, expect, it } from "vitest";
import { VirtualList } from "../../../src/components/virtual-list/index";
import { VirtualListRootPropsSchema, VirtualListMeta } from "../../../src/components/virtual-list/virtual-list.props";

describe("VirtualList", () => {
  it("renders the inner spacer sized to totalSize via render prop", () => {
    render(() => (
      <VirtualList count={100} data-testid="vl" style={{ height: "200px" }}>
        {(item) => <div data-testid={`item-${item.index}`}>Item {item.index}</div>}
      </VirtualList>
    ));
    const inner = screen.getByTestId("vl").querySelector("[data-part='inner']") as HTMLElement;
    expect(inner).toBeTruthy();
    expect(inner.style.height).toBe("4000px");
  });

  it("renders with data-scope and data-part attributes", () => {
    render(() => (
      <VirtualList count={10} data-testid="vlist" style={{ height: "300px" }}>
        {(item) => <div>Row {item.index}</div>}
      </VirtualList>
    ));
    const el = screen.getByTestId("vlist");
    expect(el.getAttribute("data-scope")).toBe("virtual-list");
    expect(el.getAttribute("data-part")).toBe("root");
  });

  it("renders zero items when count is 0", () => {
    render(() => (
      <VirtualList count={0} data-testid="empty" style={{ height: "300px" }}>
        {(item) => <div data-testid={`row-${item.index}`}>Row {item.index}</div>}
      </VirtualList>
    ));
    expect(screen.queryByTestId("row-0")).toBeNull();
  });

  it("schema validates required count field", () => {
    expect(VirtualListRootPropsSchema.safeParse({ count: 50 }).success).toBe(true);
    expect(VirtualListRootPropsSchema.safeParse({ count: "bad" }).success).toBe(false);
  });

  it("schema accepts optional fields", () => {
    const result = VirtualListRootPropsSchema.safeParse({ count: 10, estimateSize: 50, overscan: 3, horizontal: true });
    expect(result.success).toBe(true);
  });

  it("meta has required fields", () => {
    expect(VirtualListMeta.name).toBe("VirtualList");
    expect(VirtualListMeta.parts).toContain("Root");
    expect(VirtualListMeta.requiredParts).toContain("Root");
    expect(typeof VirtualListMeta.description).toBe("string");
  });
});
