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'
import { MultiTenantPage } from '@/pages/MultiTenant/MultiTenantPage'
import { SearchPage } from '@/pages/Search/SearchPage'
import { ReportsPage } from '@/pages/Reports/ReportsPage'
import { AuditLogPage } from '@/pages/AuditLog/AuditLogPage'
import { TemplatesPage } from '@/pages/Templates/TemplatesPage'
import { RoutingRulesPage } from '@/pages/RoutingRules/RoutingRulesPage'
import { ContactsPage } from '@/pages/Contacts/ContactsPage'
import { DeadlinesPage } from '@/pages/Deadlines/DeadlinesPage'
import { SignaturesPage } from '@/pages/Signatures/SignaturesPage'

/**
 * Routing principale dell'applicazione PEChub.
 *
 * 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" />} />
          <Route path="/trash"                    element={<InboxPage viewMode="trash" />} />
          <Route path="/conservation-pending"     element={<InboxPage viewMode="conservation_pending" />} />
          <Route path="/conservation-archived"    element={<InboxPage viewMode="conservation_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" />} />
          <Route path="/mailbox/:mailboxId/trash"                     element={<InboxPage viewMode="trash" />} />
          <Route path="/mailbox/:mailboxId/conservation-pending"      element={<InboxPage viewMode="conservation_pending" />} />
          <Route path="/mailbox/:mailboxId/conservation-archived"     element={<InboxPage viewMode="conservation_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 />} />

          {/* Super Admin – Gestione Multi-Tenant */}
          <Route path="/multitenant" element={<MultiTenantPage />} />

          {/* Ricerca avanzata full-text */}
          <Route path="/search" element={<SearchPage />} />

          {/* Dashboard e Reportistica */}
          <Route path="/reports" element={<ReportsPage />} />

          {/* Audit Log */}
          <Route path="/audit-log" element={<AuditLogPage />} />

          {/* Nuove funzionalita' */}
          <Route path="/templates"      element={<TemplatesPage />} />
          <Route path="/routing-rules"  element={<RoutingRulesPage />} />
          <Route path="/contacts"       element={<ContactsPage />} />
          <Route path="/deadlines"      element={<DeadlinesPage />} />
          <Route path="/signatures"     element={<SignaturesPage />} />

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

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