import { createRoot } from 'react-dom/client'
import { LogtoProvider, type LogtoConfig } from '@logto/react';
import './index.css'
import App from './App.tsx'

const LOGTO_URL = import.meta.env.VITE_LOGTO_URL || 'https://logto-admin-bzlued1boxl3t8ewsyn99an9.187.77.227.172.sslip.io';
const APP_ID = import.meta.env.VITE_LOGTO_APP_ID || 'gpi-app-final';

const config: LogtoConfig = {
  endpoint: LOGTO_URL,
  appId: APP_ID,
  scopes: ['openid', 'profile', 'email'],
};

// Mantenha estas para compatibilidade temporária se necessário
export function getToken() {
  return sessionStorage.getItem('logto_token');
}

export function getUser() {
  const user = sessionStorage.getItem('logto_user');
  return user ? JSON.parse(user) : null;
}

export function setUser(token: string, user: any) {
  sessionStorage.setItem('logto_token', token);
  sessionStorage.setItem('logto_user', JSON.stringify(user));
}

createRoot(document.getElementById('root')!).render(
  <LogtoProvider config={config}>
    <App />
  </LogtoProvider>
)
