import type { FormSubmitEvent } from '@nuxt/ui'

export const useUserStore = defineStore('User', () => {
  const name = ref('')

  async function signUp(payload: FormSubmitEvent<Schema>) {
    await authClient.signUp.email(
      {
        email: payload.data.email,
        password: payload.data.password,
        name: payload.data.name
      },
      {
        onRequest: (ctx) => {
          // TODO: Show loading spinner
          console.log('Sending register request')
        },
        onResponse: () => {
          // TODO: Hide loading spinner
          console.log('Receiving register response')
        },
        onSuccess: async (ctx) => {
          console.log('Successfully registered!')
          await navigateTo('/')
        },
        onError: (ctx) => {
          console.log(ctx.error.message)
          useToast().add({
            title: 'Fehler bei der Registrierung',
            description: ctx.error.message,
            color: 'success'
          })
        }
      }
    )
  }

  return { name, signUp }
})
