import { useState } from 'react';
import { pb } from './config/pocketbase';
import Login from './pages/Auth/Login';
import DashboardLayout from './layouts/DashboardLayout';
import TrustCenter from './pages/TrustCenter/TrustCenter';
import ServiceDesk from './pages/Support/ServiceDesk';
import DocumentVault from './pages/Vault/DocumentVault';
import AccountSettings from './pages/Account/AccountSettings';

function App() {
  // On initialise l'état avec la présence du token, mais on ne met plus 
  // d'écouteur automatique. C'est le composant Login qui gérera le flux.
  const [isAuthenticated, setIsAuthenticated] = useState(pb.authStore.isValid);
  const [activeView, setActiveView] = useState<'dashboard' | 'support' | 'vault' | 'account'>('dashboard');

  const handleLogout = () => {
    pb.authStore.clear(); // Détruit le jeton de sécurité
    setIsAuthenticated(false);
  };

  // Tant que l'utilisateur n'est pas totalement authentifié (MFA inclus), 
  // on le maintient dans le sas de sécurité.
  if (!isAuthenticated) {
    return <Login onLoginSuccess={() => setIsAuthenticated(true)} />;
  }

  const renderContent = () => {
    switch (activeView) {
      case 'dashboard': return <TrustCenter onNavigate={setActiveView} />;
      case 'support': return <ServiceDesk />;
      case 'vault': return <DocumentVault />;
      case 'account': return <AccountSettings />;
      default: return <TrustCenter onNavigate={setActiveView} />;
    }
  };

  return (
    <DashboardLayout 
      activeView={activeView} 
      onNavigate={setActiveView}
      onLogout={handleLogout}
    >
      {renderContent()}
    </DashboardLayout>
  );
}

export default App;