import type { IUserInfoRes } from '@/api/types/login'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
    getUserInfo,
} from '@/api/login'

// 初始化状态
const userInfoState: IUserInfoRes = {
    userId: -1,
    username: '',
    nickname: '',
    avatar: '/static/images/default-avatar.png',
}

export const useUserStore = defineStore(
    'user',
    () => {
        // 定义用户信息
        const userInfo = ref<IUserInfoRes>({ ...userInfoState })
        // 设置用户信息
        const setUserInfo = (val: IUserInfoRes) => {
            console.log('设置用户信息', val)
            // 若头像为空 则使用默认头像
            if (!val.avatar) {
                val.avatar = userInfoState.avatar
            }
            userInfo.value = val
            // 直接更新本地存储（无论本地是否已有用户信息）
            const currentUser = uni.getStorageSync('user')
            const updatedUser = currentUser ? { ...currentUser, ...val } : val
            uni.setStorageSync('user', updatedUser)
            console.log('本地存储用户信息已更新', updatedUser)
        }
        const setUserAvatar = (avatar: string) => {
            userInfo.value.avatar = avatar
            console.log('设置用户头像', avatar)
            console.log('userInfo', userInfo.value)
            // 直接更新本地存储（无论本地是否已有用户信息）
            const currentUser = uni.getStorageSync('user')
            const updatedUser = currentUser ? { ...currentUser, avatar } : { ...userInfo.value }
            uni.setStorageSync('user', updatedUser)
            console.log('本地存储用户头像已更新', updatedUser)
        }
        // 删除用户信息
        const clearUserInfo = () => {
            userInfo.value = { ...userInfoState }
            uni.removeStorageSync('user')
        }

        /**
                                 * 获取用户信息
                                 */
        const fetchUserInfo = async () => {
            const res = JSON.parse(uni.getStorageSync('user'))
            return res
        }

        return {
            userInfo,
            clearUserInfo,
            fetchUserInfo,
            setUserInfo,
            setUserAvatar,
        }
    },
    {
        persist: true,
    },
)
