import { createContext, useContext, useCallback } from 'react';
import type { ReactNode } from 'react';
import { message } from 'antd';

export type ToastType = 'success' | 'error' | 'info' | 'warning';

interface ToastContextType {
  showToast: (type: ToastType, msg: string, duration?: number) => void;
}

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

export function ToastProvider({ children }: { children: ReactNode }) {
  const showToast = useCallback((type: ToastType, msg: string, duration = 4) => {
    // 使用 Ant Design 的 message API
    message[type](msg, duration);
  }, []);

  return (
    <ToastContext.Provider value={{ showToast }}>{children}</ToastContext.Provider>
  );
}

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