interface SendAuthEmailInput {
  to: string
  subject: string
  text: string
  html: string
}

const EMAIL_WEBHOOK_URL = process.env.AUTH_EMAIL_WEBHOOK_URL?.trim() || ""

export const sendAuthEmail = async (input: SendAuthEmailInput) => {
  if (!EMAIL_WEBHOOK_URL) {
    console.info("[auth-email] Webhook no configurado. Contenido del correo:", {
      to: input.to,
      subject: input.subject,
      text: input.text
    })
    return
  }

  const response = await fetch(EMAIL_WEBHOOK_URL, {
    method: "POST",
    headers: {
      "content-type": "application/json"
    },
    body: JSON.stringify(input)
  })

  if (!response.ok) {
    const details = await response.text().catch(() => "")
    throw new Error(`No pude enviar correo de autenticacion (${response.status}). ${details}`)
  }
}
