'use client'
// src/app/client-layout.tsx — 客户端布局（登录页不显示 Sidebar/TopBar）
import { usePathname } from 'next/navigation'
import { useEffect, useState } from 'react'
import Sidebar from '@/components/Sidebar'
import ThemeProvider from '@/components/ThemeProvider'
import TopBar from '@/components/TopBar'

interface User { username: string; display_name: string; role: string }

export default function ClientLayout({ children }: { children: React.ReactNode }) {
  const pathname = usePathname()
  const isLoginPage = pathname === '/login'
  const [user, setUser] = useState<User | null>(null)

  useEffect(() => {
    if (isLoginPage) return
    fetch('/api/auth/me')
      .then(r => r.json())
      .then(d => { if (d.user) setUser(d.user) })
      .catch(() => {})
  }, [isLoginPage])

  if (isLoginPage) {
    return <ThemeProvider>{children}</ThemeProvider>
  }

  return (
    <ThemeProvider>
      <div className="min-h-screen bg-slate-50 dark:bg-slate-950">
        <Sidebar userRole={user?.role} />
        <TopBar user={user} />
        <main className="ml-60 pt-14 min-h-screen">
          <div className="p-6">{children}</div>
        </main>
      </div>
    </ThemeProvider>
  )
}
