import { defineStore } from 'pinia'
import { storage, USER_KEY, SESSION_KEY } from '@/utils/storage'

export const useUserStore = defineStore('user', {
	state: () => ({
		user: storage.get(USER_KEY),
		sessionKey: storage.get(SESSION_KEY),
		isLoggedIn: !!storage.get(SESSION_KEY)
	}),

	actions: {
		setUser(user, sessionKey) {
			this.user = user
			this.sessionKey = sessionKey
			this.isLoggedIn = true
			storage.set(USER_KEY, user)
			storage.set(SESSION_KEY, sessionKey)
		},

		logout() {
			this.user = null
			this.sessionKey = null
			this.isLoggedIn = false
			storage.remove(USER_KEY)
			storage.remove(SESSION_KEY)
		}
	}
})
