import type { ReactNode } from 'react'
import type { UserRole } from '../../domain/enums'
import { useSessionStore } from '../../stores/sessionStore'
import { AccessDenied } from './AccessDenied'

interface RoleGuardProps {
  roles: UserRole[]
  fallback?: ReactNode
  children: ReactNode
}

export function RoleGuard({ roles, fallback, children }: RoleGuardProps) {
  const { currentUser } = useSessionStore()

  if (!currentUser || !roles.includes(currentUser.role)) {
    return <>{fallback ?? <AccessDenied />}</>
  }

  return <>{children}</>
}
