import { Navigate, Route, Routes } from 'react-router-dom';
import { getToken } from './lib/api';
import AdminLayout from './layouts/AdminLayout';
import LoginPage from './pages/LoginPage';
import DashboardPage from './pages/DashboardPage';
import UsersPage from './pages/UsersPage';
import OrdersPage from './pages/OrdersPage';
import StoresPage from './pages/StoresPage';
import StoreAccountsPage from './pages/StoreAccountsPage';
import PartnersPage from './pages/PartnersPage';
import PartnerAccountsPage from './pages/PartnerAccountsPage';
import BenefitCouponsPage from './pages/BenefitCouponsPage';
import BenefitLedgersPage from './pages/BenefitLedgersPage';
import RedeemRecordsPage from './pages/RedeemRecordsPage';
import DeliveriesPage from './pages/DeliveriesPage';
import HqAccountsPage from './pages/HqAccountsPage';
import CitiesPage from './pages/CitiesPage';
import StoreMediaPage from './pages/StoreMediaPage';
import ProductsPage from './pages/ProductsPage';
import ResourcesPage from './pages/ResourcesPage';

function RequireAuth({ children }: { children: React.ReactNode }) {
  if (!getToken()) return <Navigate to="/login" replace />;
  return <>{children}</>;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route
        element={
          <RequireAuth>
            <AdminLayout />
          </RequireAuth>
        }
      >
        <Route path="/" element={<DashboardPage />} />
        <Route path="/users" element={<UsersPage />} />
        <Route path="/orders" element={<OrdersPage />} />
        <Route path="/products" element={<ProductsPage />} />
        <Route path="/stores" element={<StoresPage />} />
        <Route path="/store-accounts" element={<StoreAccountsPage />} />
        <Route path="/store-media" element={<StoreMediaPage />} />
        <Route path="/resources" element={<ResourcesPage />} />
        <Route path="/partners" element={<PartnersPage />} />
        <Route path="/cities" element={<CitiesPage />} />
        <Route path="/partner-accounts" element={<PartnerAccountsPage />} />
        <Route path="/benefit/coupons" element={<BenefitCouponsPage />} />
        <Route path="/benefit/ledgers" element={<BenefitLedgersPage />} />
        <Route path="/redeem-records" element={<RedeemRecordsPage />} />
        <Route path="/deliveries" element={<DeliveriesPage />} />
        <Route path="/hq-accounts" element={<HqAccountsPage />} />
      </Route>
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  );
}
