import { BrowserRouter, Routes, Route, Outlet } from 'react-router-dom'
import { AuthProvider } from './context/AuthContext'
import { ToastProvider } from './context/ToastContext'
import { AdminDomainGuard } from './components/AdminDomainGuard'
import { ProtectedRoute } from './components/ProtectedRoute'
import { GuestRoute } from './components/GuestRoute'
import { PageLayout } from './components/PageLayout'
import { RouteErrorBoundary } from './components/RouteErrorBoundary'
import { HomePage } from './pages/HomePage'
import { BusinessesPage } from './pages/BusinessesPage'
import { BusinessInvoicesPage } from './pages/BusinessInvoicesPage'
import { UsersPage } from './pages/UsersPage'
import { WebsitesPage } from './pages/WebsitesPage'
import { SettingsPage } from './pages/SettingsPage'
import { InvoiceTemplateEditorPage } from './pages/InvoiceTemplateEditorPage'
import { IssueInvoicePage } from './pages/IssueInvoicePage'
import { PublicInvoicePage } from './pages/PublicInvoicePage'
import { ProfilePage } from './pages/ProfilePage'
import { LoginPage } from './pages/LoginPage'

function ProtectedPages() {
  return (
    <RouteErrorBoundary>
      <Outlet />
    </RouteErrorBoundary>
  )
}

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <ToastProvider>
          <Routes>
            {/* Host-agnostic: can be served from meshkee.com or other public domains */}
            <Route path="invoices/:invoiceId" element={<PublicInvoicePage />} />

            <Route element={<AdminDomainGuard />}>
              <Route element={<GuestRoute />}>
                <Route path="login" element={<LoginPage />} />
              </Route>

              <Route element={<ProtectedRoute />}>
                <Route element={<PageLayout />}>
                  <Route element={<ProtectedPages />}>
                    <Route index element={<HomePage />} />
                    <Route path="businesses" element={<BusinessesPage />} />
                    <Route
                      path="businesses/:businessId/invoices"
                      element={<BusinessInvoicesPage />}
                    />
                    <Route
                      path="businesses/:businessId/invoices/new"
                      element={<IssueInvoicePage />}
                    />
                    <Route
                      path="businesses/:businessId/invoices/:invoiceId/edit"
                      element={<IssueInvoicePage />}
                    />
                    <Route path="users" element={<UsersPage />} />
                    <Route path="websites" element={<WebsitesPage />} />
                    <Route path="settings" element={<SettingsPage />} />
                    <Route
                      path="settings/invoice-templates/new"
                      element={<InvoiceTemplateEditorPage />}
                    />
                    <Route
                      path="settings/invoice-templates/:templateId"
                      element={<InvoiceTemplateEditorPage />}
                    />
                    <Route path="profile" element={<ProfilePage />} />
                  </Route>
                </Route>
              </Route>
            </Route>
          </Routes>
        </ToastProvider>
      </AuthProvider>
    </BrowserRouter>
  )
}

export default App
