import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { type DashboardDocument } from "@dimensionlab/dashboard-model";
import { dimensionLabDashboardFixture } from "$lib/dashboard-seed/dimensionlab";
import { genericDashboardFixture } from "@dimensionlab/dashboard-model/fixtures";
import { dashboardDocumentToUiDashboard } from "./model-renderer";

const appRoot = process.cwd().endsWith(`${join("apps", "web")}`)
  ? process.cwd()
  : join(process.cwd(), "apps", "web");

describe("dashboard model renderer", () => {
  test("projects the Dimension Lab model into UI component props", () => {
    const dashboard = dashboardDocumentToUiDashboard(dimensionLabDashboardFixture);

    expect(dashboard.title).toBe(dimensionLabDashboardFixture.metadata.title);
    expect(dashboard.subtitle).toBe(dimensionLabDashboardFixture.metadata.subtitle);
    expect(dashboard.telemetry.map((card) => card.id)).toEqual(
      dimensionLabDashboardFixture.layout.telemetry,
    );
    expect(dashboard.telemetry[0]).toMatchObject({
      label: "Infra RAM",
      icon: "mdi:memory",
      severity: "ok",
    });
    expect(dashboard.serviceGroups.map((group) => group.id)).toEqual(
      dimensionLabDashboardFixture.layout.serviceGroups,
    );
    expect(dashboard.serviceGroups.find((group) => group.id === "runtime-health")?.layout).toBe(
      "grid",
    );
    expect(dashboard.modules.map((module) => module.id)).toEqual([
      "weather-amsterdam",
      "runtime-health-summary",
    ]);
    expect(dashboard.statusItems.map((item) => item.id)).toEqual([
      "footer-status:system-status",
      "footer-status:last-sync",
      "footer-status:uptime",
      "footer-status:load-avg",
      "footer-status:auto-refresh",
    ]);
    expect(dashboard.statusStripId).toBe("footer-status");
  });

  test("projects a second fixture through the same mapper", () => {
    const dashboard = dashboardDocumentToUiDashboard(genericDashboardFixture);

    expect(dashboard.title).toBe("Operations Console");
    expect(dashboard.telemetry.map((card) => card.id)).toEqual([
      "service-uptime",
      "queue-depth",
    ]);
    expect(dashboard.serviceGroups[0]?.services.map((service) => service.id)).toEqual([
      "identity",
      "scheduler",
    ]);
    expect(dashboard.modules[0]).toMatchObject({
      id: "ambient",
      title: "Environment",
      icon: "mdi:radar",
    });
  });

  test("returns intentional empty arrays for missing optional sections", () => {
    const emptyModel: DashboardDocument = {
      ...genericDashboardFixture,
      layout: {
        density: "compact",
        telemetry: [],
        serviceGroups: [],
        statusStrips: [],
        modules: [],
      },
      telemetry: [],
      serviceGroups: [],
      statusStrips: [],
      modules: [],
    };

    const dashboard = dashboardDocumentToUiDashboard(emptyModel);

    expect(dashboard.telemetry).toEqual([]);
    expect(dashboard.serviceGroups).toEqual([]);
    expect(dashboard.statusItems).toEqual([]);
    expect(dashboard.modules).toEqual([]);
  });

  test("does not hardcode environment-specific content in mapper source", () => {
    const source = readFileSync(
      join(appRoot, "src/lib/ui-adapter/model-renderer.ts"),
      "utf8",
    )
      .toLowerCase()
      .replaceAll("@dimensionlab/dashboard-model", "@internal/dashboard-model")
      .replaceAll("@dimensionlab/ui", "@internal/ui");

    expect(source).not.toContain("dimension");
    expect(source).not.toContain("vaultwarden");
    expect(source).not.toContain("forgejo");
  });
});
