import { createRouter, createWebHistory } from 'vue-router'

const DISABLE_AUTH = import.meta.env.VITE_DISABLE_AUTH === 'true'

const routes = [
  // Root → /home
  { path: '/', redirect: '/home' },

  // Sign-in
  {
    path: '/sign-in',
    component: () => import('../pages/sign-in.vue'),
    beforeEnter: () => { if (DISABLE_AUTH) return { path: '/home' } }
  },

  // ── /home/* (tất cả dashboard pages) ──────────────────────────
  { path: '/home',               component: () => import('../pages/home.vue'),                    meta: { requiresAuth: true } },
  { path: '/home/product',       component: () => import('../pages/product.vue'),                 meta: { requiresAuth: true } },
  { path: '/home/product-desc',  component: () => import('../pages/product-desc.vue'),            meta: { requiresAuth: true } },
  { path: '/home/fashion-matches',component: () => import('../pages/fashion-matches.vue'),        meta: { requiresAuth: true } },
  { path: '/home/ai-report',     component: () => import('../pages/ai-report.vue'),               meta: { requiresAuth: true } },
  { path: '/home/ai-sql',        component: () => import('../pages/ai-sql.vue'),                  meta: { requiresAuth: true } },
  { path: '/home/live-monitor',  component: () => import('../pages/live-monitor.vue'),            meta: { requiresAuth: true } },
  { path: '/home/prompt-optimizer',component: () => import('../pages/prompt-optimizer.vue'),     meta: { requiresAuth: true } },
  { path: '/home/user-simulator',component: () => import('../pages/user-simulator.vue'),          meta: { requiresAuth: true } },
  { path: '/home/user-insight',  component: () => import('../pages/user-insight.vue'),            meta: { requiresAuth: true } },
  { path: '/home/knowledge-graph',component: () => import('../pages/knowledge-graph.vue'),       meta: { requiresAuth: true } },
  { path: '/home/reaction-simulator',component: () => import('../pages/reaction-simulator.vue'), meta: { requiresAuth: true } },
  { path: '/home/regression-test',component: () => import('../pages/regression-test.vue'),       meta: { requiresAuth: true } },
  { path: '/home/stress-test',   component: () => import('../pages/stress-test.vue'),             meta: { requiresAuth: true } },
  { path: '/home/diagram-agent', component: () => import('../pages/diagram-agent.vue'),           meta: { requiresAuth: true } },
  { path: '/home/competitor-research',component: () => import('../pages/competitor-research.vue'),meta: { requiresAuth: true } },
  { path: '/home/flow',          component: () => import('../pages/flow.vue'),                    meta: { requiresAuth: true } },
  { path: '/home/history',       component: () => import('../pages/history.vue'),                 meta: { requiresAuth: true } },

  // Workspace
  { path: '/home/resources',     component: () => import('../pages/resources.vue'),               meta: { requiresAuth: true } },
  { path: '/home/notes',         component: () => import('../pages/notes.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/dashboard-note',component: () => import('../pages/dashboard-note.vue'),          meta: { requiresAuth: true } },
  { path: '/home/changelog',     component: () => import('../pages/changelog.vue'),               meta: { requiresAuth: true } },
  { path: '/home/guide',         component: () => import('../pages/guide.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/roadmap',       component: () => import('../pages/roadmap.vue'),                 meta: { requiresAuth: true } },
  { path: '/home/save',          component: () => import('../pages/save.vue'),                    meta: { requiresAuth: true } },

  // Dev Tools
  { path: '/home/tag-search',    component: () => import('../pages/tag-search.vue'),              meta: { requiresAuth: true } },
  { path: '/home/sku-search',    component: () => import('../pages/sku-search.vue'),              meta: { requiresAuth: true } },
  { path: '/home/store-search',  component: () => import('../pages/store-search.vue'),            meta: { requiresAuth: true } },
  { path: '/home/image-search',  component: () => import('../pages/image-search.vue'),            meta: { requiresAuth: true } },
  { path: '/home/lead-flow',     component: () => import('../pages/lead-flow.vue'),               meta: { requiresAuth: true } },
  { path: '/home/lead-history',  component: () => import('../pages/lead-history-dashboard.vue'),  meta: { requiresAuth: true } },
  { path: '/home/cache',         component: () => import('../pages/cache.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/limit',         component: () => import('../pages/limit.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/merge-history', component: () => import('../pages/merge-history.vue'),           meta: { requiresAuth: true } },
  { path: '/home/experiment-log',component: () => import('../pages/experiment-log.vue'),          meta: { requiresAuth: true } },
  { path: '/home/experiment/:id',component: () => import('../pages/experiment_detail.vue'),       meta: { requiresAuth: true } },
  { path: '/home/ai-analytic-guide',component: () => import('../pages/ai-analytic-guide.vue'),   meta: { requiresAuth: true } },
  { path: '/home/product-guide', component: () => import('../pages/product-guide.vue'),           meta: { requiresAuth: true } },
  { path: '/home/test-db',       component: () => import('../pages/test_db.vue'),                 meta: { requiresAuth: true } },
  { path: '/home/test-sql',      component: () => import('../pages/test_sql.vue'),                meta: { requiresAuth: true } },
  { path: '/home/feedback-demo', component: () => import('../pages/feedback_demo.vue'),           meta: { requiresAuth: true } },
  { path: '/home/limit',         component: () => import('../pages/limit.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/notes',         component: () => import('../pages/notes.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/changelog',     component: () => import('../pages/changelog.vue'),               meta: { requiresAuth: true } },
  { path: '/home/guide',         component: () => import('../pages/guide.vue'),                   meta: { requiresAuth: true } },
  { path: '/home/roadmap',       component: () => import('../pages/roadmap.vue'),                 meta: { requiresAuth: true } },
  { path: '/home/save',          component: () => import('../pages/save.vue'),                    meta: { requiresAuth: true } },

  // Legacy redirects (giữ backwards compatibility)
  { path: '/product',            redirect: '/home/product' },
  { path: '/product-desc',       redirect: '/home/product-desc' },
  { path: '/fashion-matches',    redirect: '/home/fashion-matches' },
  { path: '/ai-report',          redirect: '/home/ai-report' },
  { path: '/ai-sql',             redirect: '/home/ai-sql' },
  { path: '/live-monitor',       redirect: '/home/live-monitor' },
  { path: '/prompt-optimizer',   redirect: '/home/prompt-optimizer' },
  { path: '/user-simulator',     redirect: '/home/user-simulator' },
  { path: '/user-insight',       redirect: '/home/user-insight' },
  { path: '/knowledge-graph',    redirect: '/home/knowledge-graph' },
  { path: '/reaction-simulator', redirect: '/home/reaction-simulator' },
  { path: '/regression-test',    redirect: '/home/regression-test' },
  { path: '/stress-test',        redirect: '/home/stress-test' },
  { path: '/diagram-agent',      redirect: '/home/diagram-agent' },
  { path: '/competitor-research',redirect: '/home/competitor-research' },
  { path: '/flow',               redirect: '/home/flow' },
  { path: '/history',            redirect: '/home/history' },
  { path: '/lead-flow',          redirect: '/home/lead-flow' },
  { path: '/lead_flow',          redirect: '/home/lead-flow' },
  { path: '/lead-history',       redirect: '/home/lead-history' },
  { path: '/cache',              redirect: '/home/cache' },
  { path: '/notes',              redirect: '/home/notes' },
  { path: '/resources',          redirect: '/home/resources' },
  { path: '/changelog',          redirect: '/home/changelog' },
  { path: '/guide',              redirect: '/home/guide' },
  { path: '/roadmap',            redirect: '/home/roadmap' },
  { path: '/save',               redirect: '/home/save' },
  { path: '/tag-search',         redirect: '/home/tag-search' },
  { path: '/sku-search',         redirect: '/home/sku-search' },
  { path: '/store-search',       redirect: '/home/store-search' },
  { path: '/image-search',       redirect: '/home/image-search' },
  { path: '/merge-history',      redirect: '/home/merge-history' },
  { path: '/experiment-log',     redirect: '/home/experiment-log' },

  // ── Catch-all: redirect unknown routes → /home ────────────────────────────
  { path: '/:pathMatch(.*)*', redirect: '/home' },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// ── GLOBAL GUARD ──────────────────────────────────────────────────────────────
router.beforeEach(async (to) => {
  if (DISABLE_AUTH) return true
  if (!to.meta.requiresAuth) return true

  const clerk = window.Clerk
  if (!clerk) return true

  const isSignedIn = !!clerk.user
  if (!isSignedIn) return { path: '/sign-in' }

  return true
})

export default router
