import { convertFileToBase64String } from "@/composables/utils";
import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
    state: () => ({
        userData: {
            email: "",
            name: "",
            surname: "",
            image: "",
        },
        laCoins: 123,
        introWatched: false,
    }),
    getters: {
        isLoggedIn: (state) => state.userData.email !== "",
    },
    actions: {
        setData(data: { name: string; surname: string; email: string; image: string }) {
            this.userData = data;
            this.saveData();
        },

        saveData() {
            localStorage.setItem("user", JSON.stringify(this.userData));
            localStorage.setItem("introWatched", JSON.stringify(this.introWatched));
        },

        loadData() {
            const userData = localStorage.getItem("user");
            if (userData) {
                this.userData = JSON.parse(userData);
            }

            const introWatched = localStorage.getItem("introWatched");
            if (introWatched) {
                this.introWatched = JSON.parse(introWatched);
            }
        },

        resetData() {
            this.setData({ email: "", name: "", surname: "", image: "" });
        },

        setFieldValue(field: "name" | "surname" | "image", value: string) {
            this.userData[field] = value;
            this.saveData();
        },
    },
});
