import React, { 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 OrderList from '@/pages/OrderList';
import OrderDetail from '@/pages/OrderDetail';
import RoomList from '@/pages/RoomList';
import RoomForm from '@/pages/RoomForm';
import RoomCalendar from '@/pages/RoomCalendar';
import ReviewList from '@/pages/ReviewList';
import FinanceSettlements from '@/pages/finance/Settlements';
import FinanceSettlementDetail from '@/pages/finance/SettlementDetail';
import FinanceWithdrawals from '@/pages/finance/Withdrawals';
import FinanceWallet from '@/pages/finance/Wallet';
import FinanceTransactions from '@/pages/finance/Transactions';
import Settings from '@/pages/Settings';

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

const App: React.FC = () => (
  <ConfigProvider locale={zhCN} theme={{ token: { colorPrimary: '#FF6B35' } }}>
    <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="orders" element={<OrderList />} />
            <Route path="orders/:orderNo" element={<OrderDetail />} />
            <Route path="rooms" element={<RoomList />} />
            <Route path="rooms/create" element={<RoomForm />} />
            <Route path="rooms/edit/:id" element={<RoomForm />} />
            <Route path="room-calendar" element={<RoomCalendar />} />
            <Route path="room-calendar/:roomId" element={<RoomCalendar />} />
            <Route path="reviews" element={<ReviewList />} />
            <Route path="finance">
              <Route index element={<Navigate to="/finance/settlements" replace />} />
              <Route path="settlements" element={<FinanceSettlements />} />
              <Route path="settlements/:id" element={<FinanceSettlementDetail />} />
              <Route path="withdrawals" element={<FinanceWithdrawals />} />
              <Route path="wallet" element={<FinanceWallet />} />
              <Route path="transactions" element={<FinanceTransactions />} />
            </Route>
            <Route path="settings" element={<Settings />} />
          </Route>
        </Routes>
      </Suspense>
    </BrowserRouter>
  </ConfigProvider>
);

export default App;
