import React from 'react';
import { Suspense } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import MainLayout from '@/layouts/MainLayout';
import Login from '@/pages/Login';
import Dashboard from '@/pages/Dashboard';
import MerchantList from '@/pages/MerchantList';
import MerchantDetail from '@/pages/MerchantDetail';
import RoomAudit from '@/pages/RoomAudit';
import UserList from '@/pages/UserList';
import OrderList from '@/pages/OrderList';
import OrderDetail from '@/pages/OrderDetail';
import OrderStatistics from '@/pages/OrderStatistics';
import CouponList from '@/pages/coupon/CouponList';
import CouponForm from '@/pages/coupon/CouponForm';
import Promotion from '@/pages/Promotion';
import SystemSettings from '@/pages/SystemSettings';
import FinanceSettlements from '@/pages/finance/Settlements';
import FinanceWithdrawals from '@/pages/finance/Withdrawals';
import FinanceEarnings from '@/pages/finance/Earnings';
import FinanceServiceFees from '@/pages/finance/ServiceFees';
import FinanceDashboard from '@/pages/finance/Dashboard';
import FinanceAccounts from '@/pages/finance/Accounts';
import PlatformWallet from '@/pages/finance/PlatformWallet';
import PlatformTransactions from '@/pages/finance/PlatformTransactions';
import PlatformWithdrawals from '@/pages/finance/PlatformWithdrawals';
import BankCards from '@/pages/finance/BankCards';
import InviteManage from '@/pages/InviteManage';
import ReviewManage from '@/pages/ReviewManage';

const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const token = localStorage.getItem('admin_token');
  if (!token) return <Navigate to="/login" replace />;
  return <>{children}</>;
};

const App: React.FC = () => (
  <ConfigProvider locale={zhCN} theme={{ token: { colorPrimary: '#1890ff' } }}>
    <BrowserRouter>
      <Suspense fallback={<div style={{ textAlign: 'center', padding: 100 }}>加载中...</div>}>
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route path="/" element={<ProtectedRoute><MainLayout /></ProtectedRoute>}>
            <Route index element={<Navigate to="/dashboard" replace />} />
            <Route path="dashboard" element={<Dashboard />} />
            <Route path="merchants" element={<MerchantList />} />
            <Route path="merchants/:id" element={<MerchantDetail />} />
            <Route path="room-audit" element={<RoomAudit />} />
            <Route path="users" element={<UserList />} />
            <Route path="orders" element={<OrderList />} />
            <Route path="orders/:id" element={<OrderDetail />} />
            <Route path="order-statistics" element={<OrderStatistics />} />
            <Route path="finance">
              <Route index element={<Navigate to="/finance/dashboard" replace />} />
              <Route path="dashboard" element={<FinanceDashboard />} />
              <Route path="accounts" element={<FinanceAccounts />} />
              <Route path="platform-wallet" element={<PlatformWallet />} />
              <Route path="platform-transactions" element={<PlatformTransactions />} />
              <Route path="platform-withdrawals" element={<PlatformWithdrawals />} />
              <Route path="bank-cards" element={<BankCards />} />
              <Route path="settlements" element={<FinanceSettlements />} />
              <Route path="withdrawals" element={<FinanceWithdrawals />} />
              <Route path="earnings" element={<FinanceEarnings />} />
              <Route path="service-fees" element={<FinanceServiceFees />} />
            </Route>
            <Route path="invite" element={<InviteManage />} />
            <Route path="reviews" element={<ReviewManage />} />
            <Route path="coupons" element={<CouponList />} />
            <Route path="coupons/create" element={<CouponForm />} />
            <Route path="coupons/edit/:id" element={<CouponForm />} />
            <Route path="promotions" element={<Promotion />} />
            <Route path="settings" element={<SystemSettings />} />
          </Route>
        </Routes>
      </Suspense>
    </BrowserRouter>
  </ConfigProvider>
);

export default App;
