"use client";

/**
 * 根级 Client Providers 包装
 *
 * 这里只保留跨路由的 Character Catalog、Auth、User、消息预览和 viewport
 * Provider。Chat、Payment、PrivateZone 状态机由对应路由 layout 按需挂载。
 */

import type { ReactNode } from "react";

import { CharacterCatalogProvider } from "@/providers/character-catalog-provider";
import { SplashLatestMessageProvider } from "@/providers/splash-latest-message-provider";
import { ViewportCssVarsProvider } from "@/providers/viewport-css-vars-provider";
import { AppNavigationGuard } from "@/router/app-navigation-guard";
import { AuthProvider } from "@/stores/auth/auth-context";
import { AuthStatusChecker } from "@/stores/auth/auth-status-checker";
import { OAuthSessionSync } from "@/stores/auth/oauth-session-sync";
import { UserAuthSync } from "@/stores/sync/user-auth-sync";
import { UserProvider } from "@/stores/user/user-context";

export interface RootProvidersProps {
  children: ReactNode;
}

export function RootProviders({ children }: RootProvidersProps) {
  return (
    <CharacterCatalogProvider>
      <ViewportCssVarsProvider>
        <AuthProvider>
          <AuthStatusChecker />
          <OAuthSessionSync />
          <AppNavigationGuard />
          <SplashLatestMessageProvider>
            <UserProvider>
              <UserAuthSync />
              {children}
              <div id="toast-portal" />
            </UserProvider>
          </SplashLatestMessageProvider>
        </AuthProvider>
      </ViewportCssVarsProvider>
    </CharacterCatalogProvider>
  );
}
