import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Address } from './address'

export interface CustomerData {
  companyName: string
  companyEmail: string
  companyAddress: string
  regon: string
  nip: string
  vat: string
  billingAddressId: number | null
  companyAddressId: number | null
}

export const useCustomerStore = defineStore('customer', () => {
  const customer = ref<CustomerData | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)

  const hasAccount = computed(() => customer.value !== null)

  function setCustomer(data: CustomerData) {
    customer.value = data
  }

  function clearCustomer() {
    customer.value = null
  }

  function updateCustomer(data: Partial<CustomerData>) {
    if (customer.value) {
      customer.value = { ...customer.value, ...data }
    }
  }

  return {
    customer,
    loading,
    error,
    hasAccount,
    setCustomer,
    clearCustomer,
    updateCustomer
  }
})