import { Routes, Route, Navigate } from 'react-router-dom'
import AuthGate from './components/AuthGate'
import Layout from './components/Layout'

// Re-export so existing components can keep `import { useAuth } from '../App'`
export { useAuth } from './components/AuthGate'

// Pages
import Settings from './pages/Settings'
import NewbookData from './pages/NewbookData'
import ResosData from './pages/ResosData'
import ResidentsTableChart from './pages/ResidentsTableChart'
import BookingsStats from './pages/BookingsStats'
import WastageLogbook from './pages/WastageLogbook'
import UploadApp from './pages/UploadApp'

// Components used as full pages
import Dashboard from './components/Dashboard'
import Upload from './components/Upload'
import InvoiceList from './components/InvoiceList'
import Review from './components/Review'
import Disputes from './components/Disputes'
import Purchases from './components/Purchases'
import Budget from './components/Budget'
import GPReport from './components/GPReport'
import PurchasesReport from './components/PurchasesReport'
import AllowancesReport from './components/AllowancesReport'
import SalesGPReport from './components/SalesGPReport'
import ReconcilePurchases from './components/ReconcilePurchases'
import UsageVarianceReport from './components/UsageVarianceReport'
import SearchInvoices from './components/SearchInvoices'
import SearchLineItems from './components/SearchLineItems'
import PurchaseOrderList from './components/PurchaseOrderList'
import Ingredients from './components/Ingredients'
import RecipeList from './components/RecipeList'
import RecipeEditor from './components/RecipeEditor'
import EventOrders from './components/EventOrders'
import EventOrderEditor from './components/EventOrderEditor'
import DishList from './components/DishList'
import DishEditor from './components/DishEditor'
import MenuList from './components/MenuList'
import MenuEditor from './components/MenuEditor'
import BulkAllergens from './components/BulkAllergens'
import PriceImpact from './components/PriceImpact'

export default function App() {
  return (
    <AuthGate>
      <Routes>
        {/* Fullscreen PWA routes — no sidebar layout */}
        <Route path="/upload-app" element={<UploadApp />} />

        {/* All other routes get the sidebar Layout via Outlet */}
        <Route element={<Layout />}>
          <Route path="/" element={<Navigate to="/dashboard" replace />} />
          <Route path="/dashboard" element={<Dashboard />} />

          {/* Invoices */}
          <Route path="/upload" element={<Upload />} />
          <Route path="/invoices" element={<InvoiceList />} />
          <Route path="/invoice/:id" element={<Review />} />
          <Route path="/disputes" element={<Disputes />} />
          <Route path="/search/invoices" element={<SearchInvoices />} />
          <Route path="/search/line-items" element={<SearchLineItems />} />
          <Route path="/purchase-orders" element={<PurchaseOrderList />} />

          {/* Reports */}
          <Route path="/gp" element={<GPReport />} />
          <Route path="/purchases-report" element={<PurchasesReport />} />
          <Route path="/purchases" element={<Purchases />} />
          <Route path="/purchases/reconcile" element={<ReconcilePurchases />} />
          <Route path="/allowances-report" element={<AllowancesReport />} />
          <Route path="/sales-gp" element={<SalesGPReport />} />
          <Route path="/usage-variance" element={<UsageVarianceReport />} />

          {/* Kitchen */}
          <Route path="/logbook" element={<WastageLogbook />} />
          <Route path="/budget" element={<Budget />} />
          <Route path="/event-orders" element={<EventOrders />} />
          <Route path="/event-orders/:id" element={<EventOrderEditor />} />

          {/* Bookings */}
          <Route path="/resos" element={<ResosData />} />
          <Route path="/resos-stats" element={<BookingsStats />} />
          <Route path="/residents-table-chart" element={<ResidentsTableChart />} />
          <Route path="/newbook" element={<NewbookData />} />

          {/* Recipes */}
          <Route path="/ingredients" element={<Ingredients />} />
          <Route path="/recipes" element={<RecipeList />} />
          <Route path="/recipes/:id" element={<RecipeEditor />} />
          <Route path="/dishes" element={<DishList />} />
          <Route path="/dishes/:id" element={<DishEditor />} />
          <Route path="/menus" element={<MenuList />} />
          <Route path="/menus/:id" element={<MenuEditor />} />
          <Route path="/allergens" element={<BulkAllergens />} />
          <Route path="/price-impact" element={<PriceImpact />} />

          {/* Settings */}
          <Route path="/settings" element={<Settings />} />

          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Route>
      </Routes>
    </AuthGate>
  )
}
