import { useState, useEffect, useCallback } from 'react';

export type ToastVariant = 'success' | 'error';

export interface ToastState {
  message: string;
  variant: ToastVariant;
}

export function useToast(durationMs = 3500) {
  const [state, setState] = useState<ToastState | null>(null);

  useEffect(() => {
    if (!state) return;
    const id = setTimeout(() => setState(null), durationMs);
    return () => clearTimeout(id);
  }, [state, durationMs]);

  const showToast = useCallback((message: string, variant: ToastVariant = 'success') => {
    setState({ message, variant });
  }, []);

  return { toast: state, showToast };
}
