import { useEffect } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider, App as AntApp, Spin } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import AdminLayout from './pages/AdminLayout';
import AdminLoginPage from './pages/AdminLoginPage';
import AdminDashboard from './pages/AdminDashboard';
import AdminUsers from './pages/AdminUsers';
import AdminModels from './pages/AdminModels';
import AdminSettings from './pages/AdminSettings';
import AdminNotificationManager from './pages/AdminNotificationManager';
import AdminCreditRecords from './pages/AdminCreditRecords';
import AdminPaymentConfig from './pages/AdminPaymentConfig';
import AdminIndustries from './pages/AdminIndustries';
import AdminVideoEngines from './pages/AdminVideoEngines';
import AdminImageEngines from './pages/AdminImageEngines';
import AdminCreditRatios from './pages/AdminCreditRatios';
import AdminMenuConfig from './pages/AdminMenuConfig';
import AdminRechargePackages from './pages/AdminRechargePackages';
import AdminOperationLogs from './pages/AdminOperationLogs';
import AdminGenerationRecords from './pages/AdminGenerationRecords';
import { useAdminStore } from './store';

const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  const { user, loading } = useAdminStore();
  if (loading) {
    return (
      <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
        <Spin size="large" />
      </div>
    );
  }
  if (!user) return <Navigate to="/login" replace />;
  return <>{children}</>;
};

const App = () => {
  const { checkAuth } = useAdminStore();
  useEffect(() => { checkAuth(); }, []);

  return (
    <ConfigProvider locale={zhCN} theme={{
      token: { colorPrimary: '#6366f1', borderRadius: 8 },
      components: {
        Button: { controlHeight: 36, controlHeightLG: 44 },
        Card: { boxShadow: '0 1px 3px rgba(0,0,0,0.04)' },
        Table: { headerBg: '#fafbfc' },
      },
    }}>
      <AntApp>
        <BrowserRouter>
          <Routes>
            <Route path="/login" element={<AdminLoginPage />} />
            <Route path="/" element={<ProtectedRoute><AdminLayout /></ProtectedRoute>}>
              <Route index element={<AdminDashboard />} />
              <Route path="users" element={<AdminUsers />} />
              <Route path="credit-records" element={<AdminCreditRecords />} />
              <Route path="models" element={<AdminModels />} />
              <Route path="credit-ratios" element={<AdminCreditRatios />} />
              <Route path="video-engines" element={<AdminVideoEngines />} />
              <Route path="industries" element={<AdminIndustries />} />
              <Route path="menu-configs" element={<AdminMenuConfig />} />
              <Route path="recharge-packages" element={<AdminRechargePackages />} />
              <Route path="payment" element={<AdminPaymentConfig />} />
              <Route path="settings" element={<AdminSettings />} />
              <Route path="notifications" element={<AdminNotificationManager />} />
              <Route path="operation-logs" element={<AdminOperationLogs />} />
              <Route path="generation-records" element={<AdminGenerationRecords />} />
            </Route>
            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </BrowserRouter>
      </AntApp>
    </ConfigProvider>
  );
};

export default App;
