/* Marketing Desk — the route table.
   Deliberately NOT routes: the ⌘K palette, the notification panel, the row
   menus, the period scope control, the calendar's month/week switch, the filter
   bars, the new-broadcast dialog and every confirm dialog. They are states of a
   screen. A breadcrumb that wants one of them is a bug in the screen, not a
   missing route.

   Twelve top-level destinations, dense record work and four per-destination
   counts, so this product runs the SIDEBAR shell in its labelled mode. Twelve is
   outside §6.3's 5–9 for a single group, so the nav carries three groups, each
   labelled: what is being sent, who it goes to, and who is doing it.

   Parameterised routes exist where the thing has its own controls and its own
   numbers: a journey (its step ladder), a broadcast (its send report), an
   audience (its members), a person (what they own and what they have written), a
   contact (what has reached them). Three creation screens are routes because
   they are too big for a dialog and staged where their parent is instant.

   Activity is registered but off the sidebar: it is one feed reached from the
   Inbox, from Overview and from every person's profile, and a thirteenth
   destination for it would push the nav past what a group can hold.

   Settings is six screens plus the team table, all off the sidebar and reached
   from the account menu. The auth namespace carries sign-in and the whole
   password reset flow; signup, MFA and lockout are not built, so nothing links
   to them. */

const MD_NAV = [
  /* Sending */
  { path: "/", label: "Overview", icon: "activity", group: "Sending" },
  { path: "/calendar", label: "Calendar", icon: "calendar-days", group: "Sending" },
  { path: "/journeys", label: "Journeys", icon: "workflow", badge: "journeysNeedingAttention", group: "Sending" },
  { path: "/broadcasts", label: "Broadcasts", icon: "send", group: "Sending" },
  { path: "/approvals", label: "Approvals", icon: "check-check", badge: "approvalsWaiting", group: "Sending" },

  /* Who it goes to */
  { path: "/audiences", label: "Audiences", icon: "users", group: "Audience" },
  { path: "/contacts", label: "Contacts", icon: "contact", group: "Audience" },
  { path: "/content", label: "Content", icon: "file-text", group: "Audience" },

  /* Who is doing it, and whether it arrives */
  { path: "/people", label: "People", icon: "user-round", group: "Team" },
  { path: "/inbox", label: "Inbox", icon: "message-circle", badge: "mentionsOwed", group: "Team" },
  { path: "/reports", label: "Reports", icon: "chart-line", group: "Team" },
  { path: "/deliverability", label: "Deliverability", icon: "shield-check", badge: "domainsUnauthenticated", group: "Team" },

  /* Registered, not in the nav. */
  { path: "/activity", label: "Activity", offSidebar: true },
  { path: "/journeys/new", label: "New journey", parent: "/journeys", offSidebar: true },
  { path: "/journeys/:id", label: "Journey", parent: "/journeys", offSidebar: true },
  { path: "/journeys/:id/steps", label: "Steps", parent: "/journeys/:id", offSidebar: true },
  { path: "/broadcasts/:id", label: "Broadcast", parent: "/broadcasts", offSidebar: true },
  { path: "/audiences/new", label: "New audience", parent: "/audiences", offSidebar: true },
  { path: "/audiences/:id", label: "Audience", parent: "/audiences", offSidebar: true },
  { path: "/contacts/:id", label: "Contact", parent: "/contacts", offSidebar: true },
  { path: "/people/:id", label: "Person", parent: "/people", offSidebar: true },
  { path: "/me/profile", label: "Your profile", offSidebar: true },
  { path: "/settings", label: "Workspace settings", offSidebar: true },
  { path: "/settings/team", label: "Team and roles", parent: "/settings", offSidebar: true },
  { path: "/settings/channels", label: "Connected channels", parent: "/settings", offSidebar: true },
  { path: "/settings/sending", label: "Sending domains", parent: "/settings", offSidebar: true },
  { path: "/settings/notifications", label: "Notifications", parent: "/settings", offSidebar: true },
  { path: "/settings/billing", label: "Billing and plan", parent: "/settings", offSidebar: true },
  { path: "/settings/api", label: "API keys and webhooks", parent: "/settings", offSidebar: true },

  /* Auth namespace — excluded from the shell nav; no product chrome. The reset
     flow is five screens because AGENTS §7.5 wants all of them: request, the
     sent confirmation, set-new behind the token, the expired-token state and the
     outcome. Signup, MFA and lockout are still not built, so nothing links to
     them. */
  { path: "/signin", label: "Sign in", offSidebar: true },
  { path: "/forgot-password", label: "Forgot password", offSidebar: true },
  { path: "/forgot-password/sent", label: "Reset link sent", parent: "/forgot-password", offSidebar: true },
  { path: "/reset-password", label: "Set a new password", offSidebar: true },
  { path: "/reset-password/expired", label: "Reset link expired", parent: "/reset-password", offSidebar: true },
  { path: "/reset-password/done", label: "Password changed", parent: "/reset-password", offSidebar: true },
];

const MD_ROUTES = indexRoutes(MD_NAV);

/* Projections the shell renders — derived by filtering the tree, never retyped.
   Three groups, so every group carries a label (§6.3). */
const MD_GROUP_ORDER = ["Sending", "Audience", "Team"];
const MD_SIDEBAR = MD_GROUP_ORDER.map((group) => ({
  label: group === "Audience" ? "Who it goes to" : group === "Team" ? "Team and health" : "Sending",
  items: MD_NAV.filter((r) => !r.offSidebar && r.group === group),
}));

/* The account menu carries registered routes plus sign out (§6.2). */
const MD_ACCOUNT_MENU = [
  MD_ROUTES["/me/profile"],
  MD_ROUTES["/settings"],
  MD_ROUTES["/settings/team"],
  MD_ROUTES["/settings/channels"],
  MD_ROUTES["/settings/sending"],
  MD_ROUTES["/settings/notifications"],
  MD_ROUTES["/settings/billing"],
  MD_ROUTES["/settings/api"],
];

/* Paths that render without product chrome (AGENTS §7.1). */
const MD_AUTH_PATHS = ["/signin", "/forgot-password", "/forgot-password/sent", "/reset-password", "/reset-password/expired", "/reset-password/done"];

/* /journeys/:id → /journeys/jny_onboard. Breadcrumbs and every nested row use
   it, so no screen builds a path by concatenation. */
const resolvePath = (pattern, params) => pattern.replace(/:([^/]+)/g, (_, key) => (params && params[key] != null ? params[key] : ":" + key));

/* The preview menu's page list — this table read whole and grouped, with the
   parameterised routes resolved against the fixture so each link lands on a real
   record rather than on ":id". */
const MD_PAGES = (data) => {
  const journey = data.journeys[0], broadcast = data.broadcasts[0], audience = data.audiences[0];
  const draft = data.journeys.find((j) => j.status === "draft");
  const person = data.members[0], contact = data.contacts[0];
  return templatePages(MD_NAV, MD_AUTH_PATHS, {
    "/journeys/:id": journey ? resolvePath("/journeys/:id", { id: journey.id }) : null,
    "/journeys/:id/steps": draft ? resolvePath("/journeys/:id/steps", { id: draft.id }) : null,
    "/broadcasts/:id": broadcast ? resolvePath("/broadcasts/:id", { id: broadcast.id }) : null,
    "/audiences/:id": audience ? resolvePath("/audiences/:id", { id: audience.id }) : null,
    "/people/:id": person ? resolvePath("/people/:id", { id: person.id }) : null,
    "/contacts/:id": contact ? resolvePath("/contacts/:id", { id: contact.id }) : null,
  });
};

Object.assign(window, { MD_NAV, MD_ROUTES, MD_SIDEBAR, MD_GROUP_ORDER, MD_ACCOUNT_MENU, MD_AUTH_PATHS, MD_PAGES, resolvePath });
