import { describe, expect, test } from "vitest";
import {
  dimensionLabDashboardFixture,
} from "./dimensionlab";
import type {
  DashboardDocument,
  DatasourceReference,
  ServiceEntry,
  TelemetryCard,
} from "@dimensionlab/dashboard-model";

describe("Dimension Lab dashboard seed", () => {
  test("defines the primary first-screen sections as model data", () => {
    expect(dimensionLabDashboardFixture.metadata.title).toBe("System Overview");
    expect(dimensionLabDashboardFixture.layout.telemetry).toHaveLength(16);
    expect(dimensionLabDashboardFixture.layout.serviceGroups).toEqual([
      "essentials",
      "monitoring",
      "ai-automation",
      "systems",
      "runtime-health",
    ]);
    expect(dimensionLabDashboardFixture.layout.statusStrips).toEqual([
      "footer-status",
    ]);
    expect(dimensionLabDashboardFixture.layout.modules).toEqual([
      "weather-amsterdam",
      "runtime-health-summary",
    ]);
  });

  test("keeps icon, link, and datasource references in seed data", () => {
    const iconIds = [
      ...dimensionLabDashboardFixture.telemetry.map((card) => card.icon),
      ...allServices(dimensionLabDashboardFixture).map((service) => service.icon),
      ...(dimensionLabDashboardFixture.modules || []).map((module) => module.icon),
    ].filter((icon): icon is string => Boolean(icon));

    expect(
      dimensionLabDashboardFixture.telemetry.every(
        (card) => card.icon && card.datasource,
      ),
    ).toBe(true);
    expect(
      dimensionLabDashboardFixture.telemetry.some(
        (card) => card.datasource?.type === "external" &&
          card.datasource.adapter === "prometheus",
      ),
    ).toBe(true);
    expect(
      allServices(dimensionLabDashboardFixture).some(
        (service) => service.datasource?.type === "external" &&
          service.datasource.adapter === "http-status",
      ),
    ).toBe(true);

    for (const service of allServices(dimensionLabDashboardFixture)) {
      expect(service.icon, `${service.id} must declare an Iconify id`).toBeTruthy();
      expect(verifiedSeedIconIds.has(service.icon || ""), `${service.id} icon must be verified`).toBe(true);
      expect(service.datasource, `${service.id} must declare health source`).toBeTruthy();
      if (service.link) {
        expect(service.link.label, `${service.id} link needs an accessible label`).toBeTruthy();
        expect(service.link.external).toBe(true);
      }
    }

    for (const icon of iconIds) {
      expect(verifiedSeedIconIds.has(icon), `${icon} must be verified`).toBe(true);
    }
  });

  test("labels fallback values and unresolved adapters explicitly", () => {
    const placeholders = collectDatasources(dimensionLabDashboardFixture).filter(
      (datasource) => datasource.type === "placeholder",
    );
    const fallbackText = [
      ...dimensionLabDashboardFixture.telemetry.map((card) => card.detail || ""),
      ...allServices(dimensionLabDashboardFixture).map((service) => service.detail || ""),
      ...dimensionLabDashboardFixture.statusStrips.flatMap((strip) =>
        strip.items.map((item) => item.value),
      ),
      ...(dimensionLabDashboardFixture.modules || []).map((module) => module.detail || ""),
    ].join("\n").toLowerCase();

    expect(fallbackText).toContain("fallback");
    for (const service of allServices(dimensionLabDashboardFixture)) {
      expect(service.detail?.toLowerCase(), `${service.id} detail must label fallback state`).toContain("fallback");
    }
    for (const statusItem of dimensionLabDashboardFixture.statusStrips.flatMap((strip) => strip.items)) {
      expect(statusItem.value.toLowerCase(), `${statusItem.id} value must label fallback state`).toContain("fallback");
    }
    expect(placeholders.length).toBeGreaterThan(0);
    for (const datasource of placeholders) {
      expect(datasource.reason.toLowerCase()).toMatch(/fallback|pending|unresolved/);
    }
  });
});

function allServices(document: DashboardDocument): ServiceEntry[] {
  return document.serviceGroups.flatMap((group) => group.services);
}

function collectDatasources(document: DashboardDocument): DatasourceReference[] {
  const telemetry = document.telemetry
    .map((card: TelemetryCard) => card.datasource)
    .filter((datasource): datasource is DatasourceReference => Boolean(datasource));
  const services = allServices(document)
    .map((service) => service.datasource)
    .filter((datasource): datasource is DatasourceReference => Boolean(datasource));
  const modules = (document.modules || [])
    .map((module) => module.datasource)
    .filter((datasource): datasource is DatasourceReference => Boolean(datasource));

  return [...telemetry, ...services, ...modules];
}

const verifiedSeedIconIds = new Set([
  "mdi:account-hard-hat-outline",
  "mdi:backup-restore",
  "mdi:brain",
  "mdi:cpu-64-bit",
  "mdi:docker",
  "mdi:expansion-card",
  "mdi:fan",
  "mdi:harddisk",
  "mdi:image-edit-outline",
  "mdi:memory",
  "mdi:pulse",
  "mdi:robot-outline",
  "mdi:router-network",
  "mdi:text-box-search",
  "mdi:thermometer",
  "mdi:web",
  "mdi:weather-sunny",
  "simple-icons:adguard",
  "simple-icons:adminer",
  "simple-icons:amazonwebservices",
  "simple-icons:cockpit",
  "simple-icons:forgejo",
  "simple-icons:grafana",
  "simple-icons:n8n",
  "simple-icons:ollama",
  "simple-icons:postgresql",
  "simple-icons:prometheus",
  "simple-icons:uptimekuma",
  "simple-icons:vaultwarden",
  "simple-icons:wikidotjs",
]);
