import { createContext, useContext } from 'react';
import type { ReactNode } from 'react';
import { useToast } from '../hooks/useToast';
import { useNotificationContext } from './NotificationContext';
import type { ToastMessage } from '../components/Toast/Toast';

interface ToastContextType {
  toasts: ToastMessage[];
  addToast: (message: string, type?: ToastMessage['type'], duration?: number, jobId?: string, jobTitle?: string) => string;
  removeToast: (id: string) => void;
  success: (message: string, duration?: number, jobId?: string, jobTitle?: string) => string;
  error: (message: string, duration?: number, jobId?: string, jobTitle?: string) => string;
  warning: (message: string, duration?: number, jobId?: string, jobTitle?: string) => string;
  info: (message: string, duration?: number, jobId?: string, jobTitle?: string) => string;
  clearAll: () => void;
  // Toast-only methods (no persistent notifications)
  toastOnly: {
    success: (message: string, duration?: number) => string;
    error: (message: string, duration?: number) => string;
    warning: (message: string, duration?: number) => string;
    info: (message: string, duration?: number) => string;
  };
}

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

export function ToastProvider({ children }: { children: ReactNode }) {
  const toastMethods = useToast();
  const notifications = useNotificationContext();

  // Enhanced methods that also add to notifications
  const enhancedMethods = {
    ...toastMethods,
    addToast: (message: string, type: ToastMessage['type'] = 'info', duration?: number, jobId?: string, jobTitle?: string) => {
      const toastId = toastMethods.addToast(message, type, duration);
      notifications.addNotification(message, type, jobId, jobTitle);
      return toastId;
    },
    success: (message: string, duration?: number, jobId?: string, jobTitle?: string) => {
      const toastId = toastMethods.success(message, duration);
      notifications.addNotification(message, 'success', jobId, jobTitle);
      return toastId;
    },
    error: (message: string, duration?: number, jobId?: string, jobTitle?: string) => {
      const toastId = toastMethods.error(message, duration);
      notifications.addNotification(message, 'error', jobId, jobTitle);
      return toastId;
    },
    warning: (message: string, duration?: number, jobId?: string, jobTitle?: string) => {
      const toastId = toastMethods.warning(message, duration);
      notifications.addNotification(message, 'warning', jobId, jobTitle);
      return toastId;
    },
    info: (message: string, duration?: number, jobId?: string, jobTitle?: string) => {
      const toastId = toastMethods.info(message, duration);
      notifications.addNotification(message, 'info', jobId, jobTitle);
      return toastId;
    },
    // Toast-only methods (no persistent notifications)
    toastOnly: {
      success: (message: string, duration?: number) => {
        return toastMethods.success(message, duration);
      },
      error: (message: string, duration?: number) => {
        return toastMethods.error(message, duration);
      },
      warning: (message: string, duration?: number) => {
        return toastMethods.warning(message, duration);
      },
      info: (message: string, duration?: number) => {
        return toastMethods.info(message, duration);
      },
    },
  };
  
  return (
    <ToastContext.Provider value={enhancedMethods}>
      {children}
    </ToastContext.Provider>
  );
}

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