import type { FC, ReactNode } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ToastProvider, Button } from './components/ui';
import ErrorBoundary from './components/ErrorBoundary';
import { AuthProvider, useAuth } from './lib/auth';
import { QuickLinksProvider } from './lib/quicklinks';
import { PatientDrawerProvider } from './components/PatientDrawer';
import ChatNotifications from './lib/chat-notifications';
import Dashboard from './pages/Dashboard';
import Agenda from './pages/Agenda';
import Pacientes from './pages/Pacientes';
import Pos from './pages/Pos';
import Visitas from './pages/Visitas';
import Consultas from './pages/Consultas';
import Servicios from './pages/Servicios';
import Paquetes from './pages/Paquetes';
import Medicos from './pages/Medicos';
import Monedero from './pages/Monedero';
import Pagos from './pages/Pagos';
import Ventas from './pages/Ventas';
import Tickets from './pages/Tickets';
import Cortes from './pages/Cortes';
import Reportes from './pages/Reportes';
import Configuracion from './pages/Configuracion';
import Cumpleanos from './pages/Cumpleanos';
import Inventario from './pages/Inventario';
import WacrmMessages from './pages/WacrmMessages';
import WacrmLeads from './pages/WacrmLeads';
import Chat from './pages/Chat';
import MisVentas from './pages/MisVentas';
import Login from './pages/Login';
import './App.css';
const FullScreenLoader: FC = () => (
);
// Pantalla cuando el servidor no responde al arrancar (red lenta o caída):
// en lugar de quedarse cargando para siempre, ofrece reintentar.
const BootErrorScreen: FC<{ onRetry: () => void }> = ({ onRetry }) => (
No se pudo conectar
El servidor no responde en este momento. Revisa tu conexión a internet e intenta de nuevo.
);
const RequireAuth: FC<{ children: ReactNode; href: string }> = ({ children, href }) => {
const { user, loading, bootError, retryBoot, canSee } = useAuth();
if (loading) return ;
if (bootError) return ;
if (!user) return ;
// La ruta "/" se maneja en HomeRoute (calcula landing por rol) para evitar
// el bucle de redirecciones cuando el rol no tiene '/' en la matriz.
if (!canSee(href) && href !== '/') return ;
return <>{children}>;
};
const LoginRoute: FC = () => {
const { user, loading } = useAuth();
if (loading) return ;
if (user) return ;
return ;
};
// Pantalla inicial por rol: el médico aterriza directo en su Agenda.
// Si el rol no tiene acceso al Dashboard (matriz por rol), aterriza en la
// primera sección visible; si no tiene ninguna, muestra "Sin acceso" en
// lugar de redirigir a "/" en bucle (pantalla en blanco).
const LANDING_CANDIDATOS = [
'/agenda', '/mis-ventas', '/pacientes', '/pos', '/visitas', '/consultas',
'/medicos', '/servicios', '/paquetes', '/ventas', '/pagos', '/monedero',
'/inventario', '/cortes', '/reportes', '/cumpleanos', '/wacrm/messages',
'/wacrm/leads', '/configuracion',
];
const HomeRoute: FC = () => {
const { user, canSee } = useAuth();
if (user?.role === 'medico') return ;
if (canSee('/')) return ;
for (const href of LANDING_CANDIDATOS) {
if (canSee(href)) return ;
}
return (
Sin acceso a módulos
Tu usuario no tiene secciones habilitadas. Contacta al administrador.