import { useEffect } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { AmbientBackground } from './components/AmbientBackground';
import { Toast } from './components/Toast';
import { VideoGenerationPage } from './components/VideoGenerationPage';
import { ProtectedRoute } from './components/ProtectedRoute';
import { GlobalAnnouncementGate } from './components/GlobalAnnouncementGate';
import { LandingPage } from './pages/LandingPage';

import { AdminLayout } from './pages/AdminLayout';
import { DashboardPage } from './pages/DashboardPage';
import { TeamsPage } from './pages/TeamsPage';
import { UsersPage } from './pages/UsersPage';
import { RecordsPage } from './pages/RecordsPage';
import { SettingsPage } from './pages/SettingsPage';
import { AuditLogsPage } from './pages/AuditLogsPage';
import { AnomalyLogPage } from './pages/AnomalyLogPage';
import { LoginRecordsPage } from './pages/LoginRecordsPage';
import { ProfilePage } from './pages/ProfilePage';
import { AssetsPage } from './pages/AssetsPage';
import { NotificationsPage } from './pages/NotificationsPage';

import { TeamAdminLayout } from './pages/TeamAdminLayout';
import { TeamDashboardPage } from './pages/TeamDashboardPage';
import { TeamMembersPage } from './pages/TeamMembersPage';
import { TeamRecordsPage } from './pages/TeamRecordsPage';
import { AdminAssetsPage } from './pages/AdminAssetsPage';
import { TeamAssetsPage } from './pages/TeamAssetsPage';

import { useAuthStore } from './store/auth';

// 观察者团管进 /admin 跳 assets,超管进 /admin 跳 dashboard
function RoleAwareAdminIndexRedirect() {
  const user = useAuthStore((s) => s.user);
  if (user?.role === 'team_admin' && user?.is_observer) {
    return <Navigate to="/admin/assets" replace />;
  }
  return <Navigate to="/admin/dashboard" replace />;
}

export default function App() {
  const initialize = useAuthStore((s) => s.initialize);

  useEffect(() => {
    initialize();
  }, [initialize]);

  return (
    <BrowserRouter>
      <AmbientBackground />
      <Toast />
      {/* 全局公告 — 任意路由有未读公告就强弹 modal,必须看 */}
      <GlobalAnnouncementGate />
      <Routes>
        <Route path="/" element={<LandingPage />} />
        <Route path="/login" element={<LandingPage autoLogin />} />
        <Route
          path="/app"
          element={
            <ProtectedRoute requireTeamMember>
              <VideoGenerationPage />
            </ProtectedRoute>
          }
        />
        <Route
          path="/user-assets"
          element={
            <ProtectedRoute requireTeamMember>
              <AssetsPage />
            </ProtectedRoute>
          }
        />
        <Route
          path="/profile"
          element={
            <ProtectedRoute requireTeamMember>
              <ProfilePage />
            </ProtectedRoute>
          }
        />
        <Route
          path="/notifications"
          element={
            <ProtectedRoute>
              <NotificationsPage />
            </ProtectedRoute>
          }
        />
        {/* Super Admin routes — 父 requireAdminOrObserver,子页面除 assets 外仍 requireAdmin (观察者团管只能进 assets) */}
        <Route
          path="/admin"
          element={
            <ProtectedRoute requireAdminOrObserver>
              <AdminLayout />
            </ProtectedRoute>
          }
        >
          <Route index element={<RoleAwareAdminIndexRedirect />} />
          <Route path="dashboard" element={<ProtectedRoute requireAdmin><DashboardPage /></ProtectedRoute>} />
          <Route path="teams" element={<ProtectedRoute requireAdmin><TeamsPage /></ProtectedRoute>} />
          <Route path="users" element={<ProtectedRoute requireAdmin><UsersPage /></ProtectedRoute>} />
          <Route path="records" element={<ProtectedRoute requireAdmin><RecordsPage /></ProtectedRoute>} />
          <Route path="settings" element={<ProtectedRoute requireAdmin><SettingsPage /></ProtectedRoute>} />
          <Route path="security" element={<ProtectedRoute requireAdmin><AnomalyLogPage /></ProtectedRoute>} />
          <Route path="login-records" element={<ProtectedRoute requireAdmin><LoginRecordsPage /></ProtectedRoute>} />
          <Route path="logs" element={<ProtectedRoute requireAdmin><AuditLogsPage /></ProtectedRoute>} />
          <Route path="assets" element={<AdminAssetsPage />} />
        </Route>
        {/* Team Admin routes */}
        <Route
          path="/team"
          element={
            <ProtectedRoute requireTeamAdmin>
              <TeamAdminLayout />
            </ProtectedRoute>
          }
        >
          <Route index element={<Navigate to="/team/dashboard" replace />} />
          <Route path="dashboard" element={<TeamDashboardPage />} />
          <Route path="members" element={<TeamMembersPage />} />
          <Route path="records" element={<TeamRecordsPage />} />
          <Route path="assets" element={<TeamAssetsPage />} />
        </Route>
        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </BrowserRouter>
  );
}
