'use client'
import {Button} from '@/components/ui/button'
import {logout} from '@/actions/auth'
import {useProfileStore} from '@/components/providers/StoreProvider'
import {useRouter} from 'next/navigation'
import {toast} from 'sonner'

export type ProfileProps = {}

export default function Profile(props: ProfileProps) {
  const refreshProfile = useProfileStore(store => store.refreshProfile)
  const router = useRouter()
  const doLogout = async () => {
    try {
      const resp = await logout()
      if (resp.success) {
        await refreshProfile()
        router.push('/')
      }
    }
    catch (e) {
      toast.error('退出登录失败', {
        description: (e as Error).message,
      })
    }
  }

  return (
    <div className="flex gap-2 items-center">
      <Button theme={`error`} onClick={doLogout}>
        退出登录
      </Button>
    </div>
  )
}
