import { defineStore } from "pinia";
import { getUser } from "src/api/user";
import { ref } from "vue";

export const userStore = defineStore("user", () => {
  const accessToken = ref(null);
  const isAdmin = ref(false);
  const user = ref(null);

  const fetchUser = async () => {
    const response = await getUser();
    setUser(response);
  };

  const resetUser = () => {
    user.value = null;
    isAdmin.value = false;
    accessToken.value = false;
  };

  const setUser = (userData) => {
    user.value = userData;
    isAdmin.value = String(userData.type).toUpperCase() === "ADMIN";
  };

  return {
    accessToken,
    isAdmin,
    user,
    fetchUser,
    resetUser,
    setUser,
  };
});
