import { redirect } from "next/navigation"
import { auth } from "@/lib/auth"
import { getAccountsAction } from "@/lib/actions/account"
import { AppSidebar } from "@/components/app-sidebar"
import { AppHeader } from "@/components/app-header"
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar"
import { AccountsView } from "./accounts-view"

export default async function AccountsPage() {
  const session = await auth()

  if (!session?.user) {
    redirect("/login")
  }

  const user = session.user
  const userName = user.name || "Fluxent 用户"
  const userEmail = user.email || ""
  const userAvatar = user.image || null

  const accountsRes = await getAccountsAction()
  const initialAccounts =
    accountsRes.success && accountsRes.data ? accountsRes.data : []

  return (
    <SidebarProvider>
      <AppSidebar
        user={{
          name: userName,
          email: userEmail,
          avatar: userAvatar,
        }}
      />
      <SidebarInset>
        <AppHeader title="资金账户" />

        <AccountsView
          key={JSON.stringify(initialAccounts)}
          initialAccounts={initialAccounts}
        />
      </SidebarInset>
    </SidebarProvider>
  )
}
