import React, { useEffect } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { AuthProvider } from './contexts/AuthContext';
import { WechatAccountProvider } from './contexts/WechatAccountContext';
import { ToastProvider } from './components/ui/toast';
import ProtectedRoute from './components/ProtectedRoute';
import LayoutWrapper from './components/LayoutWrapper';
import { initInterceptors } from './api';
import Home from './pages/Home';
import Login from './pages/login/Login';
import Devices from './pages/devices/Devices';
import DeviceDetail from './pages/devices/DeviceDetail';
import WechatAccounts from './pages/wechat-accounts/WechatAccounts';
import WechatAccountDetail from './pages/wechat-accounts/WechatAccountDetail';
import Workspace from './pages/workspace/Workspace';
import AutoLike from './pages/workspace/auto-like/AutoLike';
import NewAutoLike from './pages/workspace/auto-like/NewAutoLike';
import AutoLikeDetail from './pages/workspace/auto-like/AutoLikeDetail';
import AutoGroup from './pages/workspace/auto-group/AutoGroup';
import AutoGroupDetail from './pages/workspace/auto-group/Detail';
import GroupPush from './pages/workspace/group-push/GroupPush';
import MomentsSync from './pages/workspace/moments-sync/MomentsSync';
import MomentsSyncDetail from './pages/workspace/moments-sync/Detail';
import NewMomentsSync from './pages/workspace/moments-sync/new';
import AIAssistant from './pages/workspace/ai-assistant/AIAssistant';
import TrafficDistribution from './pages/workspace/traffic-distribution/TrafficDistribution';
import TrafficDistributionDetail from './pages/workspace/traffic-distribution/Detail';
import Scenarios from './pages/scenarios/Scenarios';
import NewPlan from './pages/scenarios/NewPlan';
import ScenarioDetail from './pages/scenarios/ScenarioDetail';
import Profile from './pages/profile/Profile';
import Plans from './pages/plans/Plans';
import PlanDetail from './pages/plans/PlanDetail';
import Orders from './pages/orders/Orders';
import TrafficPool from './pages/traffic-pool/TrafficPool';
import ContactImport from './pages/contact-import/ContactImport';
import Content from './pages/content/Content';

function App() {
  // 初始化HTTP拦截器
  useEffect(() => {
    const cleanup = initInterceptors();
    return cleanup;
  }, []);

  return (
    <BrowserRouter>
      <AuthProvider>
        <WechatAccountProvider>
          <ToastProvider>
            <ProtectedRoute>
              <LayoutWrapper>
                <Routes>
                  <Route path="/" element={<Home />} />
                  <Route path="/login" element={<Login />} />
                  <Route path="/devices" element={<Devices />} />
                  <Route path="/devices/:id" element={<DeviceDetail />} />
                  <Route path="/wechat-accounts" element={<WechatAccounts />} />
                  <Route path="/wechat-accounts/:id" element={<WechatAccountDetail />} />
                  <Route path="/workspace" element={<Workspace />} />
                  <Route path="/workspace/auto-like" element={<AutoLike />} />
                  <Route path="/workspace/auto-like/new" element={<NewAutoLike />} />
                  <Route path="/workspace/auto-like/:id" element={<AutoLikeDetail />} />
                  <Route path="/workspace/auto-like/:id/edit" element={<NewAutoLike />} />
                  <Route path="/workspace/auto-group" element={<AutoGroup />} />
                  <Route path="/workspace/auto-group/:id" element={<AutoGroupDetail />} />
                  <Route path="/workspace/group-push" element={<GroupPush />} />
                  <Route path="/workspace/moments-sync" element={<MomentsSync />} />
                  <Route path="/workspace/moments-sync/new" element={<NewMomentsSync />} />
                  <Route path="/workspace/moments-sync/:id" element={<MomentsSyncDetail />} />
                  <Route path="/workspace/moments-sync/edit/:id" element={<NewMomentsSync />} />
                  <Route path="/workspace/ai-assistant" element={<AIAssistant />} />
                  <Route path="/workspace/traffic-distribution" element={<TrafficDistribution />} />
                  <Route path="/workspace/traffic-distribution/:id" element={<TrafficDistributionDetail />} />
                  <Route path="/scenarios" element={<Scenarios />} />
                  <Route path="/scenarios/new" element={<NewPlan />} />
                  {/* 通用场景路由 - 支持查询参数传递name */}
                  <Route path="/scenarios/:scenarioId" element={<ScenarioDetail />} />
                  <Route path="/profile" element={<Profile />} />
                  <Route path="/plans" element={<Plans />} />
                  <Route path="/plans/:planId" element={<PlanDetail />} />
                  <Route path="/orders" element={<Orders />} />
                  <Route path="/traffic-pool" element={<TrafficPool />} />
                  <Route path="/contact-import" element={<ContactImport />} />
                  <Route path="/content" element={<Content />} />
                  {/* 你可以继续添加更多路由 */}
                </Routes>
              </LayoutWrapper>
            </ProtectedRoute>
          </ToastProvider>
        </WechatAccountProvider>
      </AuthProvider>
    </BrowserRouter>
  );
}

export default App;
