import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { AppLayout } from '@/components/Layout/AppLayout'
import { LoginPage } from '@/pages/Login/LoginPage'
import { InboxPage } from '@/pages/Inbox/InboxPage'
import { MessageDetailPage } from '@/pages/MessageDetail/MessageDetailPage'
import { ComposePage } from '@/pages/Compose/ComposePage'
import { MailboxesPage } from '@/pages/Mailboxes/MailboxesPage'
import { UsersPage } from '@/pages/Users/UsersPage'
import { PermissionsPage } from '@/pages/Permissions/PermissionsPage'
import { SettingsPage } from '@/pages/Settings/SettingsPage'
import { VirtualBoxesPage } from '@/pages/VirtualBoxes/VirtualBoxesPage'
import { NotificationsPage } from '@/pages/Notifications/NotificationsPage'

/**
 * Routing principale dell'applicazione PecFlow.
 *
 * Struttura:
 * - /login                              → LoginPage (pubblica)
 * - /* → AppLayout (richiede autenticazione)
 *   - /inbox                            → Posta in arrivo (tutte le caselle)
 *   - /sent                             → Posta inviata (tutte le caselle)
 *   - /starred                          → Preferiti (tutte le caselle)
 *   - /archived                         → Archiviati (tutte le caselle)
 *   - /mailbox/:mailboxId/inbox         → Posta in arrivo di una specifica casella
 *   - /mailbox/:mailboxId/sent          → Posta inviata di una specifica casella
 *   - /mailbox/:mailboxId/starred       → Preferiti di una specifica casella
 *   - /mailbox/:mailboxId/archived      → Archiviati di una specifica casella
 *   - /messages/:id                     → Dettaglio messaggio
 *   - /compose                          → Nuova PEC
 *   - /mailboxes                        → Gestione caselle (admin)
 *   - /users                            → Gestione utenti (admin)
 *   - /permissions                      → Gestione permessi (admin)
 */
export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        {/* Pagina pubblica */}
        <Route path="/login" element={<LoginPage />} />

        {/* Pagine protette (dentro AppLayout) */}
        <Route element={<AppLayout />}>
          <Route path="/" element={<Navigate to="/inbox" replace />} />

          {/* Vista globale: tutte le caselle insieme */}
          <Route path="/inbox"    element={<InboxPage viewMode="inbox" />} />
          <Route path="/sent"     element={<InboxPage viewMode="sent" />} />
          <Route path="/starred"  element={<InboxPage viewMode="starred" />} />
          <Route path="/archived" element={<InboxPage viewMode="archived" />} />

          {/* Vista per singola casella PEC */}
          <Route path="/mailbox/:mailboxId/inbox"    element={<InboxPage viewMode="inbox" />} />
          <Route path="/mailbox/:mailboxId/sent"     element={<InboxPage viewMode="sent" />} />
          <Route path="/mailbox/:mailboxId/starred"  element={<InboxPage viewMode="starred" />} />
          <Route path="/mailbox/:mailboxId/archived" element={<InboxPage viewMode="archived" />} />

          {/* Vista per Virtual Box assegnata */}
          <Route path="/virtual-box/:vboxId/inbox"    element={<InboxPage viewMode="inbox" />} />
          <Route path="/virtual-box/:vboxId/sent"     element={<InboxPage viewMode="sent" />} />
          <Route path="/virtual-box/:vboxId/starred"  element={<InboxPage viewMode="starred" />} />
          <Route path="/virtual-box/:vboxId/archived" element={<InboxPage viewMode="archived" />} />

          <Route path="/messages/:id" element={<MessageDetailPage />} />
          <Route path="/compose" element={<ComposePage />} />

          {/* Pagine admin */}
          <Route path="/mailboxes"      element={<MailboxesPage />} />
          <Route path="/users"          element={<UsersPage />} />
          <Route path="/permissions"    element={<PermissionsPage />} />
          <Route path="/virtual-boxes"  element={<VirtualBoxesPage />} />
          <Route path="/notifications"  element={<NotificationsPage />} />

          {/* Profilo utente */}
          <Route path="/settings" element={<SettingsPage />} />

          {/* Fallback */}
          <Route path="*" element={<Navigate to="/inbox" replace />} />
        </Route>
      </Routes>
    </BrowserRouter>
  )
}
