import { computed } from 'vue'
import { storeToRefs } from 'pinia'
import { useAuthStore } from '@/stores/auth'

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

export const OPS_NAV_LINK = {
  to: '/operations/gateways',
  label: 'Operations',
  icon: 'ops',
}

export const MAIN_NAV_LINKS = [
  { to: '/ward', label: 'Virtual Ward', icon: 'ward', roles: CLINICAL_ROLES },
  { to: '/departments', label: 'Departments', icon: 'departments', roles: CLINICAL_ROLES },
  { to: '/sepsis', label: 'Sepsis Board', icon: 'sepsis', roles: CLINICAL_ROLES },
  { to: '/alerts', label: 'Alert Center', icon: 'alerts', roles: CLINICAL_ROLES },
  { to: '/analytics/alerts', label: 'Alert Quality', icon: 'analytics', roles: CLINICAL_ROLES },
  { to: '/admin/reconciliation', label: 'Data Quality', icon: 'reconciliation', roles: CLINICAL_ROLES },
]

export function canAccessOps(role) {
  if (import.meta.env.VITE_SHOW_OPS === 'true') return isDashboardRole(role)
  return role === 'ADMIN'
}

export const ADMIN_NAV_LINKS = [
  { to: '/admin/thresholds', label: 'Alert Thresholds', icon: 'admin', roles: ['ADMIN'] },
  { to: '/admin/users', label: 'User Management', icon: 'users', roles: ['ADMIN'] },
  { to: '/admin/audit', label: 'Audit Logs', icon: 'audit', roles: ['ADMIN'] },
]

export const MOBILE_NAV_LINKS = [
  { to: '/ward', label: 'Ward', icon: 'ward', roles: CLINICAL_ROLES },
  { to: '/alerts', label: 'Alerts', icon: 'alerts', roles: CLINICAL_ROLES },
  { to: '/sepsis', label: 'Sepsis', icon: 'sepsis', roles: CLINICAL_ROLES },
  { to: '/admin/reconciliation', label: 'Quality', icon: 'reconciliation', roles: CLINICAL_ROLES },
]

export function isDashboardRole(role) {
  return CLINICAL_ROLES.includes(role)
}

export function defaultRouteForRole(role) {
  if (isDashboardRole(role)) return '/ward'
  return '/login'
}

export function roleCanAccessRoute(role, meta = {}) {
  if (meta.public) return true
  if (!role || !isDashboardRole(role)) return false
  if (meta.allowedRoles && !meta.allowedRoles.includes(role)) return false
  return true
}

export function filterNavLinks(links, role) {
  if (!role) return []
  return links.filter((link) => link.roles.includes(role))
}

export function useRoleAccess() {
  const auth = useAuthStore()
  const { role } = storeToRefs(auth)

  const isNurse = computed(() => role.value === 'NURSE')
  const isPhysician = computed(() => role.value === 'PHYSICIAN')
  const isAdmin = computed(() => role.value === 'ADMIN')
  const isClinical = computed(() => isDashboardRole(role.value))

  const mainNavLinks = computed(() => {
    const links = filterNavLinks(MAIN_NAV_LINKS, role.value)
    if (canAccessOps(role.value)) links.push(OPS_NAV_LINK)
    return links
  })
  const adminNavLinks = computed(() => filterNavLinks(ADMIN_NAV_LINKS, role.value))
  const mobileNavLinks = computed(() => filterNavLinks(MOBILE_NAV_LINKS, role.value))
  const showAdminSection = computed(() => adminNavLinks.value.length > 0)

  const nursePatientLayout = computed(() => isNurse.value)
  const physicianPatientLayout = computed(() => isPhysician.value)

  return {
    role,
    isNurse,
    isPhysician,
    isAdmin,
    isClinical,
    mainNavLinks,
    adminNavLinks,
    mobileNavLinks,
    showAdminSection,
    nursePatientLayout,
    physicianPatientLayout,
  }
}
