import Layout from "./Layout.jsx";

import HotTopics from "./HotTopics";

import TemplateManagement from "./TemplateManagement";

import MaterialManagement from "./MaterialManagement";
import MaterialLibrary from "./MaterialLibrary";

import AIGeneration from "./AIGeneration_new827";

import ContentBrowser from "./ContentBrowser";

import AutoPublish from "./AutoPublish";

import History from "./History";

import AIAssetSearch from "./AIAssetSearch";

import Dashboard from "./Dashboard";

import LoginPage from "./LoginPage";

import RegisterPage from "./RegisterPage";

import Loading from "@/components/ui/loading";

import { BrowserRouter as Router, Route, Routes, useLocation, Navigate } from 'react-router-dom';

import { useAuth } from '../contexts/AuthContext';

const PAGES = {
    
    HotTopics: HotTopics,
    
    TemplateManagement: TemplateManagement,
    
    MaterialManagement: MaterialManagement,
    MaterialLibrary: MaterialLibrary,
    
    AIGeneration: AIGeneration,
    
    ContentBrowser: ContentBrowser,
    
    AutoPublish: AutoPublish,
    
    History: History,
    
    AIAssetSearch: AIAssetSearch,
    
    Dashboard: Dashboard,
    
}

function _getCurrentPage(url) {
    if (url.endsWith('/')) {
        url = url.slice(0, -1);
    }
    let urlLastPart = url.split('/').pop();
    if (urlLastPart.includes('?')) {
        urlLastPart = urlLastPart.split('?')[0];
    }

    const pageName = Object.keys(PAGES).find(page => page.toLowerCase() === urlLastPart.toLowerCase());
    return pageName || Object.keys(PAGES)[0];
}

// 受保护的路由组件
const ProtectedRoute = ({ children }) => {
    const { isAuthenticated, loading } = useAuth();
    
    if (loading) {
        return <Loading message="正在验证身份..." />;
    }
    
    if (!isAuthenticated) {
        return <Navigate to="/login" replace />;
    }
    
    return children;
};

// 公共路由组件（已登录用户重定向到仪表盘）
const PublicRoute = ({ children }) => {
    const { isAuthenticated, loading } = useAuth();
    
    if (loading) {
        return <Loading message="正在验证身份..." />;
    }
    
    if (isAuthenticated) {
        return <Navigate to="/Dashboard" replace />;
    }
    
    return children;
};

// Create a wrapper component that uses useLocation inside the Router context
function PagesContent() {
    const location = useLocation();
    const currentPage = _getCurrentPage(location.pathname);
    
    return (
        <Routes>
            {/* 认证相关路由 */}
            <Route path="/login" element={
                <PublicRoute>
                    <LoginPage />
                </PublicRoute>
            } />
            <Route path="/register" element={
                <PublicRoute>
                    <RegisterPage />
                </PublicRoute>
            } />
            
            {/* 受保护的应用路由 */}
            <Route path="/" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <HotTopics />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/HotTopics" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <HotTopics />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/TemplateManagement" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <TemplateManagement />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/MaterialManagement" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <MaterialManagement />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/MaterialLibrary" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <MaterialLibrary />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/AIGeneration" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <AIGeneration />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/ContentBrowser" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <ContentBrowser />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/AutoPublish" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <AutoPublish />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/History" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <History />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/AIAssetSearch" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <AIAssetSearch />
                    </Layout>
                </ProtectedRoute>
            } />
            
            <Route path="/Dashboard" element={
                <ProtectedRoute>
                    <Layout currentPageName={currentPage}>
                        <Dashboard />
                    </Layout>
                </ProtectedRoute>
            } />
            
            {/* 默认重定向到登录页面 */}
            <Route path="*" element={<Navigate to="/login" replace />} />
        </Routes>
    );
}

export default function Pages() {
    return (
        <Router>
            <PagesContent />
        </Router>
    );
}