import { createContext, useContext, ReactNode } from 'react';
import toast, { Toaster, ToastOptions } from 'react-hot-toast';

interface ToastContextType {
    success: (message: string, options?: ToastOptions) => void;
    error: (message: string, options?: ToastOptions) => void;
    warning: (message: string, options?: ToastOptions) => void;
    info: (message: string, options?: ToastOptions) => void;
    loading: (message: string, options?: ToastOptions) => string;
    dismiss: (id?: string) => void;
    promise: <T>(
        promise: Promise<T>,
        messages: {
            loading: string;
            success: string | ((data: T) => string);
            error: string | ((error: any) => string);
        },
        options?: ToastOptions
    ) => Promise<T>;
}

const ToastContext = createContext<ToastContextType | undefined>(undefined);

const defaultOptions: ToastOptions = {
    duration: 4000,
    position: 'top-center',
    style: {
        fontFamily: 'inherit',
        direction: 'rtl',
    },
};

export const ToastProvider = ({ children }: { children: ReactNode }) => {
    const success = (message: string, options?: ToastOptions) => {
        toast.success(message, { ...defaultOptions, ...options });
    };

    const error = (message: string, options?: ToastOptions) => {
        toast.error(message, { ...defaultOptions, ...options });
    };

    const warning = (message: string, options?: ToastOptions) => {
        toast(message, {
            ...defaultOptions,
            icon: '⚠️',
            style: {
                ...defaultOptions.style,
                backgroundColor: '#fef3c7',
                color: '#92400e',
            },
            ...options,
        });
    };

    const info = (message: string, options?: ToastOptions) => {
        toast(message, {
            ...defaultOptions,
            icon: 'ℹ️',
            style: {
                ...defaultOptions.style,
                backgroundColor: '#dbeafe',
                color: '#1e40af',
            },
            ...options,
        });
    };

    const loading = (message: string, options?: ToastOptions): string => {
        return toast.loading(message, { ...defaultOptions, ...options });
    };

    const dismiss = (id?: string) => {
        toast.dismiss(id);
    };

    const promiseToast = <T,>(
        promise: Promise<T>,
        messages: {
            loading: string;
            success: string | ((data: T) => string);
            error: string | ((error: any) => string);
        },
        options?: ToastOptions
    ): Promise<T> => {
        return toast.promise(promise, messages, { ...defaultOptions, ...options });
    };

    return (
        <ToastContext.Provider
            value={{
                success,
                error,
                warning,
                info,
                loading,
                dismiss,
                promise: promiseToast,
            }}
        >
            {children}
            <Toaster
                position="top-center"
                reverseOrder={false}
                gutter={8}
                containerStyle={{
                    direction: 'rtl',
                }}
                toastOptions={{
                    duration: 4000,
                    style: {
                        background: 'var(--toast-bg)',
                        color: 'var(--toast-color)',
                        fontFamily: 'inherit',
                        direction: 'rtl',
                    },
                    success: {
                        iconTheme: {
                            primary: '#10b981',
                            secondary: '#ffffff',
                        },
                    },
                    error: {
                        iconTheme: {
                            primary: '#ef4444',
                            secondary: '#ffffff',
                        },
                    },
                }}
            />
        </ToastContext.Provider>
    );
};

export const useToast = () => {
    const context = useContext(ToastContext);
    if (context === undefined) {
        throw new Error('useToast must be used within a ToastProvider');
    }
    return context;
}; 