import { Routes, Route, Navigate } from 'react-router-dom';
import { useEffect } from 'react';
import TabLayout from './layouts/TabLayout';
import LoginPage from './pages/LoginPage';
import HomePage from './pages/HomePage';
import ProductDetailPage from './pages/ProductDetailPage';
import OrderConfirmPage from './pages/OrderConfirmPage';
import AddressListPage from './pages/AddressListPage';
import AddressEditPage from './pages/AddressEditPage';
import OrderListPage from './pages/OrderListPage';
import OrderDetailPage from './pages/OrderDetailPage';
import StoreListPage from './pages/StoreListPage';
import StoreDetailPage from './pages/StoreDetailPage';
import BenefitPage from './pages/BenefitPage';
import BenefitDetailPage from './pages/BenefitDetailPage';
import MinePage from './pages/MinePage';
import RedeemPage from './pages/RedeemPage';
import RedeemCodePage from './pages/RedeemCodePage';
import RedeemSuccessPage from './pages/RedeemSuccessPage';
import PayPage from './pages/PayPage';
import CustomerServicePage from './pages/CustomerServicePage';
import { UserSessionProvider } from './contexts/UserSessionContext';
import { capturePromoFromUrl, touchPromoIfNeeded } from './lib/promo';
import WechatShareBootstrap from './components/WechatShareBootstrap';

function PromoBootstrap() {
  useEffect(() => {
    capturePromoFromUrl();
    void touchPromoIfNeeded();
  }, []);
  return null;
}

export default function App() {
  return (
    <UserSessionProvider>
    <PromoBootstrap />
    <WechatShareBootstrap />
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route element={<TabLayout />}>
        <Route path="/" element={<HomePage />} />
        <Route path="/stores" element={<StoreListPage />} />
        <Route path="/benefit" element={<BenefitPage />} />
        <Route path="/mine" element={<MinePage />} />
      </Route>
      <Route path="/product/:id" element={<ProductDetailPage />} />
      <Route path="/stores/:id" element={<StoreDetailPage />} />
      <Route path="/order/confirm" element={<OrderConfirmPage />} />
      <Route path="/pay" element={<PayPage />} />
      <Route path="/customer-service" element={<CustomerServicePage />} />
      <Route path="/addresses" element={<AddressListPage />} />
      <Route path="/addresses/new" element={<AddressEditPage />} />
      <Route path="/addresses/:id/edit" element={<AddressEditPage />} />
      <Route path="/orders" element={<OrderListPage />} />
      <Route path="/orders/:id" element={<OrderDetailPage />} />
      <Route path="/benefit/:id" element={<BenefitDetailPage />} />
      <Route path="/redeem" element={<RedeemPage />} />
      <Route path="/redeem/code" element={<RedeemCodePage />} />
      <Route path="/redeem/success" element={<RedeemSuccessPage />} />
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
    </UserSessionProvider>
  );
}
