import { useState, useCallback } from 'react';

interface UserInfo {
  id: string;
  name: string;
  email: string;
  role: string;
}

interface GlobalState {
  user: UserInfo | null;
  loading: boolean;
  error: string | null;
  theme: 'light' | 'dark';
}

const useGlobalModel = () => {
  const [state, setState] = useState<GlobalState>({
    user: null,
    loading: false,
    error: null,
    theme: 'light',
  });

  const setUser = useCallback((user: UserInfo | null) => {
    setState(prev => ({ ...prev, user }));
  }, []);

  const setLoading = useCallback((loading: boolean) => {
    setState(prev => ({ ...prev, loading }));
  }, []);

  const setError = useCallback((error: string | null) => {
    setState(prev => ({ ...prev, error }));
  }, []);

  const toggleTheme = useCallback(() => {
    setState(prev => ({
      ...prev,
      theme: prev.theme === 'light' ? 'dark' : 'light',
    }));
  }, []);

  return {
    ...state,
    setUser,
    setLoading,
    setError,
    toggleTheme,
  };
};

export default useGlobalModel;