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

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

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

    setUser(response);
  };

  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;
  };

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