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

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/LoginWrapper.vue'),
    meta: { requiresAuth: false, title: 'Autentificare - ROA2WEB' }
  },
  {
    path: '/reports',
    component: () => import('@/modules/reports/ReportsLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@reports/views/DashboardView.vue'),
        meta: { requiresAuth: true, title: 'Dashboard - ROA2WEB' }
      },
      {
        path: 'invoices',
        name: 'Invoices',
        component: () => import('@reports/views/InvoicesView.vue'),
        meta: { requiresAuth: true, title: 'Facturi - ROA2WEB' }
      },
      {
        path: 'bank-cash',
        name: 'BankCash',
        component: () => import('@reports/views/BankCashRegisterView.vue'),
        meta: { requiresAuth: true, title: 'Casa și Banca - ROA2WEB' }
      },
      {
        path: 'trial-balance',
        name: 'TrialBalance',
        component: () => import('@reports/views/TrialBalanceView.vue'),
        meta: { requiresAuth: true, title: 'Balanță de Verificare - ROA2WEB' }
      },
      {
        path: 'telegram',
        name: 'Telegram',
        component: () => import('@reports/views/TelegramView.vue'),
        meta: { requiresAuth: true, title: 'Telegram Bot - ROA2WEB' }
      },
      {
        path: 'cache-stats',
        name: 'CacheStats',
        component: () => import('@reports/views/CacheStatsView.vue'),
        meta: { requiresAuth: true, title: 'Statistici Cache - ROA2WEB' }
      },
      {
        path: 'server-logs',
        name: 'ServerLogs',
        component: () => import('@reports/views/ServerLogsView.vue'),
        meta: { requiresAuth: true, title: 'Server Logs - ROA2WEB' }
      }
    ]
  },
  {
    path: '/data-entry',
    component: () => import('@/modules/data-entry/DataEntryLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'ReceiptsList',
        component: () => import('@data-entry/views/receipts/ReceiptsListView.vue'),
        meta: { requiresAuth: true, title: 'Lista Bonuri - ROA2WEB' }
      },
      {
        path: 'create',
        name: 'ReceiptCreate',
        component: () => import('@data-entry/views/receipts/ReceiptCreateUnifiedView.vue'),
        meta: { requiresAuth: true, title: 'Bon Nou - ROA2WEB' }
      },
      {
        path: 'create-old',
        name: 'ReceiptCreateOld',
        component: () => import('@data-entry/views/receipts/ReceiptCreateView.vue'),
        meta: { requiresAuth: true, title: 'Bon Nou (Legacy) - ROA2WEB' }
      },
      {
        path: ':id',
        name: 'ReceiptDetail',
        component: () => import('@data-entry/views/receipts/ReceiptCreateUnifiedView.vue'),
        meta: { requiresAuth: true, title: 'Detalii Bon - ROA2WEB' }
      },
      {
        path: ':id/edit',
        name: 'ReceiptEdit',
        component: () => import('@data-entry/views/receipts/ReceiptCreateUnifiedView.vue'),
        meta: { requiresAuth: true, title: 'Editare Bon - ROA2WEB' }
      },
      {
        path: 'ocr-metrics',
        name: 'OCRMetrics',
        component: () => import('@data-entry/views/OCRMetricsView.vue'),
        meta: { requiresAuth: true, title: 'Metrici OCR - ROA2WEB' }
      },
      {
        // US-013: Redirect old bulk-upload route to main list (backwards compatibility)
        path: 'bulk-upload',
        redirect: '/data-entry'
      }
    ]
  },
  {
    path: '/',
    redirect: '/reports/dashboard'
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/reports/dashboard'
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// Navigation guard for authentication
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('access_token')

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else if (to.path === '/login' && isAuthenticated) {
    next('/reports/dashboard')
  } else {
    next()
  }
})

// Set page title after navigation
router.afterEach((to) => {
  document.title = to.meta.title || 'ROA2WEB'
  window.scrollTo(0, 0)
})

export default router
