import { describe, expect, it } from "vitest";

import { getAdminNavigation } from "@/lib/admin-navigation";

const copy = {
  overview: "Overview",
  maintenance: "Maintenance",
  uiKit: "UI Kit",
  portfolio: "Portfolio",
  media: "Media",
  siteSettings: "Site Settings",
  brandSettings: "Brand",
  localizationSettings: "Localization",
  marquee: "Marquee",
  smtp: "SMTP",
};

describe("getAdminNavigation", () => {
  it("returns the full set of top-level sections", () => {
    const nav = getAdminNavigation(copy, "overview");
    const labels = nav.map((item) => item.label);
    expect(labels).toEqual([
      "Overview",
      "Maintenance",
      "UI Kit",
      "Media",
      "Site Settings",
      "Marquee",
      "SMTP",
      "Portfolio",
    ]);
  });

  it("marks the active top-level section", () => {
    const nav = getAdminNavigation(copy, "smtp");
    expect(nav.find((item) => item.label === "SMTP")?.active).toBe(true);
    expect(nav.find((item) => item.label === "Overview")?.active).toBe(false);
  });

  it("expands site-settings and marks the active child", () => {
    const nav = getAdminNavigation(copy, "site-settings", undefined, "localization");
    const siteSettings = nav.find((item) => item.label === "Site Settings");
    expect(siteSettings?.expanded).toBe(true);
    expect(siteSettings?.active).toBe(false); // has a child selected
    const localization = siteSettings?.children?.find((c) => c.label === "Localization");
    expect(localization?.active).toBe(true);
  });

  it("marks the parent active when no child is selected", () => {
    const nav = getAdminNavigation(copy, "site-settings");
    const siteSettings = nav.find((item) => item.label === "Site Settings");
    expect(siteSettings?.active).toBe(true);
    expect(siteSettings?.expanded).toBe(true);
  });

  it("maps portfolio children and de-duplicates hrefs", () => {
    const nav = getAdminNavigation(copy, "portfolio", "projects");
    const portfolio = nav.find((item) => item.label === "Portfolio");
    expect(portfolio?.expanded).toBe(true);
    const hrefs = portfolio?.children?.map((c) => c.href) ?? [];
    expect(new Set(hrefs).size).toBe(hrefs.length); // unique
    const projects = portfolio?.children?.find((c) => c.label === "Projects");
    expect(projects?.active).toBe(true);
  });

  it("activates the new-project child", () => {
    const nav = getAdminNavigation(copy, "portfolio", "new-project");
    const portfolio = nav.find((item) => item.label === "Portfolio");
    expect(portfolio?.children?.find((c) => c.label === "Add Project")?.active).toBe(true);
  });

  it("falls back to default child labels when copy omits them", () => {
    const minimal = { ...copy, brandSettings: undefined, localizationSettings: undefined, marquee: undefined, smtp: undefined };
    const nav = getAdminNavigation(minimal, "overview");
    const siteSettings = nav.find((item) => item.label === "Site Settings");
    expect(siteSettings?.children?.map((c) => c.label)).toEqual(["Brand", "Localization"]);
    expect(nav.find((item) => item.href.endsWith("/marquee"))?.label).toBe("Marquee");
    expect(nav.find((item) => item.href.endsWith("/smtp"))?.label).toBe("SMTP");
  });

  it("gives every item an icon and href", () => {
    const nav = getAdminNavigation(copy, "overview");
    for (const item of nav) {
      expect(item.icon).toBeTruthy();
      expect(typeof item.href).toBe("string");
    }
  });
});
