import { Navigate } from 'react-router-dom';

import { APP_ROUTE_METAS } from '@/defines/appRoutes';
import Layout from '@/layouts/BasicLayout';
import Forbidden from '@/pages/error/403';
import NotFound from '@/pages/error/404';
import ServerError from '@/pages/error/500';
import Home from '@/pages/home';
import Pricing from '@/pages/pricing';
import PrivacyPolicy from '@/pages/privacyPolicy';
import Redirect from '@/pages/redirect';
import TermsOfService from '@/pages/termsOfService';

import type { AppRouteObject } from './types';

/**
 * name → 页面组件 的映射。key 与 defines/appRoutes.ts 的 APP_ROUTE_METAS[].name 对应。
 * 组件在此显式 import（不走 glob 魔法），路径/菜单可见性/locale 等元信息统一在 APP_ROUTE_METAS 声明，
 * 二者以 name 关联。新增业务页：在 APP_ROUTE_METAS 追加一项，并在此补对应组件即可。
 */
const PAGE_COMPONENTS: Record<string, React.ReactNode> = {
    home: <Home />,
    pricing: <Pricing />,
    privacyPolicy: <PrivacyPolicy />,
    termsOfService: <TermsOfService />,
};

// 由「纯数据清单 + 组件映射」拼出 BasicLayout 下的业务子路由，与导航菜单同源（见 utils/navUtils.ts）。
const businessRoutes: AppRouteObject[] = APP_ROUTE_METAS.map((meta) => ({
    name: meta.name,
    path: `/${meta.path}`,
    locale: meta.locale,
    hideInMenu: meta.hideInMenu,
    element: PAGE_COMPONENTS[meta.name],
}));

const routes: AppRouteObject[] = [
    {
        path: '/',
        element: <Layout />,
        children: [
            {
                index: true,
                element: <Navigate to="/home" replace />,
            },
            ...businessRoutes,
        ],
    },
    {
        name: 'to',
        path: '/to',
        hideInMenu: true,
        element: <Redirect />,
    },
    {
        name: '403',
        path: '/403',
        element: <Forbidden />,
        hideInMenu: true,
    },
    {
        name: '500',
        path: '/500',
        element: <ServerError />,
        hideInMenu: true,
    },
    {
        name: '404',
        path: '*',
        element: <NotFound />,
        hideInMenu: true,
    },
] as AppRouteObject[];

export default routes;
