import { createRouter, createWebHistory } from "vue-router";
import { creativeApi } from "@/api/creative";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/LoginView.vue"),
      meta: { public: true, title: "登录" },
    },
    {
      path: "/",
      component: () => import("@/layouts/WebLayout.vue"),
      children: [
        { path: "", redirect: "/drama-projects" },
        {
          path: "drama-projects",
          name: "drama-projects",
          component: () => import("@/views/RedrawProjectsView.vue"),
          meta: { title: "短剧创作", projectType: "premium_drama" },
        },
        {
          path: "drama-projects/:projectId",
          name: "drama-project",
          component: () => import("@/views/DramaProjectView.vue"),
          meta: { title: "短剧项目" },
        },
        {
          path: "drama-projects/:projectId/episodes/:episodeId",
          name: "drama-workbench",
          component: () => import("@/views/DramaWorkbenchView.vue"),
          meta: { title: "短剧工作台", fullscreen: true },
        },
        {
          path: "redraw-projects",
          name: "redraw-projects",
          component: () => import("@/views/RedrawProjectsView.vue"),
          meta: { title: "剧本反推" },
        },
        {
          path: "redraw-projects/:projectId",
          name: "redraw-project",
          component: () => import("@/views/RedrawProjectView.vue"),
          meta: { title: "剧本反推项目" },
        },
        {
          path: "redraw-projects/:projectId/episodes/:episodeId",
          name: "redraw-workbench",
          component: () => import("@/views/RedrawWorkbenchView.vue"),
          meta: { title: "剧本反推", fullscreen: true },
          beforeEnter: async (to) => {
            try {
              const project = await creativeApi.project(String(to.params.projectId));
              if (project.project_type === "video_redraw") return true;
              return { name: "drama-project", params: { projectId: project.id } };
            } catch {
              return { name: "redraw-projects" };
            }
          },
        },
        {
          path: "script-analysis",
          name: "script-analysis",
          component: () => import("@/views/ScriptAnalysisView.vue"),
          meta: { title: "剧本分析" },
        },
        {
          path: "script-analysis/:scriptId",
          name: "script-analysis-workbench",
          component: () => import("@/views/ScriptAnalysisWorkbenchView.vue"),
          meta: { title: "剧本处理", fullscreen: true },
        },
        {
          path: "product-images",
          name: "product-images",
          component: () => import("@/views/ProductImageWorkbenchView.vue"),
          meta: { title: "图片生成" },
        },
        {
          path: "account",
          name: "account",
          component: () => import("@/views/AccountView.vue"),
          meta: { title: "用户中心" },
        },
      ],
    },
    { path: "/:pathMatch(.*)*", redirect: "/drama-projects" },
  ],
});

router.beforeEach((to) => {
  const authenticated = Boolean(localStorage.getItem("web_access_token"));
  if (!to.meta.public && !authenticated) return { name: "login", query: { redirect: to.fullPath } };
  if (to.name === "login" && authenticated) return "/drama-projects";
  document.title = "剧游AI";
  return true;
});

export default router;
