import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import PagePermissionGuard from './components/PagePermissionGuard';
import { usePagePermissions } from './hooks/usePagePermissions';
import Layout from './components/Layout';
import Dashboard from './pages/Dashboard';
import Overview from './pages/Overview';
import Operations from './pages/Operations';
import Monitoring from './pages/Monitoring';
import Finance from './pages/Finance';
import FinanceTransactionLogs from './pages/FinanceTransactionLogs';
import TokenHistoryPage from './pages/TokenHistory';
import TokenAnalytics from './pages/TokenAnalytics';
import SystemHealth from './pages/SystemHealth';
import UserProjectQuotaWrapper from './pages/UserProjectQuota';
import UserManagement from './pages/UserManagement';
import RoleManagement from './pages/RoleManagement';
import GoalfyMaxUsers from './pages/GoalfyMaxUsers';
import UserFeedback from './pages/UserFeedback';
import MessagePush from './pages/MessagePush';
import VendorModelPricing from './pages/VendorModelPricing';
import MCPProviderPricing from './pages/MCPProviderPricing';
import VmPricing from './pages/VmPricing';
import SystemConfigs from './pages/SystemConfigs';
import UserLevelConfigs from './pages/UserLevelConfigs';
import AuditLogs from './pages/AuditLogs';
import InviteCodes from './pages/InviteCodes';
import NoPermission from './pages/NoPermission';
import FinanceSandboxCosts from './pages/FinanceSandboxCosts';
import FinanceTokenUsage from './pages/FinanceTokenUsage';
import FinanceMcpUsage from './pages/FinanceMcpUsage';
import FinancePaymentRecords from './pages/FinancePaymentRecords';
import FinanceMcpAccountRechargeRecords from './pages/FinanceMcpAccountRechargeRecords';
import FinanceModelAccountRechargeRecords from './pages/FinanceModelAccountRechargeRecords';

function App() {
  const FirstAllowedRedirect: React.FC = () => {
    const { getAccessiblePages } = usePagePermissions();
    const pages = getAccessiblePages();
    const target = pages[0] || '/no-permission';
    console.log('↪️ [Router] default redirect to:', target, 'pages=', pages);
    return <Navigate to={target} replace />;
  };
  return (
    <Layout>
      <Routes>
        {/* 默认落地：首个可访问页面或无权限页 */}
        <Route path="/" element={<FirstAllowedRedirect />} />
        
        {/* 仪表盘（受页面权限控制） */}
        <Route
          path="/dashboard"
          element={
            <PagePermissionGuard pagePath="/dashboard" fallback={<Navigate to="/no-permission" replace />}> 
              <Dashboard />
            </PagePermissionGuard>
          }
        />
        
        {/* 总览页面（受页面权限控制） */}
        <Route
          path="/overview"
          element={
            <PagePermissionGuard pagePath="/overview" fallback={<Navigate to="/no-permission" replace />}> 
              <Overview />
            </PagePermissionGuard>
          }
        />
        
        {/* 运营页面 - 嵌套路由（父路径受控） */}
        <Route path="/operations" element={<Navigate to="/operations/user-feedback" replace />} />
        <Route
          path="/operations/user-feedback"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <UserFeedback />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/operations/message-push"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <MessagePush />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/operations/vendor-model-pricing"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <VendorModelPricing />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/operations/mcp-provider-pricing"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <MCPProviderPricing />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/operations/vm-pricing"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <VmPricing />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/operations/invite-codes"
          element={
            <PagePermissionGuard pagePath="/operations" fallback={<Navigate to="/no-permission" replace />}> 
              <InviteCodes />
            </PagePermissionGuard>
          }
        />
        
        {/* 监控页面 - 嵌套路由（父路径受控） */}
        <Route path="/monitoring" element={<Navigate to="/monitoring/token-history" replace />} />
        <Route
          path="/monitoring/token-history"
          element={
            <PagePermissionGuard pagePath="/monitoring" fallback={<Navigate to="/no-permission" replace />}> 
              <TokenHistoryPage />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/monitoring/token-analytics"
          element={
            <PagePermissionGuard pagePath="/monitoring" fallback={<Navigate to="/no-permission" replace />}> 
              <TokenAnalytics />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/monitoring/system-health"
          element={
            <PagePermissionGuard pagePath="/monitoring" fallback={<Navigate to="/no-permission" replace />}> 
              <SystemHealth />
            </PagePermissionGuard>
          }
        />
        
        {/* 财务页面（父路由重定向到子页面：用户流水） */}
        <Route path="/finance" element={<Navigate to="/finance/transaction-logs" replace />} />
        <Route
          path="/finance/transaction-logs"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceTransactionLogs />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/sandbox-costs"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceSandboxCosts />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/token-usage"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceTokenUsage />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/mcp-usage"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceMcpUsage />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/payment-records"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinancePaymentRecords />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/mcp-account-recharge-records"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceMcpAccountRechargeRecords />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/finance/model-account-recharge-records"
          element={
            <PagePermissionGuard pagePath="/finance" fallback={<Navigate to="/no-permission" replace />}> 
              <FinanceModelAccountRechargeRecords />
            </PagePermissionGuard>
          }
        />
        
        {/* 系统管理页面 - 嵌套路由 */}
        <Route path="/system" element={<Navigate to="/system/user-management" replace />} />
        <Route
          path="/system/user-project-quota"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <UserProjectQuotaWrapper />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/user-management"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <UserManagement />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/role-management"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <RoleManagement />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/goalfymax-users"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <GoalfyMaxUsers />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/user-level-configs"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <UserLevelConfigs />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/system-configs"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <SystemConfigs />
            </PagePermissionGuard>
          }
        />
        <Route
          path="/system/audit-logs"
          element={
            <PagePermissionGuard pagePath="/system" fallback={<Navigate to="/no-permission" replace />}> 
              <AuditLogs />
            </PagePermissionGuard>
          }
        />

        {/* 无权限页面 */}
        <Route path="/no-permission" element={<NoPermission />} />
        
        {/* 404页面 */}
        <Route path="*" element={<Navigate to="/dashboard" replace />} />
      </Routes>
    </Layout>
  );
}

export default App;