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'

/**
 * Routing principale dell'applicazione PecFlow.
 *
 * Struttura:
 * - /login → LoginPage (pubblica)
 * - /* → AppLayout (richiede autenticazione)
 *   - /inbox → InboxPage
 *   - /sent → InboxPage (filtrata su outbound)
 *   - /messages/:id → MessageDetailPage
 *   - /compose → ComposePage
 *   - /mailboxes → MailboxesPage (admin)
 *   - /users → UsersPage (admin)
 *   - /permissions → PermissionsPage (admin)
 *   - / → redirect a /inbox
 */
export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        {/* Pagine pubbliche */}
        <Route path="/login" element={<LoginPage />} />

        {/* Pagine protette (dentro AppLayout) */}
        <Route element={<AppLayout />}>
          <Route path="/" element={<Navigate to="/inbox" replace />} />
          <Route path="/inbox" element={<InboxPage />} />
          <Route
            path="/sent"
            element={<InboxPage />}
          />
          <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 />} />

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