import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: { guest: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/auth/Register.vue'),
    meta: { guest: true }
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: () => import('@/views/auth/ForgotPassword.vue'),
    meta: { guest: true }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: { requiresAuth: true }
  },
  // Admin routes
  {
    path: '/admin/tenants',
    name: 'Tenants',
    component: () => import('@/views/admin/Tenants.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  {
    path: '/admin/tenants/:id',
    name: 'TenantEdit',
    component: () => import('@/views/admin/TenantEdit.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  {
    path: '/admin/modules',
    name: 'Modules',
    component: () => import('@/views/admin/Modules.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  {
    path: '/admin/audit-logs',
    name: 'AuditLogs',
    component: () => import('@/views/admin/AuditLogs.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  {
    path: '/admin/invoices',
    name: 'Invoices',
    component: () => import('@/views/admin/Invoices.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  {
    path: '/admin/admins',
    name: 'PlatformAdmins',
    component: () => import('@/views/admin/PlatformAdmins.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  // Tenant routes
  {
    path: '/branches',
    name: 'Branches',
    component: () => import('@/views/tenant/Branches.vue'),
    meta: { requiresAuth: true, module: 'office' }
  },
  {
    path: '/users',
    name: 'Users',
    component: () => import('@/views/tenant/Users.vue'),
    meta: { requiresAuth: true, module: 'office' }
  },
  {
    path: '/calendar',
    name: 'Calendar',
    component: () => import('@/views/tenant/Calendar.vue'),
    meta: { requiresAuth: true, module: 'calendar' }
  },
  {
    path: '/messenger',
    name: 'Messenger',
    component: () => import('@/views/tenant/Messenger.vue'),
    meta: { requiresAuth: true, module: 'messenger' }
  },
  {
    path: '/learning',
    name: 'Learning',
    component: () => import('@/views/tenant/Learning.vue'),
    meta: { requiresAuth: true, module: ['learning_blank', 'learning_premium'] }
  },
  {
    path: '/files',
    name: 'Files',
    component: () => import('@/views/tenant/Files.vue'),
    meta: { requiresAuth: true, module: 'office' }
  },
  {
    path: '/branding',
    name: 'Branding',
    component: () => import('@/views/tenant/Branding.vue'),
    meta: { requiresAuth: true, adminOnly: true }
  },
  // Catch all
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

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

router.beforeEach((to, from, next) => {
  const auth = useAuthStore()
  
  if (to.meta.requiresAuth && !auth.isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
    return
  }
  
  if (to.meta.adminOnly && !auth.isPlatformAdmin) {
    next({ name: 'Dashboard' })
    return
  }
  
  if (to.meta.guest && auth.isAuthenticated) {
    next({ name: 'Dashboard' })
    return
  }
  
  next()
})

export default router