export const useVendorsStore = defineStore('vendorsStore', () => {
  // State
  const vendors = ref([])
  const currentVendor = ref(null)
  const loading = ref(false)
  const error = ref(null)
  
  // Search & Filter State
  const searchConditions = ref({
    name: '',
    category: '',
    page: 1,
    size: 10
  })
  
  // Pagination State
  const pagination = ref({
    currentPage: 1,
    pageSize: 10,
    totalCount: 0,
    totalPages: 0
  })

  // Getters (직접 반환)
  const getVendors = computed(() => vendors.value)
  const getCurrentVendor = computed(() => currentVendor.value)
  const getLoading = computed(() => loading.value)
  const getError = computed(() => error.value)
  const getSearchConditions = computed(() => searchConditions.value)
  const getPagination = computed(() => pagination.value)

  // Actions
  function setLoading(state) {
    loading.value = state
  }

  function setError(errorMessage) {
    error.value = errorMessage
  }

  function clearError() {
    error.value = null
  }

  function setVendors(vendorList) {
    vendors.value = vendorList
  }

  function setCurrentVendor(vendor) {
    currentVendor.value = vendor
  }

  function updateSearchConditions(conditions) {
    searchConditions.value = { ...searchConditions.value, ...conditions }
  }

  function updatePagination(paginationData) {
    pagination.value = { ...pagination.value, ...paginationData }
  }

  function resetSearch() {
    searchConditions.value = {
      name: '',
      category: '',
      page: 1,
      size: 10
    }
    pagination.value = {
      currentPage: 1,
      pageSize: 10,
      totalCount: 0,
      totalPages: 0
    }
  }

  // Reset function
  function reset() {
    vendors.value = []
    currentVendor.value = null
    loading.value = false
    error.value = null
    searchConditions.value = {
      name: '',
      category: '',
      page: 1,
      size: 10
    }
    pagination.value = {
      currentPage: 1,
      pageSize: 10,
      totalCount: 0,
      totalPages: 0
    }
  }

  // API Actions
  async function searchVendors(conditions = {}) {
    setLoading(true)
    clearError()
    
    try {
      const searchParams = { ...searchConditions.value, ...conditions }
      updateSearchConditions(searchParams)
      
      const response = await useAxios().get('/vendors', {
        params: searchParams
      })
      
      if (response.data) {
        setVendors(response.data.vendors || [])
        updatePagination({
          currentPage: response.data.currentPage || 1,
          totalCount: response.data.totalCount || 0,
          totalPages: Math.ceil((response.data.totalCount || 0) / searchParams.size)
        })
      }
    } catch (err) {
      setError(err.message || '벤더사 검색 중 오류가 발생했습니다.')
      setVendors([])
    } finally {
      setLoading(false)
    }
  }

  async function getVendorById(id) {
    setLoading(true)
    clearError()
    
    try {
      const response = await useAxios().get(`/vendors/${id}`)
      
      if (response.data) {
        setCurrentVendor(response.data)
        return response.data
      }
    } catch (err) {
      setError(err.message || '벤더사 정보를 불러오는 중 오류가 발생했습니다.')
      setCurrentVendor(null)
    } finally {
      setLoading(false)
    }
  }

  return {
    // State
    vendors,
    currentVendor,
    loading,
    error,
    searchConditions,
    pagination,
    
    // Getters
    getVendors,
    getCurrentVendor,
    getLoading,
    getError,
    getSearchConditions,
    getPagination,
    
    // Actions
    setLoading,
    setError,
    clearError,
    setVendors,
    setCurrentVendor,
    updateSearchConditions,
    updatePagination,
    resetSearch,
    searchVendors,
    getVendorById,
    reset
  }
}, {
  persist: {
    storage: persistedState.sessionStorage,
    paths: ['searchConditions', 'pagination']
  }
})