export type NavKey =
  | "today"
  | "crew"
  | "studio"
  | "scout"
  | "radar"
  | "crm"
  | "outbox"
  | "jobs"
  | "brands"
  | "policy"
  | "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: "Topics" },
  /** 進階：訂閱關鍵字／每日巡邏設定。開工在今日收件匣。 */
  { key: "radar", path: "/app/radar/watches", labelKey: "nav.radar", label: "巡邏", en: "Patrol" },
  { key: "crm", path: "/app/crm", labelKey: "nav.crm", label: "名單", en: "CRM" },
  { 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: "policy", path: "/app/policy", labelKey: "nav.policy", label: "商機政策", en: "Policy" },
  {
    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" },
];

/** 第一次工作導覽期間只留這條線需要的入口 */
export const firstRunNavKeys: NavKey[] = ["today", "crew"];

/** 手機底欄：今日清名單、名單、帳號，其餘進更多 */
export const mobileDockPrimaryKeys: NavKey[] = ["today", "crm", "crew"];

/** 手機底欄「更多」＝進階功能（舊網址都還在） */
export const mobileDockMoreKeys: NavKey[] = [
  "radar",
  "studio",
  "scout",
  "outbox",
  "jobs",
  "brands",
  "policy",
  "insights",
  "utm",
];

export type NavGroupKey = "workflow" | "publish" | "advanced";

export type NavGroup = {
  key: NavGroupKey;
  /** i18n key，例如 navGroup.workflow */
  labelKey: string;
  keys: NavKey[];
  /** 側欄預設收合：設定類的頁面設一次就好，不該每天佔著視線。 */
  advanced?: boolean;
};

/** 側欄：每天走的那條線放最上面，設定與報表收進進階 */
export const navGroups: NavGroup[] = [
  { key: "workflow", labelKey: "navGroup.workflow", keys: ["today", "crm", "crew"] },
  { key: "publish", labelKey: "navGroup.publish", keys: ["studio", "outbox"] },
  {
    key: "advanced",
    labelKey: "navGroup.advanced",
    advanced: true,
    keys: ["radar", "scout", "brands", "policy", "jobs", "insights", "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.key === "today") {
    return (
      pathname === "/app" ||
      pathname === "/app/today" ||
      pathname === "/app/radar" ||
      pathname === "/app/radar/today" ||
      pathname.startsWith("/app/radar/opportunities")
    );
  }
  if (item.key === "radar") {
    return pathname.startsWith("/app/radar/watches");
  }
  if (item.key === "crm") {
    return pathname === "/app/crm" || pathname.startsWith("/app/crm/");
  }
  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";
  if (n.ref_type === "contact" && n.ref_id) return `/app/crm?contact=${encodeURIComponent(n.ref_id)}`;
  if (n.ref_type === "contact") return "/app/crm/followups";
  if (n.ref_type === "followup" || n.ref_type === "follow_up") return "/app/crm/followups";
  if (n.ref_type === "opportunity" && n.ref_id) return "/app/today";
  if (n.ref_type === "sweep" || n.ref_type === "radar_watch") return "/app/radar/watches";
  if (n.ref_type === "radar") return "/app/today";
  // 任務／系統／無 ref：任務中心
  return "/app/jobs";
}
