"use client";

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

import { useChatsInitialization } from "@/hooks/chats/useChatsInitialization";

interface ChatsInitializationContextType {
  isInitializing: boolean;
  isInitialized: boolean;
  error: string | null;
}

const ChatsInitializationContext =
  createContext<ChatsInitializationContextType | null>(null);

export const useChatsInitializationContext = () => {
  const context = useContext(ChatsInitializationContext);
  if (!context) {
    throw new Error(
      "useChatsInitializationContext must be used within ChatsInitializationProvider"
    );
  }
  return context;
};

interface ChatsInitializationProviderProps {
  children: ReactNode;
}

export const ChatsInitializationProvider = ({
  children,
}: ChatsInitializationProviderProps) => {
  const { isInitializing, isInitialized, error } = useChatsInitialization();

  return (
    <ChatsInitializationContext.Provider
      value={{
        isInitializing,
        isInitialized,
        error,
      }}
    >
      {children}
    </ChatsInitializationContext.Provider>
  );
};
