import { describe, expect, test } from "vitest";
import { CHART_TYPE_IDS } from "@/modules/ee/analysis/types/analysis";
import {
  findNextOpenSlot,
  findOverlappingWidget,
  getDefaultWidgetLayout,
  getWidgetMinSize,
  parseWidgetLayouts,
} from "./widget-placement";

describe("findNextOpenSlot", () => {
  test("places the first widget at the origin", () => {
    expect(findNextOpenSlot([], { w: 4, h: 4 })).toEqual({ x: 0, y: 0 });
  });

  test("fills the gap next to a single widget instead of stacking below it", () => {
    expect(findNextOpenSlot([{ x: 0, y: 0, w: 4, h: 4 }], { w: 4, h: 4 })).toEqual({ x: 4, y: 0 });
  });

  test("skips a partly filled row and finds the hole further along it", () => {
    const existing = [
      { x: 0, y: 0, w: 4, h: 4 },
      { x: 8, y: 0, w: 4, h: 4 },
    ];

    expect(findNextOpenSlot(existing, { w: 4, h: 4 })).toEqual({ x: 4, y: 0 });
  });

  test("starts a new row when the current one leaves no wide enough gap", () => {
    const existing = [
      { x: 0, y: 0, w: 6, h: 4 },
      { x: 6, y: 0, w: 4, h: 4 },
    ];

    // Only 2 columns are left on row 0, so a 4-wide widget has to go below.
    expect(findNextOpenSlot(existing, { w: 4, h: 4 })).toEqual({ x: 0, y: 4 });
  });

  test("uses a narrow leftover gap when the widget is small enough for it", () => {
    const existing = [
      { x: 0, y: 0, w: 6, h: 4 },
      { x: 6, y: 0, w: 4, h: 4 },
    ];

    expect(findNextOpenSlot(existing, { w: 2, h: 2 })).toEqual({ x: 10, y: 0 });
  });

  test("finds a gap left by a shorter widget above", () => {
    const existing = [
      { x: 0, y: 0, w: 12, h: 2 },
      { x: 0, y: 2, w: 8, h: 4 },
    ];

    expect(findNextOpenSlot(existing, { w: 4, h: 4 })).toEqual({ x: 8, y: 2 });
  });

  test("appends below everything when the grid is full", () => {
    const existing = [{ x: 0, y: 0, w: 12, h: 4 }];

    expect(findNextOpenSlot(existing, { w: 4, h: 4 })).toEqual({ x: 0, y: 4 });
  });

  test("clamps a widget wider than the grid instead of never fitting it", () => {
    expect(findNextOpenSlot([{ x: 0, y: 0, w: 12, h: 3 }], { w: 14, h: 3 })).toEqual({ x: 0, y: 3 });
  });

  test("honours a narrower breakpoint", () => {
    expect(findNextOpenSlot([{ x: 0, y: 0, w: 4, h: 4 }], { w: 4, h: 4 }, 6)).toEqual({ x: 0, y: 4 });
  });
});

describe("parseWidgetLayouts", () => {
  test("keeps well formed layouts", () => {
    const widgets = [{ layout: { x: 0, y: 0, w: 4, h: 4 } }, { layout: { x: 4, y: 0, w: 4, h: 4 } }];

    expect(parseWidgetLayouts(widgets)).toEqual([
      { x: 0, y: 0, w: 4, h: 4 },
      { x: 4, y: 0, w: 4, h: 4 },
    ]);
  });

  test("drops rows whose JSON is not a layout", () => {
    const widgets = [
      { layout: null },
      { layout: "nope" },
      { layout: { x: 0, y: 0 } },
      { layout: { x: 0, y: 0, w: 0, h: 4 } },
      { layout: { x: 0, y: 0, w: 4, h: 4 } },
    ];

    expect(parseWidgetLayouts(widgets)).toEqual([{ x: 0, y: 0, w: 4, h: 4 }]);
  });
});

describe("findOverlappingWidget", () => {
  const others = [
    { id: "left", layout: { x: 0, y: 0, w: 6, h: 4 } },
    { id: "unreadable", layout: { x: "6" } },
  ];

  test("names the widget a layout would cover", () => {
    expect(findOverlappingWidget({ x: 4, y: 2, w: 4, h: 4 }, others)?.id).toBe("left");
  });

  test("allows a widget that only touches another's edge, and gaps", () => {
    expect(findOverlappingWidget({ x: 6, y: 0, w: 6, h: 4 }, others)).toBeNull();
    expect(findOverlappingWidget({ x: 0, y: 4, w: 6, h: 4 }, others)).toBeNull();
    expect(findOverlappingWidget({ x: 0, y: 10, w: 3, h: 2 }, others)).toBeNull();
  });

  test("skips a stored layout it cannot read rather than refusing everything", () => {
    expect(findOverlappingWidget({ x: 6, y: 0, w: 6, h: 4 }, others)).toBeNull();
  });
});

describe("getDefaultWidgetLayout", () => {
  test("sizes a widget for its chart type", () => {
    expect(getDefaultWidgetLayout("big_number")).toEqual({ x: 0, y: 0, w: 3, h: 2 });
    expect(getDefaultWidgetLayout("matrix")).toEqual({ x: 0, y: 0, w: 6, h: 5 });
    expect(getDefaultWidgetLayout("bar")).toEqual({ x: 0, y: 0, w: 4, h: 4 });
  });
});

describe("getWidgetMinSize", () => {
  // The grid refuses to resize a widget below its minimum, so a default under it is a size the user
  // reaches once and can never get back to (ENG-2222).
  test.each(CHART_TYPE_IDS)("a new %s widget can be resized back to the size it was created at", (type) => {
    const created = getDefaultWidgetLayout(type);
    const { minW, minH } = getWidgetMinSize(type);

    expect(created.w).toBeGreaterThanOrEqual(minW);
    expect(created.h).toBeGreaterThanOrEqual(minH);
  });
});
