import { describe, it, expect, beforeEach } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import {
  canAccessOps,
  filterNavLinks,
  isDashboardRole,
  roleCanAccessRoute,
  MAIN_NAV_LINKS,
  useRoleAccess,
} from '@/composables/roleAccess'
import { useAuthStore } from '@/stores/auth'
import { useSimulationStore } from '@/stores/simulation'

describe('roleAccess', () => {
  it('filtersNavLinksByRole', () => {
    const nurseLinks = filterNavLinks(MAIN_NAV_LINKS, 'NURSE')
    expect(nurseLinks.some((l) => l.to === '/analytics/alerts')).toBe(true)
    expect(nurseLinks.some((l) => l.to === '/alerts')).toBe(true)

    const physicianLinks = filterNavLinks(MAIN_NAV_LINKS, 'PHYSICIAN')
    expect(physicianLinks.some((l) => l.to === '/analytics/alerts')).toBe(true)
  })

  it('rejectsIntegrationDashboardRole', () => {
    expect(isDashboardRole('INTEGRATION')).toBe(false)
    expect(isDashboardRole('NURSE')).toBe(true)
  })

  it('guardsRoutesByAllowedRoles', () => {
    expect(roleCanAccessRoute('NURSE', { allowedRoles: ['PHYSICIAN', 'ADMIN'] })).toBe(false)
    expect(roleCanAccessRoute('ADMIN', { allowedRoles: ['ADMIN'] })).toBe(true)
    expect(roleCanAccessRoute('INTEGRATION', { allowedRoles: ['NURSE'] })).toBe(false)
  })

  it('restrictsOpsAccessToAdminByDefault', () => {
    expect(canAccessOps('ADMIN')).toBe(true)
    expect(canAccessOps('NURSE')).toBe(false)
    expect(canAccessOps('PHYSICIAN')).toBe(false)
    expect(canAccessOps('INTEGRATION')).toBe(false)
  })
})

describe('useRoleAccess simulation nav', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    const auth = useAuthStore()
    auth.token = 'tok'
    auth.user = {
      userId: '1',
      username: 'nurse',
      displayName: 'Nurse',
      role: 'NURSE',
    }
  })

  it('includesSimulationLinkOnlyWhenEnabled', () => {
    const sim = useSimulationStore()
    const { mainNavLinks } = useRoleAccess()

    sim.enabled = false
    expect(mainNavLinks.value.some(l => l.to === '/simulation')).toBe(false)

    sim.enabled = true
    expect(mainNavLinks.value.some(l => l.to === '/simulation')).toBe(true)
  })

  it('omitsSimulationLinkWhenDisabledRegardlessOfRole', () => {
    const auth = useAuthStore()
    const sim = useSimulationStore()
    sim.enabled = false

    for (const role of ['NURSE', 'PHYSICIAN', 'ADMIN']) {
      auth.user = { ...auth.user, role }
      const { mainNavLinks } = useRoleAccess()
      expect(mainNavLinks.value.some(l => l.to === '/simulation')).toBe(false)
    }
  })

  it('movesQualityToOverflowWhenSimulationEnabledOnMobile', () => {
    const sim = useSimulationStore()
    const { mobileNavLinks, mobileOverflowLinks } = useRoleAccess()

    sim.enabled = true
    expect(mobileNavLinks.value.some(l => l.to === '/simulation')).toBe(true)
    expect(mobileNavLinks.value.some(l => l.to === '/admin/reconciliation')).toBe(false)
    expect(mobileOverflowLinks.value.some(l => l.to === '/admin/reconciliation')).toBe(true)
  })
})
