/**
 * Email Queue admin API — calls targo-hub /email-queue/* (proxies ERPNext).
 * Lets ops see + delete outbound emails sitting in ERPNext's queue.
 */
import { HUB_URL as HUB } from 'src/config/hub'

export async function listEmailQueue (status = '') {
  const qs = status ? `?status=${encodeURIComponent(status)}` : ''
  const r = await fetch(HUB + '/email-queue' + qs)
  if (!r.ok) throw new Error('Email queue API: ' + r.status)
  return r.json()
}

export async function deleteEmailQueueItem (name) {
  const r = await fetch(HUB + '/email-queue/' + encodeURIComponent(name), { method: 'DELETE' })
  if (!r.ok) throw new Error('Suppression échouée: ' + r.status)
  return r.json()
}

export async function purgeEmailQueue (status = 'Not Sent') {
  const r = await fetch(HUB + '/email-queue/purge', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ status }),
  })
  if (!r.ok) throw new Error('Purge échouée: ' + r.status)
  return r.json()
}
