"use client";

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

import type { ReactNode } from "react";

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";
import { UserProvider } from "@/stores/user/user-context";

export interface RootProvidersProps {
  children: ReactNode;
}

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