import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import './assets/landing-dynamics.css'
import './styles/page-animations.css'
import { fetchWebRoutes } from './api/webPages'

async function bootstrap() {
  const app = createApp(App)
  app.use(router)
  try {
    const data = await fetchWebRoutes()
    const DynamicPage = () => import('./views/DynamicPage.vue')
    for (const r of data.routes || []) {
      const path = r.path
      if (!path || path === '/') continue
      router.addRoute({
        path,
        name: `web-page-${r.id}`,
        component: DynamicPage,
        meta: { title: r.title || '', pagePath: path }
      })
    }
  } catch (_) {
    /* 动态路由失败时仍展示首页 */
  }
  // 通配路由须最后注册，避免盖住已发布的动态页面
  router.addRoute({
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    meta: { title: '页面未找到' },
    component: () => import('./views/NotFound.vue')
  })
  app.mount('#app')
}

bootstrap()
