/* Hearth — the route table.
   Deliberately NOT routes: the ⌘K palette, the notifications panel, the room
   controls, the live camera view, the pairing flow, the connection test, every
   filter bar and tab strip, the arm/disarm sheet, the schedule editor, the code
   sheet, every confirm dialog and every staged dialog. They are states of a
   screen. A breadcrumb that wants one of them is a bug in the screen, not a
   missing route.

   TEN top-level destinations over two groups of five. This is a house with five
   people in it, not a business: the sidebar carries what somebody standing in
   the hall would want, and the register-keeping — the bills, the handed-out
   codes, the trade contacts, the whole log — hangs off the destination it
   belongs to rather than taking a line of its own. Every one of those four is
   still a registered route with a screen behind it; each is reached from its
   parent, which is where a person looks for it.

   The first group is what the house is DOING: the verdict, the rooms and their
   controls, the cameras, the heating, the doors. The second is what keeps it
   running: the devices and their radio, the routines, the people, the
   electricity, the work owed to the building.

   There is no /rooms/:id and no /cameras/:id. Rooms holds every room and its
   controls at once, which is what a house is; the camera screen focuses one
   camera as a STATE, because a camera has no fact a card cannot carry.

   Parameterised routes exist where the thing has its own controls, its own
   history and its own numbers: a device, a person, a service provider, a job of
   work. Rooms, cameras, zones, entries, codes and bills do not — each is a row
   with a control, complete where it stands.

   Settings is one namespace of five screens off the sidebar, reached from the
   account menu, with a tab strip on the page derived from this table. 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 HEARTH_GROUPS = ["The house", "Running it"];

const HEARTH_NAV = [
  /* The house — what it is doing right now, and what you would reach for. */
  { path: "/", label: "Today", icon: "sun", group: "The house" },
  { path: "/rooms", label: "Rooms", icon: "door-open", group: "The house" },
  { path: "/cameras", label: "Cameras", icon: "video", group: "The house", badge: "camerasBlind" },
  { path: "/climate", label: "Climate", icon: "thermometer", group: "The house" },
  { path: "/security", label: "Security", icon: "shield", group: "The house", badge: "entriesInsecure" },

  /* Running it — the kit, the rules, the people and the money. */
  { path: "/devices", label: "Devices", icon: "plug", group: "Running it", badge: "devicesNeedingYou" },
  { path: "/routines", label: "Routines", icon: "workflow", group: "Running it" },
  { path: "/people", label: "People", icon: "users", group: "Running it", badge: "requestsWaiting" },
  { path: "/energy", label: "Energy", icon: "zap", group: "Running it" },
  { path: "/upkeep", label: "Upkeep", icon: "wrench", group: "Running it" },

  /* Registered, reached from their parent rather than the sidebar. Each is a
     register somebody consults occasionally, and each already has one obvious
     place a person would look for it. */
  { path: "/spend", label: "Bills", parent: "/energy", offSidebar: true },
  { path: "/access", label: "Access", parent: "/people", offSidebar: true },
  { path: "/providers", label: "Providers", parent: "/upkeep", offSidebar: true },
  { path: "/log", label: "Log", parent: "/", offSidebar: true },

  /* Records. */
  { path: "/devices/:id", label: "Device", parent: "/devices", offSidebar: true },
  { path: "/people/:id", label: "Person", parent: "/people", offSidebar: true },
  { path: "/providers/:id", label: "Provider", parent: "/providers", offSidebar: true },
  { path: "/upkeep/:id", label: "Job", parent: "/upkeep", offSidebar: true },
  { path: "/account/profile", label: "Your profile", offSidebar: true },

  /* Settings — one namespace, five screens, a tab strip on the page. */
  { path: "/settings", label: "Residence", offSidebar: true, group: "Settings" },
  { path: "/settings/tariff", label: "Tariff and meters", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/security", label: "Alarm and response", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/notifications", label: "Notifications", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/data", label: "Data and privacy", parent: "/settings", offSidebar: true, group: "Settings" },

  /* Auth namespace — excluded from the shell nav; no product chrome. */
  { 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 HEARTH_ROUTES = indexRoutes(HEARTH_NAV);

/* Projections the shell renders — derived by filtering the tree, never retyped. */
const HEARTH_SIDEBAR = HEARTH_GROUPS.map((group) => ({
  label: group,
  items: HEARTH_NAV.filter((r) => !r.offSidebar && r.group === group),
}));

const HEARTH_SETTINGS_NAV = HEARTH_NAV.filter((r) => r.group === "Settings");

/* The account menu carries registered routes plus sign out (§6.2). Settings is
   one entry: its own tab strip carries the five screens inside it. */
const HEARTH_ACCOUNT_MENU = [HEARTH_ROUTES["/account/profile"], HEARTH_ROUTES["/settings"]];

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

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 HEARTH_PAGES = (data) => {
  const device = data.devices.find((d) => d.status === "online") || data.devices[0];
  const person = data.members[0];
  const provider = data.providers[0];
  const job = data.jobs[0];
  return templatePages(HEARTH_NAV, HEARTH_AUTH_PATHS, {
    "/devices/:id": device ? "/devices/" + device.id : null,
    "/people/:id": person ? "/people/" + person.id : null,
    "/providers/:id": provider ? "/providers/" + provider.id : null,
    "/upkeep/:id": job ? "/upkeep/" + job.id : null,
  });
};

Object.assign(window, {
  HEARTH_GROUPS, HEARTH_NAV, HEARTH_ROUTES, HEARTH_SIDEBAR, HEARTH_SETTINGS_NAV,
  HEARTH_ACCOUNT_MENU, HEARTH_AUTH_PATHS, HEARTH_PAGES, resolvePath,
});
