import {User} from '@/lib/models'
import {createStore} from 'zustand/vanilla'
import {getProfile} from '@/actions/auth'

export type ProfileStore = ProfileState & ProfileActions

export type ProfileState = {
  profile: User | null
}

export type ProfileActions = {
  refreshProfile: () => Promise<void>
}

export const createProfileStore = () => {
  return createStore<ProfileStore>()(set => ({
    profile: null,
    refreshProfile: async () => {
      const result = await getProfile()
      set({profile: result.success ? result.data : null})
    },
  }))
}
