import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/api/client'

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

  async function login(password: string) {
    const { data } = await api.post('/auth/login', { password })
    token.value = data.token
    localStorage.setItem('prism_token', data.token)
  }

  function logout() {
    token.value = ''
    localStorage.removeItem('prism_token')
  }

  const isLoggedIn = () => !!token.value

  return { token, login, logout, isLoggedIn }
})
