/* Marque Talent Desk — the route table.
   Deliberately NOT routes: the ⌘K palette, the notifications panel, the row
   menus, the confirm dialogs, the pass-on-brief and mark-published dialogs, the
   briefs pipeline tabs, the deliverables week ladder. They are states of a
   screen, not destinations — a breadcrumb that wants one of them is a bug in
   the screen.

   Five top-level destinations, so this product runs the TOP-NAV shell. Brands
   deliberately did NOT become a sixth: a brand is reached through the work that
   names it — a brief, a deal, an invoice — and six items in the bar would have
   spent the last slot the shell has on a screen nobody opens cold.

   There is no deals list either: a deal is always reached through the work that
   produced it. It sits under Briefs in the tree because that is where it came
   from, so a deal screen keeps Briefs marked active and its breadcrumb resolves
   without inventing a namespace. Brands sit there for the same reason. */

const MARQUE_NAV = [
  { path: "/", label: "Roster" },
  { path: "/briefs", label: "Briefs", badge: "briefsToTriage" },  { path: "/deliverables", label: "Deliverables", badge: "deliverablesNeedingAttention" },
  { path: "/performance", label: "Performance" },
  { path: "/money", label: "Money", badge: "invoicesOverdue" },

  /* Registered, not in the nav. The two creates are routes rather than dialogs
     because each is a real form (AGENTS §5), and each hangs off the list whose
     records it makes. */
  { path: "/talent/new", label: "New creator", parent: "/", offSidebar: true },
  { path: "/talent/:id", label: "Creator", parent: "/", offSidebar: true },
  { path: "/talent/:id/statement", label: "Statement", parent: "/talent/:id", offSidebar: true },
  { path: "/briefs/new", label: "New brief", parent: "/briefs", offSidebar: true },
  { path: "/briefs/:id", label: "Brief", parent: "/briefs", offSidebar: true },
  { path: "/brands/:id", label: "Brand", parent: "/briefs", offSidebar: true },
  { path: "/deals/:id", label: "Deal", parent: "/briefs", offSidebar: true },
  { path: "/deliverables/:id", label: "Deliverable", parent: "/deliverables", offSidebar: true },
  { path: "/performance/:id", label: "Wrap report", parent: "/performance", offSidebar: true },
  { path: "/inbox", label: "Inbox", offSidebar: true },
  { path: "/search", label: "Search results", offSidebar: true },
  { path: "/account/profile", label: "Manager profile", offSidebar: true },
  { path: "/account/agency", label: "Agency and commission", offSidebar: true },
  { path: "/account/team", label: "Team and ownership", offSidebar: true },

  /* Auth namespace — excluded from the shell nav; no product chrome. Every one
     of these is linked from another auth screen: nothing here is a dead end and
     nothing is registered that the flow cannot reach (AGENTS §7.2). */
  { path: "/signin", label: "Sign in", offSidebar: true },
  { path: "/signout", label: "Sign out", offSidebar: true },
  { path: "/forgot-password", label: "Forgot password", offSidebar: true },
  { path: "/forgot-password/sent", label: "Reset link sent", offSidebar: true },
  { path: "/reset-password", label: "Set a new password", offSidebar: true },
  { path: "/reset-password/done", label: "Password set", offSidebar: true },
  { path: "/reset-password/expired", label: "Link expired", offSidebar: true },
  { path: "/account-locked", label: "Account locked", offSidebar: true },
];

const MARQUE_ROUTES = indexRoutes(MARQUE_NAV);

/* Projections the shell renders — derived by filtering the tree, never retyped. */
const MARQUE_DESTINATIONS = MARQUE_NAV.filter((r) => !r.offSidebar);

const MARQUE_ACCOUNT_MENU = [
  MARQUE_ROUTES["/account/profile"],
  MARQUE_ROUTES["/account/agency"],
  MARQUE_ROUTES["/account/team"],
  MARQUE_ROUTES["/signout"],
];

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

/* The preview menu's page list — this table read whole and grouped, never
   retyped, with the parameterised routes resolved against the fixture so the
   link lands on a real record rather than on ":id". A pattern with no fixture
   example is left out rather than offered as a link resolving nowhere. */
const MARQUE_PAGES = (data) => {
  const creator = data.creators[0], brief = data.briefs[0], deal = data.deals[0];
  /* The brand with the most work behind it, so the link lands on a full page
     rather than on the emptiest one in the fixture. */
  const brand = data.brands.slice().sort((a, b) =>
    (dealsForBrand(data.deals, b).length - dealsForBrand(data.deals, a).length) ||
    (briefsForBrand(data.briefs, b).length - briefsForBrand(data.briefs, a).length))[0];
  /* The deliverable that needs somebody, and a campaign that has finished — the
     two records those screens are worth opening on. */
  const rows = allDeliverables(data.deals, data.creators);
  const item = (deliverablesNeedingAttention(rows, data.now)[0] || rows[0]);
  const measured = completeDeals(data.deals)[0] || deal;
  return templatePages(MARQUE_NAV, MARQUE_AUTH_PATHS, {
    "/talent/:id": creator ? "/talent/" + creator.id : null,
    "/talent/:id/statement": creator ? "/talent/" + creator.id + "/statement" : null,
    "/briefs/:id": brief ? "/briefs/" + brief.id : null,
    "/brands/:id": brand ? "/brands/" + brand.id : null,
    "/deals/:id": deal ? "/deals/" + deal.id : null,
    "/deliverables/:id": item ? "/deliverables/" + item.item.id : null,
    "/performance/:id": measured ? "/performance/" + measured.id : null,
  });
};

Object.assign(window, { MARQUE_NAV, MARQUE_ROUTES, MARQUE_DESTINATIONS, MARQUE_ACCOUNT_MENU, MARQUE_AUTH_PATHS, MARQUE_PAGES });
