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

const systemConfigGetter = createCachedGetter(async () => {
  const { data } = await api.get('/system/config')
  return data
}, 15_000)

export async function fetchSystemConfig(options = {}) {
  return systemConfigGetter.run(options)
}

export async function updateSystemConfig(payload) {
  const { data } = await api.post('/system/config', payload)
  systemConfigGetter.clear()
  return data
}

export async function executeScheduleNow() {
  const { data } = await api.post('/schedule/execute', {})
  return data
}

export async function fetchSocialLoginConfig() {
  const { data } = await api.get('/social-login/config')
  return data
}

export async function updateSocialLoginConfig(payload) {
  const { data } = await api.post('/social-login/config', payload || {})
  systemConfigGetter.clear()
  return data
}

export async function testSocialLoginConfig(payload) {
  const { data } = await api.post('/social-login/test', payload || {})
  return data
}
