/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import { buildSupportMailto } from "./supportMailto.js";

function decodeMailto(mailto) {
	const url = new URL(mailto);
	return {
		to: url.pathname,
		subject: url.searchParams.get("subject") || "",
		body: url.searchParams.get("body") || "",
	};
}

describe("lib/frontend/support/supportMailto", () => {
	it("builds a branch-specific subject and includes context lines", () => {
		const mailto = buildSupportMailto({
			user: { role: "branch", branchId: "NL20" },
			pathname: "/profile",
			currentUrl: "https://example.local/profile",
			userAgent: "Vitest-UA",
		});

		const parsed = decodeMailto(mailto);

		expect(parsed.to).toBe("info@attus.de");
		expect(parsed.subject).toBe("Support – RHL Lieferscheine (NL20)");
		expect(parsed.body).toContain("Benutzer: Niederlassung (NL20)");
		expect(parsed.body).toContain("Route: /profile");
		expect(parsed.body).toContain("URL: https://example.local/profile");
		expect(parsed.body).toContain("User-Agent: Vitest-UA");
		expect(parsed.body).toContain("Zeitpunkt:");
		expect(parsed.body).toContain("ISO:");
	});

	it("builds a generic subject without branch", () => {
		const mailto = buildSupportMailto({
			user: { role: "admin", branchId: null },
			pathname: "/",
			currentUrl: "",
			userAgent: "",
		});

		const parsed = decodeMailto(mailto);

		expect(parsed.to).toBe("info@attus.de");
		expect(parsed.subject).toBe("Support – RHL Lieferscheine");
		expect(parsed.body).toContain("Benutzer: Admin");
		expect(parsed.body).toContain("Route: /");
		expect(parsed.body).toContain("URL: (bitte einfügen)");
		expect(parsed.body).toContain("User-Agent: (unbekannt)");
	});
});
