import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { AccountAuth } from '@/types/account'
import { postForm } from '@/utils/request'

export const useSecurityStore = defineStore('security', () => {
  const auth = ref<AccountAuth | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)

  async function fetchSecuritySetting() {
    loading.value = true
    error.value = null
    try {
      const data = await postForm<AccountAuth>('/uc/approve/security/setting', {})
      auth.value = data
    } catch (e) {
      error.value = e instanceof Error ? e.message : '获取安全设置失败'
      throw e
    } finally {
      loading.value = false
    }
  }

  function clear() {
    auth.value = null
    error.value = null
  }

  return {
    auth,
    loading,
    error,
    fetchSecuritySetting,
    clear,
  }
})
