import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '@/composables/useApi'
import { useToastStore } from '@/stores/toast'
import type { AutoDoorsConfigSummary, AutoDoorsConfigFull, AutoDoorsApplyResult } from '@/types'

export const useAutoDoorsStore = defineStore('autodoors', () => {
  const configs = ref<AutoDoorsConfigSummary[]>([])
  const currentConfig = ref<AutoDoorsConfigFull | null>(null)
  const isLoading = ref(false)
  const isSaving = ref(false)
  const isApplying = ref(false)
  const isDirty = ref(false)
  const api = useApi()
  const toast = useToastStore()

  async function fetchConfigs() {
    isLoading.value = true
    try {
      const res = await api.get<{ configs: AutoDoorsConfigSummary[] }>('/autodoors/configs')
      configs.value = res.configs
    } catch (err) {
      toast.error((err as Error).message)
    } finally {
      isLoading.value = false
    }
  }

  async function loadConfig(id: string) {
    isLoading.value = true
    try {
      const res = await api.get<{ config: AutoDoorsConfigFull }>(`/autodoors/configs/${id}`)
      currentConfig.value = res.config
      isDirty.value = false
    } catch (err) {
      toast.error((err as Error).message)
    } finally {
      isLoading.value = false
    }
  }

  async function createConfig(name: string, description?: string) {
    try {
      const res = await api.post<{ config: AutoDoorsConfigFull }>('/autodoors/configs', {
        config_name: name,
        description,
      })
      await fetchConfigs()
      currentConfig.value = res.config
      isDirty.value = false
      toast.success(`Config "${name}" created`)
      return res.config
    } catch (err) {
      toast.error((err as Error).message)
      return null
    }
  }

  async function saveCurrentConfig() {
    if (!currentConfig.value) return
    isSaving.value = true
    try {
      await api.put(`/autodoors/configs/${currentConfig.value.id}`, {
        config_name: currentConfig.value.config_name,
        description: currentConfig.value.description,
        config_data: currentConfig.value.config_data,
      })
      isDirty.value = false
      await fetchConfigs()
      toast.success('Config saved')
    } catch (err) {
      toast.error((err as Error).message)
    } finally {
      isSaving.value = false
    }
  }

  async function deleteConfig(id: string) {
    try {
      await api.del(`/autodoors/configs/${id}`)
      if (currentConfig.value?.id === id) {
        currentConfig.value = null
      }
      await fetchConfigs()
      toast.success('Config deleted')
    } catch (err) {
      toast.error((err as Error).message)
    }
  }

  async function applyToServer(id: string) {
    isApplying.value = true
    try {
      const res = await api.post<AutoDoorsApplyResult>(`/autodoors/configs/${id}/apply`)
      await fetchConfigs()
      toast.success(res.message)
      return res
    } catch (err) {
      toast.error((err as Error).message)
      return null
    } finally {
      isApplying.value = false
    }
  }

  async function importFromServer(configName: string) {
    isLoading.value = true
    try {
      const res = await api.post<{ config: AutoDoorsConfigFull }>('/autodoors/import-from-server', {
        config_name: configName,
      })
      await fetchConfigs()
      currentConfig.value = res.config
      isDirty.value = false
      toast.success(`Config imported from server as "${configName}"`)
      return res.config
    } catch (err) {
      toast.error((err as Error).message)
      return null
    } finally {
      isLoading.value = false
    }
  }

  function markDirty() {
    isDirty.value = true
  }

  return {
    configs,
    currentConfig,
    isLoading,
    isSaving,
    isApplying,
    isDirty,
    fetchConfigs,
    loadConfig,
    createConfig,
    saveCurrentConfig,
    deleteConfig,
    applyToServer,
    importFromServer,
    markDirty,
  }
})
