import { create } from 'zustand';
import * as grService from './grService';

export const useGrStore = create((set, get) => ({
  envStatus: 'Não informado',
  userData: null,
  loading: false,
  error: null,

  // Inicializa o ambiente GR
  initEnvironment: async () => {
    set({ loading: true, error: null });
    try {
      const data = await grService.getGrEnvironment();
      // Adjust mapping to handle direct user object response
      set({ 
        envStatus: data.Status || 'Ativo',
        userData: data, // Map the whole response (which contains 'usuario')
        loading: false 
      });
    } catch (error) {
      set({ error: error.message, loading: false });
    }
  },

  // Helper para pegar info do usuário logado no contexto GR
  getGrUser: () => {
    const { userData } = get();
    if (!userData || !userData.usuario) return null;
    return userData.usuario;
  }
}));
