export const getLogDates = (name: string): Promise<string[]> =>
  fetch(`/api/apps/${name}/logs/dates`).then(r => r.json())

export const getLogsForDate = (name: string, date: string): Promise<string[]> =>
  fetch(`/api/apps/${name}/logs?date=${date}`).then(r => r.json())

export const getSystemInfo = (): Promise<{ version: string, sha: string, uptime: number }> =>
  fetch('/api/system/info').then(r => r.json())

export const shareApp = (name: string) =>
  fetch(`/api/apps/${name}/tunnel`, { method: 'POST' })

export const unshareApp = (name: string) =>
  fetch(`/api/apps/${name}/tunnel`, { method: 'DELETE' })

export const applyUpdate = () =>
  fetch('/api/system/update', { method: 'POST' }).then(r => { if (!r.ok) throw new Error('update failed'); return r.json() })

export const checkForUpdate = (): Promise<{ available: boolean, current: string, latest: string, commits: string[] }> =>
  fetch('/api/system/update').then(r => { if (!r.ok) throw new Error('check failed'); return r.json() })

export const restartApp = (name: string) => fetch(`/api/apps/${name}/restart`, { method: 'POST' })

export const restartServer = () =>
  fetch('/api/system/restart', { method: 'POST' }).then(r => { if (!r.ok) throw new Error('restart failed'); return r.json() })

export const startApp = (name: string) => fetch(`/api/apps/${name}/start`, { method: 'POST' })

export const stopApp = (name: string) => fetch(`/api/apps/${name}/stop`, { method: 'POST' })
