export type PageTitleSpec = {
  ns: string;
  key: string;
  params?: Record<string, string>;
};

const EXACT_ROUTES: Record<string, PageTitleSpec> = {
  "/admin": { ns: "dashboard", key: "title" },
  "/admin/players": { ns: "players", key: "title" },
  "/admin/draws": { ns: "draws", key: "statusListTitle" },
  "/admin/tickets": { ns: "tickets", key: "title" },
  "/admin/settlement-batches": { ns: "settlement", key: "batchList" },
  "/admin/reconcile": { ns: "reconcile", key: "title" },
  "/admin/reports": { ns: "reports", key: "title" },
  "/admin/reports/profit": { ns: "reports", key: "title" },
  "/admin/reports/wallet": { ns: "reports", key: "title" },
  "/admin/reports/risk": { ns: "reports", key: "title" },
  "/admin/reports/audit": { ns: "reports", key: "title" },
  "/admin/audit-logs": { ns: "audit", key: "title" },
  "/admin/admin-users": { ns: "adminUsers", key: "title" },
  "/admin/admin-roles": { ns: "adminRoles", key: "title" },
  "/admin/agents": { ns: "agents", key: "title" },
  "/admin/agents/provision": { ns: "agents", key: "subnav.provision" },
  "/admin/agents/sites": { ns: "config", key: "integrationSites.title" },
  "/admin/settlement-center": { ns: "settlementCenter", key: "title" },
  "/admin/agents/settlement-bills": { ns: "settlementCenter", key: "title" },
  "/admin/config/integration-sites": { ns: "config", key: "integrationSites.title" },
  "/admin/wallet": { ns: "wallet", key: "title" },
  "/admin/wallet/transactions": { ns: "wallet", key: "walletTransactions" },
  "/admin/wallet/transfer-orders": { ns: "wallet", key: "transferOrders" },
  "/admin/wallet/player": { ns: "wallet", key: "playerWalletQuery" },
  "/admin/risk": { ns: "risk", key: "center" },
  "/admin/settings": { ns: "common", key: "nav.settings" },
  "/admin/settings/currencies": { ns: "config", key: "currencies.title" },
  "/admin/currencies": { ns: "config", key: "currencies.title" },
  "/admin/config": { ns: "config", key: "hub.title" },
  "/admin/rules/plays": { ns: "config", key: "nav.rulesPlaysTitle" },
  "/admin/rules/odds": { ns: "config", key: "nav.rulesOddsTitle" },
  "/admin/jackpot": { ns: "jackpot", key: "configTitle" },
  "/admin/risk/cap": { ns: "config", key: "nav.riskCapTitle" },
  "/admin/login": { ns: "auth", key: "title" },
  "/admin/account": { ns: "common", key: "accountSettings" },
};

type RoutePattern = {
  test: (pathname: string) => boolean;
  resolve: (pathname: string) => PageTitleSpec;
};

const ROUTE_PATTERNS: RoutePattern[] = [
  {
    test: (p) => /^\/admin\/players\/\d+$/.test(p),
    resolve: () => ({ ns: "players", key: "detailTitle" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/finance$/.test(p),
    resolve: () => ({ ns: "draws", key: "subnav.finance" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/results$/.test(p),
    resolve: () => ({ ns: "draws", key: "subnav.results" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/review$/.test(p),
    resolve: () => ({ ns: "draws", key: "subnav.review" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/publish\/\d+$/.test(p),
    resolve: () => ({ ns: "draws", key: "publishTitle" }),
  },
  {
    test: (p) =>
      /^\/admin\/draws\/\d+\/risk\/pools$/.test(p)
      || /^\/admin\/risk\/draws\/\d+\/pools$/.test(p)
      || /^\/admin\/draws\/\d+\/risk\/hot$/.test(p)
      || /^\/admin\/risk\/draws\/\d+\/hot$/.test(p)
      || /^\/admin\/draws\/\d+\/risk\/sold-out$/.test(p)
      || /^\/admin\/risk\/draws\/\d+\/sold-out$/.test(p),
    resolve: () => ({ ns: "draws", key: "subnav.riskPools" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/risk\/pools\/[^/]+$/.test(p) || /^\/admin\/risk\/draws\/\d+\/pools\/[^/]+$/.test(p),
    resolve: (p) => {
      const number = decodeURIComponent(p.split("/").pop() ?? "");
      return { ns: "risk", key: "numberTitle", params: { number } };
    },
  },
  {
    test: (p) => /^\/admin\/draws\/\d+\/risk$/.test(p),
    resolve: () => ({ ns: "risk", key: "title" }),
  },
  {
    test: (p) => /^\/admin\/draws\/\d+$/.test(p),
    resolve: () => ({ ns: "draws", key: "drawDetail" }),
  },
  {
    test: (p) => /^\/admin\/settlement-batches\/\d+\/details$/.test(p),
    resolve: () => ({ ns: "settlement", key: "details" }),
  },
];

/** 根据路径解析浏览器标题用的 i18n 规格。 */
export function resolveAdminPageTitle(pathname: string): PageTitleSpec | null {
  if (EXACT_ROUTES[pathname]) {
    return EXACT_ROUTES[pathname];
  }

  for (const pattern of ROUTE_PATTERNS) {
    if (pattern.test(pathname)) {
      return pattern.resolve(pathname);
    }
  }

  return null;
}
