import { create } from 'zustand';

interface User {
  username: string;
  email: string;
}

interface PublicAuthState {
  user: User | null;
  setUser: (user: User | null) => void;
  logout: () => void;
}

export const usePublicAuthStore = create<PublicAuthState>((set) => ({
  // Inicializamos con lo que haya en localStorage
  user: localStorage.getItem('public_user')
    ? JSON.parse(localStorage.getItem('public_user')!)
    : null,

  setUser: (user) => set({ user }),

  logout: () => {
    localStorage.removeItem('public_token');
    localStorage.removeItem('public_user');
    set({ user: null });
  }
}));