import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import GlobalLayout from "@/layouts/GlobalLayout.vue";
import HomeLayout from "@/layouts/HomeLayout.vue";
import PageLayout from "@/layouts/PageLayout.vue";
import index from "@/views/index.vue";
import { useAgentStore } from "@/stores/agentStore";
import { storeToRefs } from "pinia";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            component: GlobalLayout, // 使用 Layout 作为父组件
            children: [
                {
                    path: "",
                    component: HomeLayout, // 使用 Layout 作为父组件
                    children: [
                        {
                            path: "",
                            name: "index",
                            component: index,
                        },
                        {
                            path: "ai",
                            name: "ai",
                            component: () => import("@/views/Ai.vue"),
                        },
                        {
                            path: "agent",
                            name: "agent",
                            component: () => import("@/views/Agent.vue"),
                        },
                        {
                            path: "me",
                            name: "me",
                            component: () => import("@/views/Me.vue"),
                        },
                    ],
                },
                {
                    path: "",
                    component: PageLayout,
                    children: [
                        {
                            path: "/historyQuery",
                            name: "history",
                            component: () => import("@/views/HistoryQuery.vue"),
                            meta: { title: "历史报告", requiresAuth: true },
                        },
                        {
                            path: "/help",
                            name: "help",
                            component: () => import("@/views/Help.vue"),
                            meta: { title: "帮助中心" },
                        },

                        {
                            path: "/service",
                            name: "service",
                            component: () => import("@/views/Service.vue"),
                            meta: { title: "客服" },
                        },
                        {
                            path: "/complaint",
                            name: "complaint",
                            component: () => import("@/views/Complaint.vue"),
                            meta: { title: "投诉" },
                        },
                        {
                            path: "/report",
                            name: "report",
                            component: () => import("@/views/Report.vue"),
                            meta: { title: "报告结果", requiresAuth: true },
                        },
                        {
                            path: "/example",
                            name: "example",
                            component: () => import("@/views/Example.vue"),
                            meta: { title: "示例报告" },
                        },
                        {
                            path: "/authorization",
                            name: "authorization",
                            component: () =>
                                import("@/views/Authorization.vue"),
                            meta: { title: "授权书" },
                        },
                        {
                            path: "/privacyPolicy",
                            name: "privacyPolicy",
                            component: () =>
                                import("@/views/PrivacyPolicy.vue"),
                            meta: { title: "隐私政策" },
                        },
                        {
                            path: "/userAgreement",
                            name: "userAgreement",
                            component: () =>
                                import("@/views/UserAgreement.vue"),
                            meta: { title: "用户协议" },
                        },
                        {
                            path: "/agentManageAgreement",
                            name: "agentManageAgreement",
                            component: () =>
                                import("@/views/AgentManageAgreement.vue"),
                            meta: { title: "代理管理协议" },
                        },
                        {
                            path: "/agentSerivceAgreement",
                            name: "agentSerivceAgreement",
                            component: () =>
                                import("@/views/AgentServiceAgreement.vue"),
                            meta: { title: "信息技术服务合同" },
                        },
                        {
                            path: "/inquire/:feature",
                            name: "inquire",
                            component: () => import("@/views/Inquire.vue"),
                            meta: { title: "查询报告" },
                        },
                    ],
                },
                {
                    path: "agent",
                    component: PageLayout,
                    children: [
                        {
                            path: "promoteDetails",
                            name: "promoteDetails",
                            component: () =>
                                import("@/views/AgentPromoteDetails.vue"),
                            meta: {
                                title: "直推报告收益明细",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "rewardsDetails",
                            name: "rewardsDetails",
                            component: () =>
                                import("@/views/AgentRewardsDetails.vue"),
                            meta: {
                                title: "代理奖励收益明细",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "promote",
                            name: "promote",
                            component: () => import("@/views/Promote.vue"),
                            meta: {
                                title: "直推报告",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "invitation",
                            name: "invitation",
                            component: () => import("@/views/Invitation.vue"),
                            meta: {
                                title: "邀请下级",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "agentVip",
                            name: "agentVip",
                            component: () => import("@/views/AgentVip.vue"),
                            meta: {
                                title: "代理会员",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "vipConfig",
                            name: "agentVipConfig",
                            component: () =>
                                import("@/views/AgentVipConfig.vue"),
                            meta: {
                                title: "代理会员报告配置",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "withdraw",
                            name: "withdraw",
                            component: () => import("@/views/Withdraw.vue"),
                            meta: {
                                title: "提现",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "withdrawDetails",
                            name: "withdrawDetails",
                            component: () =>
                                import("@/views/WithdrawDetails.vue"),
                            meta: {
                                title: "提现记录",
                                requiresAuth: true,
                                requiresAgent: true,
                            },
                        },
                        {
                            path: "invitationAgentApply/self",
                            name: "invitationAgentApplySelf",
                            component: () =>
                                import("@/views/InvitationAgentApply.vue"),
                            meta: { title: "代理申请", requiresAuth: true },
                        },
                    ],
                },
            ],
        },

        {
            path: "/login",
            name: "login",
            component: () => import("@/views/Login.vue"),
        },
        {
            path: "/agent/promotionInquire/:linkIdentifier",
            name: "promotionInquire",
            component: () => import("@/views/PromotionInquire.vue"),
        },
        {
            path: "/agent/invitationAgentApply/:linkIdentifier",
            name: "invitationAgentApply",
            component: () => import("@/views/InvitationAgentApply.vue"),
            meta: { title: "代理申请" },
        },
        {
            path: "/:pathMatch(.*)*",
            name: "NotFound",
            component: () => import("@/views/NotFound.vue"),
        },
    ],
});
NProgress.configure({
    easing: "ease", // 动画方式
    speed: 500, // 递增进度条的速度（毫秒）
    showSpinner: false, // 是否显示加载的圆圈
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3, // 初始化最小百分比
});

// 路由导航守卫
router.beforeEach(async (to, from, next) => {
    NProgress.start(); // 启动进度条
    const isAuthenticated = localStorage.getItem("token");
    const agentStore = useAgentStore();
    const { isAgent, isLoaded } = storeToRefs(agentStore);
    if (to.meta.requiresAuth && !isAuthenticated) {
        next("/login");
    } else if (to.meta.requiresAgent && !isAgent.value) {
        if (!isLoaded.value) {
            await agentStore.fetchAgentStatus();
        }
        if (!isAgent.value) {
            next("/agent/invitationAgentApply/self");
        } else {
            next();
        }
    } else {
        next();
    }
});

router.afterEach(() => {
    const { aplus_queue } = window;
    aplus_queue.push({
        action: "aplus.sendPV",
        arguments: [
            {
                is_auto: false,
            },
        ],
    });
    NProgress.done(); // 结束进度条
});
export default router;
