import { api } from './client'
import { createCachedGetter } from './cache'

const emailStatsGetter = createCachedGetter(async () => {
  const { data } = await api.get('/email/stats')
  return data
}, 10_000)

export async function fetchEmailSettings() {
  const { data } = await api.get('/email/settings')
  return data
}

export async function updateEmailSettings(payload) {
  const { data } = await api.post('/email/settings', payload)
  emailStatsGetter.clear()
  return data
}

export async function fetchEmailStats(options = {}) {
  return emailStatsGetter.run(options)
}

export async function fetchEmailLogs(params) {
  const { data } = await api.get('/email/logs', { params })
  return data
}

export async function cleanupEmailLogs(days) {
  const { data } = await api.post('/email/logs/cleanup', { days })
  emailStatsGetter.clear()
  return data
}
