/**
 * 类型化路由常量
 *
 * 集中管理应用中使用的路由路径，与 Flutter `lib/router/app_router.dart` 的
 * `AppRoutes` 一一对应。被服务端组件、客户端组件、`src/proxy.ts` 三端共用。
 *
 * 关键设计：
 *  - `as const` 让每个值都是字符串字面量类型，可用于 `PageProps<'/literal'>`。
 *  - 不导出 Client-only 模块（`use-auth-gate` 走独立路径导入），保证 Server bundle 不被污染。
 *  - 动态深链通过 `ROUTE_BUILDERS` 函数生成，避免手拼字符串。
 *
 * 注意：proxy 路由跳转逻辑（2026-06-15 迁出），但本文件的类型化常量仍在
 * 被 8 个组件（auth-screen / chat-header / DeepLinkPersist /
 * error / not-found / page / sidebar-screen / splash-screen）使用 —— 不能整体注释。
 */

/** 静态路由字面量 */
export const ROUTES = {
  root: "/",
  splash: "/splash",
  chat: "/chat",
  auth: "/auth",
  sidebar: "/sidebar",
  subscription: "/subscription",
  coinsRules: "/coins-rules",
} as const;

export type StaticRoute = (typeof ROUTES)[keyof typeof ROUTES];

/** 动态深链构造器 */
export const ROUTE_BUILDERS = {
  chatDeviceId: (deviceId: string): `/chat/deviceid/${string}` =>
    `/chat/deviceid/${encodeURIComponent(deviceId)}` as const,
  chatImage: (messageId: string): `/chat/image/${string}` =>
    `/chat/image/${encodeURIComponent(messageId)}` as const,
  subscription: (
    type: "vip" | "topup",
    options: { returnTo?: "chat" } = {},
  ): `/subscription?${string}` => {
    const params = new URLSearchParams({ type });
    if (options.returnTo) params.set("returnTo", options.returnTo);
    return `${ROUTES.subscription}?${params.toString()}` as const;
  },
  authWithRedirect: (redirectTo: string): `/auth?redirect=${string}` =>
    `${ROUTES.auth}?redirect=${encodeURIComponent(redirectTo)}` as const,
} as const;

/** 仅未登录态可见的路由（命中后已登录用户应跳走） */
export const AUTH_ONLY_ROUTES: readonly StaticRoute[] = [
  ROUTES.splash,
  ROUTES.auth,
] as const;

/** 仅登录态可见的路由（命中后未登录用户应跳走） */
export const PROTECTED_ROUTES: readonly StaticRoute[] = [
  ROUTES.chat,
  ROUTES.sidebar,
  ROUTES.subscription,
] as const;

/** 公开路由（游客态也允许访问） */
export const PUBLIC_ROUTES: readonly StaticRoute[] = [
  ROUTES.chat,
  ROUTES.sidebar,
  ROUTES.coinsRules,
] as const;

/** 所有静态路由，供 `proxy.ts` matcher 与未来 sitemap 使用 */
export const ALL_STATIC_ROUTES: readonly StaticRoute[] = [
  ROUTES.splash,
  ROUTES.chat,
  ROUTES.auth,
  ROUTES.sidebar,
  ROUTES.coinsRules,
] as const;

/** 联合路由类型，包含静态路由与已知动态路由 */
export type Route =
  | StaticRoute
  | `/chat/deviceid/${string}`
  | `/chat/image/${string}`;
