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

// Mappa esplicita: Tailwind deve trovare le classi come stringhe letterali complete per includerle nel bundle
const toastAlertClass: Record<ToastType, string> = {
  success: 'alert-success',
  error:   'alert-error',
  warning: 'alert-warning',
  info:    'alert-info',
};

export function showToast(message: string, type: ToastType = 'info', duration = 3000): void {
  // container toast — uno solo nel DOM
  let container = document.getElementById('toast-container');
  if (!container) {
    container = document.createElement('div');
    container.id = 'toast-container';
    container.className = 'toast toast-top toast-center z-50';
    document.body.appendChild(container);
  }

  const alert = document.createElement('div');
  alert.className = `alert ${toastAlertClass[type]} shadow-lg transition-opacity duration-300`;
  alert.innerHTML = `<span>${message}</span>`;
  container.appendChild(alert);

  // rimozione con fade out
  setTimeout(() => {
    alert.classList.add('opacity-0');
    setTimeout(() => alert.remove(), 300);
  }, duration);
}