import { create } from 'zustand'
import { GetCurrentUser, SetUserName } from '../client/controllers'
import { UserResponse } from '../client/response'

export type UserActions = {
	sync: () => Promise<UserResponse | undefined>
	updateName: (name: string) => Promise<void>
}

export const useCurrentAuthenticatedUserStore = create<UserResponse & UserActions>((set) => ({
	Id: -1,
	Name: '',
	JobPosition: '',
	Active: false,
	Admin: false,
	sync: async () => {
		const authUser = await GetCurrentUser()
		set((state) => ({
			...state,
			...authUser
		}))
		return authUser 
	},
	updateName: async (name: string) => {
		await SetUserName(name)
	}
}))
