"use client";

import { createContext, ReactNode, useContext } from "react";

import { IUser } from "@/entities/user/types";

interface UserContextType {
  user: IUser | null;
}

const UserContext = createContext<UserContextType | undefined>(undefined);

interface UserProviderProps {
  user: IUser | null;
  children: ReactNode;
}

export function UserProvider({ user, children }: UserProviderProps) {
  return (
    <UserContext.Provider value={{ user }}>{children}</UserContext.Provider>
  );
}

export function useUser() {
  const context = useContext(UserContext);
  if (context === undefined) {
    throw new Error("useUser must be used within a UserProvider");
  }
  return context;
}
