import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { canAccessOps, defaultRouteForRole, isDashboardRole, roleCanAccessRoute } from '@/composables/roleAccess'

const CLINICAL = ['NURSE', 'PHYSICIAN', 'ADMIN']

const routes = [
  {
    path: '/',
    redirect: '/ward',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/LoginView.vue'),
    meta: { title: 'Sign In', public: true },
  },
  {
    path: '/ward',
    name: 'WardDashboard',
    component: () => import('@/views/WardDashboard.vue'),
    meta: { title: 'Virtual Ward', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/departments',
    name: 'DepartmentOverview',
    component: () => import('@/views/DepartmentOverviewView.vue'),
    meta: { title: 'Department Overview', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/patients/:encounterId',
    name: 'PatientDetail',
    component: () => import('@/views/PatientDetail.vue'),
    meta: { title: 'Patient Detail', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/alerts',
    name: 'AlertCenter',
    component: () => import('@/views/AlertCenter.vue'),
    meta: { title: 'Alert Center', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/sepsis',
    name: 'SepsisBoard',
    component: () => import('@/views/SepsisBoardView.vue'),
    meta: { title: 'Sepsis Bundle Board', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/admin/thresholds',
    name: 'ThresholdManagement',
    component: () => import('@/views/ThresholdManagementView.vue'),
    meta: { title: 'Alert Thresholds', layout: 'default', allowedRoles: ['ADMIN'] },
  },
  {
    path: '/admin/users',
    name: 'UserManagement',
    component: () => import('@/views/UserManagementView.vue'),
    meta: { title: 'User Management', layout: 'default', allowedRoles: ['ADMIN'] },
  },
  {
    path: '/admin/audit',
    name: 'AuditLog',
    component: () => import('@/views/AuditLogView.vue'),
    meta: { title: 'Audit Logs', layout: 'default', allowedRoles: ['ADMIN'] },
  },
  {
    path: '/admin/reconciliation',
    name: 'Reconciliation',
    component: () => import('@/views/ReconciliationView.vue'),
    meta: { title: 'Data Quality', layout: 'default', allowedRoles: CLINICAL },
  },
  {
    path: '/operations/gateways',
    name: 'GatewayOperations',
    component: () => import('@/views/GatewayOperations.vue'),
    meta: { title: 'Gateway Operations', layout: 'default', opsRoute: true },
  },
  {
    path: '/analytics/alerts',
    name: 'AlertQualityAnalytics',
    component: () => import('@/views/AlertQualityAnalytics.vue'),
    meta: { title: 'Alert Quality', layout: 'default' },
  },
  {
    path: '/feedback',
    redirect: '/analytics/alerts',
  },
]

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

router.beforeEach((to) => {
  document.title = `${to.meta.title ?? 'VigilCare'} — VigilCare`

  const auth = useAuthStore()
  if (!to.meta.public && !auth.isAuthenticated) {
    return { path: '/login', query: { redirect: to.fullPath } }
  }
  if (to.path === '/login' && auth.isAuthenticated && isDashboardRole(auth.role)) {
    return { path: defaultRouteForRole(auth.role) }
  }
  if (!to.meta.public && auth.isAuthenticated && to.meta.opsRoute && !canAccessOps(auth.role)) {
    return { path: defaultRouteForRole(auth.role) }
  }
  if (!to.meta.public && auth.isAuthenticated && !roleCanAccessRoute(auth.role, to.meta)) {
    return { path: defaultRouteForRole(auth.role) }
  }
})

export default router
