import React, { createContext, useContext, ReactNode } from 'react';
import { useEmailWebSocket } from '@/hooks/useEmailWebSocket';
import { Socket } from 'socket.io-client';

interface EmailWebSocketContextValue {
    socket: Socket | null;
    isConnected: boolean;
}

const EmailWebSocketContext = createContext<EmailWebSocketContextValue | null>(null);

interface EmailWebSocketProviderProps {
    children: ReactNode;
    token: string;
    serverUrl?: string;
}

export const EmailWebSocketProvider: React.FC<EmailWebSocketProviderProps> = ({
    children,
    token,
    serverUrl,
}) => {
    const { socket, isConnected } = useEmailWebSocket({ token, serverUrl });

    return (
        <EmailWebSocketContext.Provider value={{ socket, isConnected }}>
            {children}
        </EmailWebSocketContext.Provider>
    );
};

// eslint-disable-next-line react-refresh/only-export-components
export const useEmailWebSocketContext = () => {
    const context = useContext(EmailWebSocketContext);
    if (!context) {
        throw new Error('useEmailWebSocketContext must be used within EmailWebSocketProvider');
    }
    return context;
}; 