// src/components/ProtectedRoute.tsx
'use client'

import { usePathname, useRouter } from 'next/navigation'
import { useEffect } from 'react'
import { useAuth } from '@/hooks'
import FallbackSpinner from '../spinner'
import { AccountType } from '@/types/account-type.enum'

const ProtectedRoute = ({ children, allowedRoles }: { children: React.ReactNode; allowedRoles: AccountType[] }) => {
  const { loading } = useAuth()
  const router = useRouter()
  const pathname = usePathname()

  useEffect(() => {
    if (!loading) {
      router.replace('/unauthorized')
    }
  }, [pathname, loading])

  if (loading) {
    return <FallbackSpinner />
  }

  return <>{children}</>
}

export default ProtectedRoute
