import axiosInstance from '@/plugins/axios-plugin';
import { mountStoreDevtool } from 'simple-zustand-devtools';
import { toast } from 'sonner';
import { route } from 'ziggy-js';
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

const useUserStore = create(
    devtools((set, get) => ({
        // Pricing Plans
        user: null,
        user_usage: null,
        plan: {
            name: 'Free',
            tier: 'free',
        },
        billing: null,
        credits: null,

        isLoadingUser: false,

        isRedirectingToBilling: false,

        redirectBillingPortal: async () => {
            set({ isRedirectingToBilling: true });
            console.log('redirectBillingPortal');
            try {
                const response = await axiosInstance.post(route('api.user.billing_portal'));
                if (response?.data?.success?.data) {
                    window.location.href = response.data.success.data.redirect;
                }
            } catch (error) {
                console.error(route('api.user.billing_portal'));
                console.error('Error fetching:', error);
            } finally {
                set({ isRedirectingToBilling: false });
            }
        },

        // Fetch backgrounds
        fetchUser: async () => {
            set({ isLoadingUser: true });
            try {
                const response = await axiosInstance.post(route('api.user'));
                set({
                    user: response.data.success.data.user,
                    user_usage: response.data.success.data.user.user_usage,
                    plan: response.data.success.data.user.plan,
                    billing: response.data.success.data.billing,
                    credits: response.data.success.data.credits,
                });
                //return response.data.success.data;
            } catch (error) {
                console.error(route('api.user'));
                console.error('Error fetching:', error);
            } finally {
                set({ isLoadingUser: false });
            }
        },

        premiumExportRequest: async () => {
            try {
                const response = await axiosInstance.post(route('api.user.premium_export.request'));

                if (response?.data?.success?.data?.user_usage) {
                    set({
                        user_usage: response.data.success.data.user_usage,
                    });
                }

                if (response?.data?.success?.message) {
                    toast.success(response.data.success.message);
                }

                if (response?.data?.error?.message) {
                    toast.error(response.data.error.message);
                }
                return response.data;
            } catch (error) {
                console.error(route('api.user.premium_export.request'));
                console.error('Error fetching:', error);
            }
        },

        premiumExportComplete: async (exportToken) => {
            try {
                const response = await axiosInstance.post(route('api.user.premium_export.complete'), {
                    export_token: exportToken,
                });

                if (response?.data?.success?.data?.user_usage) {
                    set({
                        user_usage: response.data.success.data.user_usage,
                    });
                }

                if (response?.data?.success?.message) {
                    toast.success(response.data.success.message);
                }

                if (response?.data?.error?.message) {
                    toast.error(response.data.error.message);
                }

                return response.data;
            } catch (error) {
                console.error(route('api.user.premium_export.complete'));
                console.error('Error fetching:', error);
            }
        },
    })),
    {
        name: 'UserStore',
        store: 'UserStore',
    },
);

if (import.meta.env.APP_ENV === 'local') {
    mountStoreDevtool('UserStore', useUserStore);
}

export default useUserStore;
