import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAuthStore = defineStore('auth', () => {
  const token = ref(localStorage.getItem('token') || '')
  const username = ref(localStorage.getItem('username') || '')
  const isSuperuser = ref(localStorage.getItem('isSuperuser') === 'true')

  function setToken(newToken: string, newUsername: string, newIsSuperuser: boolean) {
    token.value = newToken
    username.value = newUsername
    isSuperuser.value = newIsSuperuser
    localStorage.setItem('token', newToken)
    localStorage.setItem('username', newUsername)
    localStorage.setItem('isSuperuser', String(newIsSuperuser))
  }

  function logout() {
    token.value = ''
    username.value = ''
    isSuperuser.value = false
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    localStorage.removeItem('isSuperuser')
  }

  return { token, username, isSuperuser, setToken, logout }
})

