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 BenefitCouponsPage from './pages/BenefitCouponsPage';
import BenefitLedgersPage from './pages/BenefitLedgersPage';
import RedeemRecordsPage from './pages/RedeemRecordsPage';
import PendingRedeemPage from './pages/PendingRedeemPage';
import RedeemDebugPage from './pages/RedeemDebugPage';
import DeliveriesPage from './pages/DeliveriesPage';
import XiaofeixiaTestPage from './pages/XiaofeixiaTestPage';
import HqAccountsPage from './pages/HqAccountsPage';
import CitiesPage from './pages/CitiesPage';
import CityPartnersPage from './pages/CityPartnersPage';
import CityWarehousesPage from './pages/CityWarehousesPage';
import FulfillmentProvidersPage from './pages/FulfillmentProvidersPage';
import StoreMediaPage from './pages/StoreMediaPage';
import StoreCategoriesPage from './pages/StoreCategoriesPage';
import PromoCodesPage from './pages/PromoCodesPage';
import PromoCodeDetailLayout from './pages/promo/PromoCodeDetailLayout';
import PromoCodeDetailPage from './pages/promo/PromoCodeDetailPage';
import PromoCodeUsersPage from './pages/promo/PromoCodeUsersPage';
import ProductsPage from './pages/ProductsPage';
import ProductDetailTemplatesPage from './pages/ProductDetailTemplatesPage';
import ResourcesPage from './pages/ResourcesPage';
import StoreBillsPage from './pages/StoreBillsPage';
import PartnerBillsPage from './pages/PartnerBillsPage';
import WineryBillsPage from './pages/WineryBillsPage';
import TicketsPage from './pages/TicketsPage';
import InvoicesPage from './pages/InvoicesPage';
import UserLogsPage from './pages/UserLogsPage';
import HqLogsPage from './pages/HqLogsPage';
import ThirdPartyLogsPage from './pages/ThirdPartyLogsPage';
import StoreLogsPage from './pages/StoreLogsPage';
import PartnerLogsPage from './pages/PartnerLogsPage';
import WechatBindingsPage from './pages/WechatBindingsPage';
import HqPermissionsPage from './pages/HqPermissionsPage';
import SystemSettingsPage from './pages/SystemSettingsPage';

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="/wechat-bindings" element={<WechatBindingsPage />} />
        <Route path="/orders" element={<OrdersPage />} />
        <Route path="/promo-codes" element={<PromoCodesPage />} />
        <Route path="/promo-codes/:id" element={<PromoCodeDetailLayout />}>
          <Route index element={<PromoCodeDetailPage />} />
          <Route path="users" element={<PromoCodeUsersPage />} />
        </Route>
        <Route path="/products" element={<ProductsPage />} />
        <Route path="/product-detail-templates" element={<ProductDetailTemplatesPage />} />
        <Route path="/stores" element={<StoresPage />} />
        <Route path="/store-categories" element={<StoreCategoriesPage />} />
        <Route path="/store-accounts" element={<StoreAccountsPage />} />
        <Route path="/store-media" element={<StoreMediaPage />} />
        <Route path="/resources" element={<ResourcesPage />} />
        <Route path="/partners" element={<Navigate to="/city-partners" replace />} />
        <Route path="/cities" element={<CitiesPage />} />
        <Route path="/city-partners" element={<CityPartnersPage />} />
        <Route path="/city-warehouses" element={<CityWarehousesPage />} />
        <Route path="/fulfillment-providers" element={<FulfillmentProvidersPage />} />
        <Route path="/partner-accounts" element={<Navigate to="/city-partners" replace />} />
        <Route path="/benefit/coupons" element={<BenefitCouponsPage />} />
        <Route path="/benefit/ledgers" element={<BenefitLedgersPage />} />
        <Route path="/redeem-records" element={<RedeemRecordsPage />} />
        <Route path="/redeem-pending" element={<PendingRedeemPage />} />
        <Route path="/redeem/debug" element={<RedeemDebugPage />} />
        <Route path="/finance/store-bills" element={<StoreBillsPage />} />
        <Route path="/finance/partner-bills" element={<PartnerBillsPage />} />
        <Route path="/finance/winery-bills" element={<WineryBillsPage />} />
        <Route path="/store-bills" element={<Navigate to="/finance/store-bills" replace />} />
        <Route path="/store-payouts" element={<Navigate to="/finance/store-bills" replace />} />
        <Route path="/partner-bills" element={<Navigate to="/finance/partner-bills" replace />} />
        <Route path="/tickets" element={<TicketsPage />} />
        <Route path="/invoices" element={<InvoicesPage />} />
        <Route path="/logs/users" element={<UserLogsPage />} />
        <Route path="/logs/stores" element={<StoreLogsPage />} />
        <Route path="/logs/partners" element={<PartnerLogsPage />} />
        <Route path="/logs/hq" element={<HqLogsPage />} />
        <Route path="/logs/third-party" element={<ThirdPartyLogsPage />} />
        <Route path="/deliveries" element={<DeliveriesPage />} />
        <Route path="/deliveries/xiaofeixia" element={<XiaofeixiaTestPage />} />
        <Route path="/hq-permissions" element={<HqPermissionsPage />} />
        <Route path="/system-settings" element={<SystemSettingsPage />} />
        <Route path="/hq-accounts" element={<HqAccountsPage />} />
      </Route>
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  );
}
