import { describe, it, expect } from "vitest";
import {
  ER_SIZES,
  getErEntityHeight,
  resolveErNodeType,
  resolveErEdgeType,
} from "./constants";
import type { Column } from "../graph";

describe("resolveErNodeType", () => {
  it("should resolve er:entity to erEntity", () => {
    expect(resolveErNodeType("er:entity")).toBe("erEntity");
  });

  it("should resolve bare entity to erEntity", () => {
    expect(resolveErNodeType("entity")).toBe("erEntity");
  });

  it("should default unknown types to erEntity", () => {
    expect(resolveErNodeType("er:unknown")).toBe("erEntity");
    expect(resolveErNodeType("something")).toBe("erEntity");
  });
});

describe("resolveErEdgeType", () => {
  it("should always resolve to erRelationship", () => {
    expect(resolveErEdgeType("relationship")).toBe("erRelationship");
    expect(resolveErEdgeType(undefined)).toBe("erRelationship");
    expect(resolveErEdgeType("inheritance")).toBe("erRelationship");
  });
});

describe("getErEntityHeight", () => {
  it("should compute height with no columns (uses minRows=1)", () => {
    const expected =
      ER_SIZES.entity.headerH +
      ER_SIZES.entity.minRows * ER_SIZES.entity.rowH +
      ER_SIZES.entity.paddingY * 2;
    expect(getErEntityHeight()).toBe(expected);
    expect(getErEntityHeight(undefined)).toBe(expected);
  });

  it("should compute height with empty columns array (uses minRows=1)", () => {
    const expected =
      ER_SIZES.entity.headerH +
      ER_SIZES.entity.minRows * ER_SIZES.entity.rowH +
      ER_SIZES.entity.paddingY * 2;
    expect(getErEntityHeight([])).toBe(expected);
  });

  it("should compute height based on column count", () => {
    const columns: Column[] = [
      { name: "id", type: "uuid", isPrimaryKey: true },
      { name: "name", type: "text" },
      { name: "email", type: "varchar" },
    ];
    const expected =
      ER_SIZES.entity.headerH +
      3 * ER_SIZES.entity.rowH +
      ER_SIZES.entity.paddingY * 2;
    expect(getErEntityHeight(columns)).toBe(expected);
  });

  it("should compute correct height for many columns", () => {
    const columns: Column[] = Array.from({ length: 10 }, (_, i) => ({
      name: `col_${i}`,
      type: "text",
    }));
    const expected =
      ER_SIZES.entity.headerH +
      10 * ER_SIZES.entity.rowH +
      ER_SIZES.entity.paddingY * 2;
    expect(getErEntityHeight(columns)).toBe(expected);
  });
});

describe("ER_SIZES", () => {
  it("should have entity dimensions defined", () => {
    expect(ER_SIZES.entity.w).toBe(260);
    expect(ER_SIZES.entity.headerH).toBe(36);
    expect(ER_SIZES.entity.rowH).toBe(24);
    expect(ER_SIZES.entity.paddingY).toBe(8);
    expect(ER_SIZES.entity.minRows).toBe(1);
  });
});
