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

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

  const setUser = (userData) => {
    user.value = userData;
    isAdmin.value = userData?.user_type?.description === "admin";
    isOwner.value = userData?.user_type?.description === "owner";
  };

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

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

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