import { Navigate } from 'react-router-dom'
import { useAuth } from '../context/AuthContext'

interface AdminRouteProps {
  children: React.ReactNode
  featureKey?: string
}

export default function AdminRoute({ children, featureKey }: AdminRouteProps) {
  const { isAuthenticated, isLoading, isAdmin, hasPermission } = useAuth()

  if (isLoading) {
    return <div className="min-h-screen flex items-center justify-center">Loading...</div>
  }

  if (!isAuthenticated) {
    return <Navigate to="/login" replace />
  }

  if (featureKey) {
    if (!hasPermission(featureKey)) {
      return <Navigate to="/" replace />
    }
  } else if (!isAdmin) {
    return <Navigate to="/" replace />
  }

  return <>{children}</>
}

