import './globals.css'
import {ReactNode} from 'react'
import {Metadata} from 'next'
import {Toaster} from '@/components/ui/sonner'
import Effects from '@/app/effects'
import {ProfileStoreProvider} from '@/components/stores/profile'
import {LayoutStoreProvider} from '@/components/stores/layout'
import {ClientStoreProvider} from '@/components/stores/client'
import {getProfile} from '@/actions/auth'

export async function generateMetadata(): Promise<Metadata> {
  return {
    title: '蓝狐代理',
  }
}

export default async function RootLayout(props: Readonly<{
  children: ReactNode
}>) {
  return (
    <html lang="zh-CN">
      <body>
        <StoreProviders>
          <Effects>{props.children}</Effects>
        </StoreProviders>
        <Toaster position="top-center" richColors expand/>
      </body>
    </html>
  )
}

function StoreProviders(props: {children: ReactNode}) {
  return (
    <ProfileStoreProvider profile={getProfile().then(resp => resp.success ? resp.data : null)}>
      <LayoutStoreProvider>
        <ClientStoreProvider>
          {props.children}
        </ClientStoreProvider>
      </LayoutStoreProvider>
    </ProfileStoreProvider>
  )
}
