import { defineStore } from 'pinia'

import { fetchVipInfo as apiFetchVipInfo, logout as apiLogout } from '../api/user'

export const useUserStore = defineStore('user', {
  state: () => ({
    vipInfo: null,
    loading: false,
  }),
  getters: {
    username: (state) => state.vipInfo?.username || '',
    isVip: (state) => Boolean(state.vipInfo?.is_vip),
    vipDaysLeft: (state) => Number(state.vipInfo?.days_left || 0),
    vipExpireTime: (state) => state.vipInfo?.expire_time || '',
  },
  actions: {
    async refreshVipInfo() {
      this.loading = true
      try {
        this.vipInfo = await apiFetchVipInfo()
      } finally {
        this.loading = false
      }
    },
    async logout() {
      try {
        await apiLogout()
      } catch {
        // ignore
      }
    },
  },
})

