// zustand/userStore.ts
import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'

type TempUser = {
  number?: string
  otp?: string
  password?: string
  repeatPassword?: string
  previousPassword?: string
  mustLogin?: boolean
  rememberMe?: boolean
}

type AuthTempStore = {
  user: TempUser | null
  update: (userData: TempUser) => void
}

export const useAuthTempStore = create<AuthTempStore>()(
  persist(
    set => ({
      user: null,
      mustLogin: false,
      update: userData => {
        set(state => {
          if (!userData) return state // no user to update
          return {
            user: {
              ...state.user,
              ...userData
            }
          }
        })
      }
    }),
    {
      name: 'auth-temp-storage', // Key in localStorage
      storage: createJSONStorage(() => sessionStorage)
    }
  )
)
