import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

interface User {
  id: number;
  name: string;
}

interface Role {
  id: number;
  name: string;
}

interface Permission {
  id: number;
  name: string;
}

interface UserWithRole extends User {
  role: Role | null;
  permissions: Permission[];
}

interface UserStore {
  user: UserWithRole | null;
  isLoading: boolean;
  error: string | null;
  setUser: (user: UserWithRole | null) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  clearUser: () => void;
}

export const useUserStore = create<UserStore>()(
  devtools(
    (set) => ({
      user: null,
      isLoading: false,
      error: null,
      setUser: (user) => set({ user, error: null }),
      setLoading: (loading) => set({ isLoading: loading }),
      setError: (error) => set({ error, user: null }),
      clearUser: () => set({ user: null, error: null, isLoading: false }),
    }),
    { name: 'user-store' }
  )
);