import {ReactNode} from 'react'
import Header from './_client/header'
import Navbar from './_client/navbar'
import Layout from './_client/layout'
import {getProfile} from '@/actions/auth'
import {redirect} from 'next/navigation'
import {ChangePasswordDialog} from '@/components/composites/dialogs/change-password-dialog'
import {RealnameAuthDialog} from '@/components/composites/dialogs/realname-auth-dialog'

export type AdminLayoutProps = {
  children: ReactNode
}

export default async function AdminLayout(props: AdminLayoutProps) {
  const resp = await getProfile()
  const profile = resp.success ? resp.data : null

  if (!profile) {
    redirect('/login')
  }

  return (
    <Layout
      navbar={<Navbar/>}
      header={<Header profile={profile}/>}
    >
      {props.children}
      <RealnameAuthDialog
        hasAuthenticated={!!profile.id_token}
        triggerClassName="hidden"
        defaultOpen={!profile.id_token}
      />
      <ChangePasswordDialog
        triggerClassName="hidden"
        defaultOpen={!profile.has_password}
      />
    </Layout>
  )
}
