/**
 * Vue Router configuration.
 *
 * Routes are organized by layout type (minimal, default, game) and
 * authentication requirements. Guards are applied globally based on
 * route meta properties.
 */
import { createRouter, createWebHistory } from 'vue-router'

import type { RouteRecordRaw } from 'vue-router'

import { requireAuth, requireGuest, requireStarter } from './guards'

const routes: RouteRecordRaw[] = [
  // ============================================
  // Minimal Layout Routes (no navigation)
  // ============================================
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/LoginPage.vue'),
    meta: { requiresGuest: true, layout: 'minimal' },
  },
  {
    path: '/auth/callback',
    name: 'AuthCallback',
    component: () => import('@/pages/AuthCallbackPage.vue'),
    meta: { layout: 'minimal' },
  },
  {
    path: '/starter',
    name: 'StarterSelection',
    component: () => import('@/pages/StarterSelectionPage.vue'),
    meta: { requiresAuth: true, layout: 'minimal' },
  },

  // ============================================
  // Default Layout Routes (sidebar/bottom tabs)
  // ============================================
  {
    path: '/',
    name: 'Dashboard',
    component: () => import('@/pages/HomePage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/collection',
    name: 'Collection',
    component: () => import('@/pages/CollectionPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/decks',
    name: 'DeckList',
    component: () => import('@/pages/DecksPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/decks/new',
    name: 'NewDeck',
    component: () => import('@/pages/DeckBuilderPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/decks/:id',
    name: 'EditDeck',
    component: () => import('@/pages/DeckBuilderPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/play',
    name: 'PlayMenu',
    component: () => import('@/pages/PlayPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/pages/ProfilePage.vue'),
    meta: { requiresAuth: true, layout: 'default' },
  },

  // ============================================
  // Game Layout Routes (full viewport, no nav)
  // ============================================
  {
    path: '/game/:id',
    name: 'Game',
    component: () => import('@/pages/GamePage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'game' },
  },

  // ============================================
  // Future Routes (Campaign - Phase 5+)
  // ============================================
  {
    path: '/campaign',
    name: 'Campaign',
    component: () => import('@/pages/CampaignPage.vue'),
    meta: { requiresAuth: true, requiresStarter: true, layout: 'default' },
  },
]

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

// Global navigation guards
router.beforeEach((to, from, next) => {
  // Check guest-only routes (e.g., login page)
  if (to.meta.requiresGuest) {
    requireGuest(to, from, next)
    return
  }

  // Check auth-required routes
  if (to.meta.requiresAuth) {
    requireAuth(to, from, (result) => {
      // If requireAuth redirected, stop here
      if (result !== undefined && result !== true) {
        next(result)
        return
      }

      // Check starter deck requirement
      if (to.meta.requiresStarter) {
        requireStarter(to, from, next)
      } else {
        next()
      }
    })
    return
  }

  // No guards needed
  next()
})

export default router
