import { defineStore } from 'pinia'
import type { ConfigDTO, TokenDTO, UserInfoDTO } from '@/api/types'
import { clearLoginSession, getCurrentUser, getToken, setCurrentUser, setLoginSession } from '@/utils/auth'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken(),
    currentUser: getCurrentUser(),
    config: undefined as ConfigDTO | undefined
  }),
  actions: {
    setSession(data: TokenDTO) {
      setLoginSession(data)
      this.token = data.token
      this.currentUser = data.currentUser.userInfo
    },
    setConfig(config: ConfigDTO) {
      this.config = config
    },
    setUser(user: UserInfoDTO) {
      setCurrentUser(user)
      this.currentUser = user
    },
    clearSession() {
      clearLoginSession()
      this.token = ''
      this.currentUser = {}
    }
  }
})

