import { useState, useCallback } from 'react';
import { ToastMessage } from '../components/ui/Toast';

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

export interface UseToasts {
  toasts: ToastMessage[];
  addToast: (type: ToastType, title: string, message?: string) => void;
  removeToast: (id: string) => void;
}

const TOAST_LIFETIME_MS = 4000;

export function useToasts(): UseToasts {
  const [toasts, setToasts] = useState<ToastMessage[]>([]);

  const removeToast = useCallback((id: string) => {
    setToasts(prev => prev.filter(t => t.id !== id));
  }, []);

  const addToast = useCallback(
    (type: ToastType, title: string, message?: string) => {
      const id = `toast-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
      setToasts(prev => [...prev, { id, type, title, message }]);
      setTimeout(() => {
        setToasts(prev => prev.filter(t => t.id !== id));
      }, TOAST_LIFETIME_MS);
    },
    []
  );

  return { toasts, addToast, removeToast };
}
