export type NavKey =
  | "today"
  | "crew"
  | "studio"
  | "scout"
  | "outbox"
  | "jobs"
  | "brands"
  | "playbooks"
  | "insights"
  | "benchmark"
  | "utm";

export type NavItem = {
  key: NavKey;
  path: string;
  /** i18n key，例如 nav.today */
  labelKey: string;
  /** 後備中文（無 i18n 時） */
  label: string;
  en: string;
};

/** 側欄完整導覽 */
export const primaryNav: NavItem[] = [
  { key: "today", path: "/app/today", labelKey: "nav.today", label: "今日", en: "Today" },
  { key: "crew", path: "/app/crew", labelKey: "nav.crew", label: "帳號", en: "Crew" },
  { key: "studio", path: "/app/studio", labelKey: "nav.studio", label: "創作", en: "Studio" },
  { key: "scout", path: "/app/scout", labelKey: "nav.scout", label: "海巡", en: "Patrol" },
  { key: "outbox", path: "/app/outbox", labelKey: "nav.outbox", label: "發送", en: "Outbox" },
  { key: "jobs", path: "/app/jobs", labelKey: "nav.jobs", label: "任務", en: "Jobs" },
  { key: "brands", path: "/app/brands", labelKey: "nav.brands", label: "品牌", en: "Brands" },
  {
    key: "playbooks",
    path: "/app/playbooks",
    labelKey: "nav.playbooks",
    label: "市集",
    en: "Playbooks",
  },
  {
    key: "insights",
    path: "/app/insights",
    labelKey: "nav.insights",
    label: "成效",
    en: "Insights",
  },
  {
    key: "benchmark",
    path: "/app/benchmark",
    labelKey: "nav.benchmark",
    label: "基準",
    en: "Benchmark",
  },
  { key: "utm", path: "/app/utm", labelKey: "nav.utm", label: "追蹤", en: "UTM" },
];

/** 手機底欄固定 4 格（主流程） */
export const mobileDockPrimaryKeys: NavKey[] = ["today", "studio", "scout", "outbox"];

/** 手機底欄「更多」內項目 */
export const mobileDockMoreKeys: NavKey[] = [
  "crew",
  "jobs",
  "brands",
  "playbooks",
  "insights",
  "benchmark",
  "utm",
];

export type NavGroupKey = "workflow" | "accounts" | "growth";

export type NavGroup = {
  key: NavGroupKey;
  /** i18n key，例如 navGroup.workflow */
  labelKey: string;
  keys: NavKey[];
};

/** 側欄二級分類：主流程 → 帳號品牌 → 成長工具，避免 11 項全部散在同一層 */
export const navGroups: NavGroup[] = [
  { key: "workflow", labelKey: "navGroup.workflow", keys: ["today", "studio", "scout", "outbox", "jobs"] },
  { key: "accounts", labelKey: "navGroup.accounts", keys: ["crew", "brands"] },
  {
    key: "growth",
    labelKey: "navGroup.growth",
    keys: ["insights", "benchmark", "playbooks", "utm"],
  },
];

export function navItemsByKeys(keys: NavKey[]): NavItem[] {
  const map = new Map(primaryNav.map((n) => [n.key, n]));
  return keys.map((k) => map.get(k)).filter((x): x is NavItem => Boolean(x));
}

/** 依 navGroups 分類，只保留 keys 中真的存在的項目；空分類不回傳。 */
export function navGroupedItemsByKeys(
  keys: NavKey[],
): { group: NavGroup; items: NavItem[] }[] {
  const allow = new Set(keys);
  return navGroups
    .map((group) => ({ group, items: navItemsByKeys(group.keys.filter((k) => allow.has(k))) }))
    .filter(({ items }) => items.length > 0);
}

export function isNavActive(pathname: string, item: NavItem): boolean {
  if (item.path === "/app/today") {
    return pathname === "/app" || pathname === "/app/today";
  }
  return pathname === item.path || pathname.startsWith(`${item.path}/`);
}

/** 目前路徑是否落在「更多」裡的某頁 */
export function isMoreNavActive(pathname: string): boolean {
  return navItemsByKeys(mobileDockMoreKeys).some((item) => isNavActive(pathname, item));
}

/** 通知點擊後的落地路徑 */
export function pathForNotification(n: {
  kind: string;
  ref_type: string;
  ref_id?: string;
}): string {
  if (n.ref_type === "job" && n.ref_id) return `/app/jobs/${n.ref_id}`;
  if (n.ref_type === "outbox" && n.ref_id) return `/app/outbox/${n.ref_id}`;
  if (n.kind === "outbox" || n.ref_type === "outbox") return "/app/outbox";
  // 任務／系統／無 ref：任務中心
  return "/app/jobs";
}
