/**
 * The health-fact matrix, migrated whole from the Overview status rows this
 * replaces. Same states, same words, same link matrix — with the Diagnostics
 * links now narrowing the page the strip sits on rather than navigating to it.
 */

import { health } from "@/lib/healthFixture";
import { healthFacts, type HealthFact } from "./healthFacts";

const LOCALE = "en-GB";
const TZ = "UTC";

function factsBy(overrides: Parameters<typeof health>[0] = {}): Record<string, HealthFact> {
	return Object.fromEntries(healthFacts(health(overrides), LOCALE, TZ).map((fact) => [fact.key, fact]));
}

test("a healthy box is five quiet facts, none of them linking anywhere", () => {
	const facts = healthFacts(health(), LOCALE, TZ);
	expect(facts.map((fact) => fact.key)).toEqual(["protection", "upstreams", "query_history", "diagnostics", "disk"]);
	expect(facts.every((fact) => fact.tone === "ok")).toBe(true);
	expect(facts.every((fact) => fact.link === undefined)).toBe(true);
});

test("protection: active, paused indefinitely, paused until a time, unavailable", () => {
	expect(factsBy()["protection"].value).toBe("Active");
	expect(factsBy({ protection: { state: "paused", until: null } })["protection"].value).toBe("Paused");
	const timed = factsBy({ protection: { state: "paused", until: Date.UTC(2026, 0, 1, 14, 5) / 1000 } })["protection"];
	expect(timed.value).toBe("Paused until 14:05");
	const gone = factsBy({ protection: { state: "unavailable", until: null } })["protection"];
	expect(gone.value).toBe("Unavailable");
	expect(gone.link).toEqual({ kind: "route", to: "/configuration/protection", label: "Blocklist sources" });
});

test("a pause never reads as a fault, and never carries a way out", () => {
	const paused = factsBy({ protection: { state: "paused", until: null } })["protection"];
	expect(paused.tone).toBe("notice");
	expect(paused.link).toBeUndefined();
});

test("upstreams count the enabled pool, and only an empty one links out", () => {
	const ok = factsBy({ upstreams: { state: "ok", available: 1, total: 3 } })["upstreams"];
	expect(ok.value).toBe("Available");
	expect(ok.detail).toBe("1 of 3 enabled");
	expect(ok.link).toBeUndefined();
	const none = factsBy({ upstreams: { state: "unavailable", available: 0, total: 3 } })["upstreams"];
	expect(none.value).toBe("None reachable");
	expect(none.link).toEqual({ kind: "route", to: "/configuration/resolution", label: "Upstreams" });
});

test("query history: losing blames the disk gate, a failed writer blames the query log", () => {
	const losing = factsBy({ query_history: { state: "losing", dropped_total: 0, last_drop_s: null } })[
		"query_history"
	];
	expect(losing.value).toBe("Losing rows");
	expect(losing.link).toEqual({ kind: "filter", component: "disk", label: "Disk diagnostics" });
	const failed = factsBy({ query_history: { state: "failed", dropped_total: 0, last_drop_s: null } })[
		"query_history"
	];
	expect(failed.value).toBe("Writer failed");
	expect(failed.link).toEqual({ kind: "filter", component: "query_log", label: "Query log diagnostics" });
});

test("drops are reported while recording, with or without a stamp on the last one", () => {
	const stamped = factsBy({
		query_history: { state: "recording", dropped_total: 5, last_drop_s: Date.UTC(2026, 0, 1, 9, 30) / 1000 },
	})["query_history"];
	expect(stamped.tone).toBe("ok");
	expect(stamped.detail).toBe("5 queries dropped, last at 09:30");
	const unstamped = factsBy({ query_history: { state: "recording", dropped_total: 1, last_drop_s: null } })[
		"query_history"
	];
	expect(unstamped.detail).toBe("1 query dropped");
	expect(factsBy()["query_history"].detail).toBeUndefined();
});

test("an unavailable diagnostics store explains itself and links nowhere", () => {
	const fact = factsBy({ diagnostics: { state: "unavailable", active_warnings: 0, active_errors: 0 } })[
		"diagnostics"
	];
	expect(fact.value).toBe("Unavailable");
	expect(fact.detail).toContain("not being recorded");
	// The page a link would filter is the thing that is broken.
	expect(fact.link).toBeUndefined();
});

test("storage names the three disk states and always shows what is free", () => {
	expect(factsBy()["disk"].value).toBe("OK");
	expect(factsBy()["disk"].link).toBeUndefined();
	const low = factsBy({ disk: { state: "low", free_bytes: 1024 } })["disk"];
	expect(low.value).toBe("Low");
	expect(low.tone).toBe("warn");
	expect(low.link).toEqual({ kind: "filter", component: "disk", label: "Disk diagnostics" });
	const critical = factsBy({ disk: { state: "critical", free_bytes: 0 } })["disk"];
	expect(critical.value).toBe("Critical");
	expect(critical.tone).toBe("danger");
	expect(critical.detail).toBe("0 B free");
});
