import { defineStore } from 'pinia'
import { ref } from 'vue'

import { useApi } from '../composables/useApi.js'

export const useBackupStore = defineStore('backup', () => {
  const api = useApi()

  const dbRuns = ref([])
  const imagesRuns = ref([])
  const settings = ref(null)
  const loading = ref({ dbRuns: false, imagesRuns: false, settings: false })
  const lastError = ref(null)

  async function loadRuns(kind) {
    const targetRef = kind === 'db' ? dbRuns : imagesRuns
    const loadingKey = kind === 'db' ? 'dbRuns' : 'imagesRuns'
    loading.value[loadingKey] = true
    lastError.value = null
    try {
      const body = await api.get('/api/system/backup/runs', {
        params: { kind, limit: 50 },
      })
      targetRef.value = body.runs || []
    } catch (e) {
      lastError.value = e.message
    } finally {
      loading.value[loadingKey] = false
    }
  }

  async function triggerBackup(kind, tag = null) {
    lastError.value = null
    try {
      await api.post(`/api/system/backup/${kind}`, {
        body: tag ? { tag } : {},
      })
    } catch (e) {
      lastError.value = e.message
      throw e
    }
  }

  async function restore(runId, confirm) {
    lastError.value = null
    try {
      await api.post(`/api/system/backup/runs/${runId}/restore`, {
        body: { confirm },
      })
    } catch (e) {
      lastError.value = e.message
      throw e
    }
  }

  async function deleteRun(runId, confirm) {
    lastError.value = null
    try {
      await api.delete(`/api/system/backup/runs/${runId}`, {
        body: { confirm },
      })
    } catch (e) {
      lastError.value = e.message
      throw e
    }
  }

  async function setTag(runId, tag) {
    lastError.value = null
    try {
      await api.patch(`/api/system/backup/runs/${runId}`, {
        body: { tag },
      })
    } catch (e) {
      lastError.value = e.message
      throw e
    }
  }

  async function loadSettings() {
    loading.value.settings = true
    lastError.value = null
    try {
      settings.value = await api.get('/api/system/backup/settings')
    } catch (e) {
      lastError.value = e.message
    } finally {
      loading.value.settings = false
    }
  }

  async function patchSettings(patch) {
    lastError.value = null
    try {
      settings.value = await api.patch('/api/system/backup/settings', {
        body: patch,
      })
    } catch (e) {
      lastError.value = e.message
      throw e
    }
  }

  return {
    dbRuns, imagesRuns, settings, loading, lastError,
    loadRuns, triggerBackup, restore, deleteRun, setTag,
    loadSettings, patchSettings,
  }
})
