/**
 * Widget Registry Tests
 * Following TDD - write tests first!
 */

/* eslint-disable @typescript-eslint/no-non-null-assertion */

import { describe, it, expect } from "vitest";
import { widgetRegistry } from "../WidgetRegistry";
import { TasksWidget } from "../TasksWidget";
import { CalendarWidget } from "../CalendarWidget";
import { QuickCaptureWidget } from "../QuickCaptureWidget";
import { OrchestratorEventsWidget } from "../OrchestratorEventsWidget";

describe("WidgetRegistry", (): void => {
  it("should have a registry of widgets", (): void => {
    expect(widgetRegistry).toBeDefined();
    expect(typeof widgetRegistry).toBe("object");
  });

  it("should include TasksWidget in registry", (): void => {
    expect(widgetRegistry.TasksWidget).toBeDefined();
    expect(widgetRegistry.TasksWidget!.component).toBe(TasksWidget);
  });

  it("should include CalendarWidget in registry", (): void => {
    expect(widgetRegistry.CalendarWidget).toBeDefined();
    expect(widgetRegistry.CalendarWidget!.component).toBe(CalendarWidget);
  });

  it("should include QuickCaptureWidget in registry", (): void => {
    expect(widgetRegistry.QuickCaptureWidget).toBeDefined();
    expect(widgetRegistry.QuickCaptureWidget!.component).toBe(QuickCaptureWidget);
  });

  it("should include OrchestratorEventsWidget in registry", (): void => {
    expect(widgetRegistry.OrchestratorEventsWidget).toBeDefined();
    expect(widgetRegistry.OrchestratorEventsWidget!.component).toBe(OrchestratorEventsWidget);
  });

  it("should have correct metadata for TasksWidget", (): void => {
    const tasksWidget = widgetRegistry.TasksWidget!;
    expect(tasksWidget.name).toBe("TasksWidget");
    expect(tasksWidget.displayName).toBe("Tasks");
    expect(tasksWidget.description).toBeDefined();
    expect(tasksWidget.defaultWidth).toBeGreaterThan(0);
    expect(tasksWidget.defaultHeight).toBeGreaterThan(0);
    expect(tasksWidget.minWidth).toBeGreaterThan(0);
    expect(tasksWidget.minHeight).toBeGreaterThan(0);
  });

  it("should have correct metadata for CalendarWidget", (): void => {
    const calendarWidget = widgetRegistry.CalendarWidget!;
    expect(calendarWidget.name).toBe("CalendarWidget");
    expect(calendarWidget.displayName).toBe("Calendar");
    expect(calendarWidget.description).toBeDefined();
    expect(calendarWidget.defaultWidth).toBeGreaterThan(0);
    expect(calendarWidget.defaultHeight).toBeGreaterThan(0);
  });

  it("should have correct metadata for QuickCaptureWidget", (): void => {
    const quickCaptureWidget = widgetRegistry.QuickCaptureWidget!;
    expect(quickCaptureWidget.name).toBe("QuickCaptureWidget");
    expect(quickCaptureWidget.displayName).toBe("Quick Capture");
    expect(quickCaptureWidget.description).toBeDefined();
    expect(quickCaptureWidget.defaultWidth).toBeGreaterThan(0);
    expect(quickCaptureWidget.defaultHeight).toBeGreaterThan(0);
  });

  it("should export getWidgetByName helper", async (): Promise<void> => {
    const { getWidgetByName } = await import("../WidgetRegistry");
    expect(typeof getWidgetByName).toBe("function");
  });

  it("getWidgetByName should return correct widget", async (): Promise<void> => {
    const { getWidgetByName } = await import("../WidgetRegistry");
    const widget = getWidgetByName("TasksWidget");
    expect(widget).toBeDefined();
    expect(widget?.component).toBe(TasksWidget);
  });

  it("getWidgetByName should return undefined for invalid name", async (): Promise<void> => {
    const { getWidgetByName } = await import("../WidgetRegistry");
    const widget = getWidgetByName("InvalidWidget");
    expect(widget).toBeUndefined();
  });

  it("should export getAllWidgets helper", async (): Promise<void> => {
    const { getAllWidgets } = await import("../WidgetRegistry");
    expect(typeof getAllWidgets).toBe("function");
  });

  it("getAllWidgets should return array of all widgets", async (): Promise<void> => {
    const { getAllWidgets } = await import("../WidgetRegistry");
    const widgets = getAllWidgets();
    expect(Array.isArray(widgets)).toBe(true);
    expect(widgets.length).toBeGreaterThanOrEqual(3);
  });
});
