/**
 * 全站「本頁說明」目錄：依路徑對應說明 id，內容在 i18n（help.page.<id>.*）。
 * 不進頁面正文，由頂欄說明抽屜顯示。
 */

export type PageHelpId =
  | "today"
  | "crew"
  | "studio"
  | "scout"
  | "radar_today"
  | "radar_watches"
  | "crm_board"
  | "crm_followups"
  | "crm_stats"
  | "outbox"
  | "jobs"
  | "brands"
  | "policy"
  | "playbooks"
  | "insights"
  | "benchmark"
  | "utm"
  | "settings"
  | "profile"
  | "invite"
  | "usage"
  | "usage_plans"
  | "usage_checkout"
  | "admin_users"
  | "generic";

export type PageHelpRelated = {
  path: string;
  /** i18n key for link label */
  labelKey: string;
};

/** 最長前綴優先；含查詢字串的路徑先去掉 search/hash。 */
const RULES: { prefix: string; id: PageHelpId }[] = [
  { prefix: "/app/radar/watches", id: "radar_watches" },
  { prefix: "/app/radar", id: "radar_today" },
  { prefix: "/app/crm/followups", id: "crm_followups" },
  { prefix: "/app/crm/stats", id: "crm_stats" },
  { prefix: "/app/crm", id: "crm_board" },
  { prefix: "/app/outbox", id: "outbox" },
  { prefix: "/app/jobs", id: "jobs" },
  { prefix: "/app/studio", id: "studio" },
  { prefix: "/app/scout", id: "scout" },
  { prefix: "/app/crew", id: "crew" },
  { prefix: "/app/brands", id: "brands" },
  { prefix: "/app/policy", id: "policy" },
  { prefix: "/app/playbooks", id: "playbooks" },
  { prefix: "/app/insights", id: "insights" },
  { prefix: "/app/benchmark", id: "benchmark" },
  { prefix: "/app/utm", id: "utm" },
  { prefix: "/app/settings", id: "settings" },
  { prefix: "/app/profile", id: "profile" },
  { prefix: "/app/invite", id: "invite" },
  { prefix: "/app/usage/plans", id: "usage_plans" },
  { prefix: "/app/usage/checkout", id: "usage_checkout" },
  { prefix: "/app/usage", id: "usage" },
  { prefix: "/app/users", id: "admin_users" },
  // 今日是簡化收件匣，進階巡邏頁（/app/radar/*）另有一份說明。
  { prefix: "/app/today", id: "today" },
  { prefix: "/app", id: "today" },
];

/** 每個說明可連到的相關頁（可選） */
export const PAGE_HELP_RELATED: Partial<Record<PageHelpId, PageHelpRelated[]>> = {
  today: [
    { path: "/app/radar/watches", labelKey: "nav.radar" },
    { path: "/app/crm", labelKey: "nav.crm" },
    { path: "/app/crew", labelKey: "nav.crew" },
  ],
  radar_today: [
    { path: "/app/radar/watches", labelKey: "radar.today.link.watches" },
    { path: "/app/crm", labelKey: "nav.crm" },
    { path: "/app/crew", labelKey: "nav.crew" },
  ],
  radar_watches: [
    { path: "/app/today", labelKey: "nav.today" },
    { path: "/app/brands", labelKey: "nav.brands" },
  ],
  crm_board: [
    { path: "/app/crm/followups", labelKey: "crm.board.link.followups" },
    { path: "/app/crm/stats", labelKey: "crm.board.link.stats" },
    { path: "/app/today", labelKey: "nav.today" },
  ],
  crm_followups: [
    { path: "/app/crm", labelKey: "crm.followups.link.board" },
    { path: "/app/today", labelKey: "nav.today" },
  ],
  crm_stats: [{ path: "/app/crm", labelKey: "crm.followups.link.board" }],
  scout: [
    { path: "/app/today", labelKey: "nav.today" },
    { path: "/app/brands", labelKey: "nav.brands" },
  ],
  brands: [
    { path: "/app/policy", labelKey: "nav.policy" },
    { path: "/app/radar/watches", labelKey: "nav.radar" },
    { path: "/app/scout", labelKey: "nav.scout" },
  ],
  policy: [
    { path: "/app/brands", labelKey: "nav.brands" },
    { path: "/app/radar/watches", labelKey: "nav.radar" },
  ],
  studio: [
    { path: "/app/outbox", labelKey: "nav.outbox" },
    { path: "/app/crew", labelKey: "nav.crew" },
  ],
  outbox: [
    { path: "/app/studio", labelKey: "nav.studio" },
    { path: "/app/jobs", labelKey: "nav.jobs" },
  ],
  crew: [{ path: "/app/settings", labelKey: "nav.settings" }],
  usage: [{ path: "/app/usage/plans", labelKey: "nav.usage" }],
};

export function normalizeAppPath(pathname: string): string {
  const path = (pathname || "/").split("?")[0].split("#")[0];
  if (path.length > 1 && path.endsWith("/")) return path.slice(0, -1);
  return path || "/";
}

export function resolvePageHelpId(pathname: string): PageHelpId {
  // 呼叫端若誤傳含 ?query 的字串也先剝掉；正常 useLocation().pathname 不含 search。
  const path = normalizeAppPath(pathname);
  if (!path.startsWith("/app")) return "generic";
  for (const rule of RULES) {
    if (path === rule.prefix || path.startsWith(`${rule.prefix}/`)) {
      return rule.id;
    }
  }
  return "generic";
}

/** i18n keys for a help id */
export function pageHelpKeys(id: PageHelpId): {
  title: string;
  what: string;
  steps: string[];
  tips: string;
} {
  const base = `help.page.${id}`;
  return {
    title: `${base}.title`,
    what: `${base}.what`,
    steps: [`${base}.step1`, `${base}.step2`, `${base}.step3`],
    tips: `${base}.tips`,
  };
}
