/**
 * Success / warning toast for a booked incoming invoice (Nuxt UI toaster, hosted by <UApp>).
 * Shared by the review screen ("book & next" stays in the review flow) and the inbox list
 * (return after "book" — see useInboxJustBooked in states.ts).
 */
export const useInboxBookedToast = () => {
  const toast = useToast()
  const { t } = useTranslation()
  const money = (n: any, cur?: string) =>
    new Intl.NumberFormat('de-DE', { style: 'currency', currency: cur || 'EUR' }).format(Number(n))

  const show = (jb: { cInvoiceId: number; documentNo?: string; vendorName?: string; grandTotal?: any; currency?: string; warning?: string }) => {
    if (!jb?.cInvoiceId) return
    const amount = (jb.grandTotal !== '' && jb.grandTotal != null) ? money(jb.grandTotal, jb.currency) : ''
    toast.add({
      title: (t.value?.inbox_booked_toast_title || 'Rechnung {no} verbucht').replace('{no}', jb.documentNo || ('#' + jb.cInvoiceId)),
      // a warning (not completed / booked net) replaces the vendor · amount line
      description: jb.warning || [jb.vendorName, amount].filter(Boolean).join(' · '),
      color: jb.warning ? 'warning' : 'success',
      icon: jb.warning ? 'i-mdi-alert-circle-outline' : 'i-mdi-check-circle-outline',
      duration: jb.warning ? 15000 : 7000,
      actions: [{
        label: t.value?.open_invoice || 'Rechnung öffnen', color: 'neutral', variant: 'outline',
        onClick: () => navigateTo(`/procurements/invoices/${jb.cInvoiceId}/edit`)
      }]
    })
  }
  return { show }
}
