import { create } from 'zustand';

export interface UserData {
    id: number;
    email: string;
    login: string;
    phone: string;
    name: string;
    surname: string;
    is_active: boolean;
    role_id: number;
}

interface AuthState {
    isAuthenticated: boolean;
    token: string | null;
    login: (token: string) => void;
    logout: () => void;
    initializeAuth: () => void;
    userData: UserData | {};
    getUserData: () => UserData;
    setUserData: (userData: string) => void;
}

export const useAuthStore = create<AuthState>((set) => ({
    isAuthenticated: false,
    token: null,
    login: (token: string) => {
        localStorage.setItem('authToken', token);
        set({ isAuthenticated: true, token });
    },
    logout: () => {
        localStorage.removeItem('authToken');
        set({ isAuthenticated: false, token: null });
    },
    initializeAuth: () => {
        const token = localStorage.getItem('authToken');
        if (token) {
            set({ isAuthenticated: true, token });
        }
    },
    userData: {},
    setUserData: (userData: string) => {
        localStorage.setItem('userData', userData)
    },
    getUserData: () => {
        const userData = localStorage.getItem('userData')
        return JSON.parse(userData || "")
    }
}));