import { create } from 'zustand'
import { newId } from '../lib/id'

/**
 * A tiny transient-notification store (audit UX6/UX9). The app had no way to tell
 * the user about a background failure — a swallowed `openPath` error, a file that
 * moved — so those actions silently did nothing. This backs a lightweight,
 * non-blocking toast stack rendered by `Toaster`; any store or component can raise
 * one via {@link toast}.
 */
export type ToastTone = 'info' | 'error' | 'success'

export interface Toast {
  id: string
  message: string
  tone: ToastTone
}

interface ToastState {
  toasts: Toast[]
  show: (message: string, tone?: ToastTone) => void
  dismiss: (id: string) => void
}

/** How long a toast lingers before auto-dismissing. */
const AUTO_DISMISS_MS = 5000

export const useToasts = create<ToastState>((set, get) => ({
  toasts: [],
  show: (message, tone = 'info') => {
    const id = newId('toast')
    set((s) => ({ toasts: [...s.toasts, { id, message, tone }] }))
    // Auto-dismiss so the stack self-clears; the user can also dismiss early.
    setTimeout(() => get().dismiss(id), AUTO_DISMISS_MS)
  },
  dismiss: (id) => set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) }))
}))

/**
 * Imperative shortcut for non-component callers (the Zustand stores), so a store
 * action can surface an error without wiring the hook through a component.
 */
export function toast(message: string, tone?: ToastTone): void {
  useToasts.getState().show(message, tone)
}
