import { lazy, Suspense } from 'react';
import { Spin } from 'antd';
import {
    RouteObject
} from 'react-router-dom';

interface StateGenerator {
    constantRoutes: RouteObject[]
}

// 按需加载
const lazyLoad = (loader: () => Promise<any>) => {
    const Component = lazy(loader);

    const Loading: React.FC = () => {
        return (
            <div className='router-lazyLoad'>
                <Spin />
            </div>
        )
    }

    return (
        <Suspense fallback={<Loading />}>
            <Component />
        </Suspense>
    );
};

export const businessRoutes: RouteObject[] = [
    {
        path: "/overview",
        element: lazyLoad(() => import('@/pages/home/index')),
        handle: {
            breadcrumbName: "概览",
            hidden: false
        }
    },
    {
        path: "/appCenter",
        handle: {
            breadcrumbName: "应用中心",
            hidden: false
        },
        children: [
            {
                index: true,
                element: lazyLoad(() => import('@/pages/appCenter/appPlazaList/index')),
                handle: {
                    hidden: true,
                    breadcrumbName: "全部 RAG 应用"
                }
            },
            {
                path: "category/*",
                element: lazyLoad(() => import('@/pages/appCenter/categoryApps/index')),
                handle: {
                    breadcrumbName: "分类应用",
                    hidden: true
                }
            },
            {
                path: "questionAnswer",
                element: lazyLoad(() => import('@/pages/questionAnswer/list/index')),
                handle: {
                    breadcrumbName: "我创建的应用",
                    hidden: false
                }
            },
            {
                path: "questionAnswer/list",
                element: lazyLoad(() => import('@/pages/questionAnswer/list/index')),
                handle: {
                    breadcrumbName: "我创建的应用列表",
                    hidden: true
                }
            },
            {
                path: "questionAnswer/create",
                element: lazyLoad(() => import('@/pages/questionAnswer/form/index')),
                handle: {
                    breadcrumbName: "创建应用",
                    hidden: true
                }
            },
            {
                path: "questionAnswer/modify",
                element: lazyLoad(() => import('@/pages/questionAnswer/form/index')),
                handle: {
                    breadcrumbName: "修改应用",
                    hidden: true
                }
            }
        ]
    },
    {
        path: "/otherApps",
        element: lazyLoad(() => import('@/pages/otherApps/otherAppList/index')),
        handle: {
            breadcrumbName: "其他非 RAG 应用",
            hidden: true
        }
    },
    {
        path: "/knowledge",
        handle: {
            breadcrumbName: "知识库管理",
            hidden: false
        },
        children: [
            {
                path: "knowledgeLib",
                handle: {
                    breadcrumbName: "知识库",
                    hidden: false
                },
                children: [
                    {
                        index: true,
                        element: lazyLoad(() => import('@/pages/knowledgeLib/list/index')),
                        handle: {
                            hidden: true
                        }
                    },
                    {
                        path: "create",
                        element: lazyLoad(() => import('@/pages/knowledgeLib/list/index')),
                        handle: {
                            breadcrumbName: "创建知识库",
                            hidden: true
                        }
                    },
                    {
                        path: ":knowledgeId/:createBy",
                        element: lazyLoad(() => import('@/pages/knowledgeLib/detail/index')),
                        handle: {
                            breadcrumbName: "知识库详情",
                            hidden: true
                        }
                    }
                ]
            },
            {
                path: "revisionTool",
                handle: {
                    breadcrumbName: "修订工具",
                    hidden: false
                },
                children: [
                    {
                        index: true,
                        element: lazyLoad(() => import('@/pages/knowledgeLib/revisionTool/list/index')),
                        handle: {
                            hidden: true
                        }
                    }
                ]
            }
        ]
    },
    {
        path: "/system",
        handle: {
            breadcrumbName: "系统管理",
            hidden: false
        },
        children: [
            {
                path: "apiKey",
                handle: {
                    breadcrumbName: "API Key",
                    hidden: false
                },
                children: [
                    {
                        index: true,
                        element: lazyLoad(() => import('@/pages/system/apiKey/index')),
                        handle: {
                            hidden: true
                        }
                    }
                ]
            },
            {
                path: "audit",
                handle: {
                    breadcrumbName: "应用审核",
                    hidden: false
                },
                children: [
                    {
                        index: true,
                        element: lazyLoad(() => import('@/pages/system/audit/index.placeholder')),
                        handle: {
                            hidden: true
                        }
                    }
                ]
            }
        ]
    },
];

export const stateGenerator: StateGenerator = {
    constantRoutes: [
        ...businessRoutes,
    ]
}

const useRouteStore = () => {
    const state = stateGenerator;

    // 简单的菜单生成逻辑
    const useMenuFromRoutes = (routes: RouteObject[]) => {
        const transformRoutesToMenu = (routes: RouteObject[], parentPath?: string): any[] => {
            return routes
                .filter(route => !(route.handle && (route.handle as any).hidden))
                .map((route) => {
                    const handle = (route.handle || {}) as any;
                    const routePath = route.path;
                    const fullPath = parentPath ? `${parentPath}/${routePath}` : routePath;

                    const item: any = {
                        key: fullPath || '',
                        icon: handle.icon || null,
                        label: handle.breadcrumbName || '未命名',
                        path: fullPath || '',
                    };

                    if (route.children && route.children.length > 0) {
                        const childMenus = transformRoutesToMenu(route.children, fullPath);
                        if (childMenus.length > 0) {
                            item.children = childMenus;
                        }
                    }
                    return item;
                });
        };
        return transformRoutesToMenu(routes);
    };

    return {
        state,
        useMenuFromRoutes
    };
}

export default useRouteStore();
