'use client'

import { AiUserBaseOutput } from '@/services/user'
import { createContext } from 'react'
import { useGetAIUserBaseInfo } from '@/hooks/aiUser'
import { useParams } from 'next/navigation'
import { useCurrentUser } from '@/hooks/auth'
import Empty from '@/components/ui/empty'

export * from './useAIUser'

const AIUserContext = createContext<{
  user: AiUserBaseOutput | undefined
  isOwner: boolean
  userId: number | undefined
}>({
  user: undefined,
  isOwner: false,
  userId: undefined,
})

export const AIUserProvider = ({ children }: { children: React.ReactNode }) => {
  const { userId } = useParams()

  const { data, error } = useGetAIUserBaseInfo({ aiId: Number(userId) })
  const { data: currentUser } = useCurrentUser()
  const isOwner = currentUser?.userId === data?.userId

  if (error) {
    return (
      <div className="flex h-full w-full items-center justify-center">
        <Empty title="Oops, there’s nothing here…" />
      </div>
    )
  }

  return (
    <AIUserContext.Provider value={{ user: data, isOwner, userId: Number(userId) }}>
      {children}
    </AIUserContext.Provider>
  )
}

export default AIUserContext
