import { Navigate } from 'react-router-dom'
import { useAuthStore } from '../stores/auth'

export default function RouteGuard({ children }: { children: React.ReactNode }) {
  const { state } = useAuthStore()

  if (state === 'loading') {
    return (
      <div className="flex min-h-screen items-center justify-center">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-accent"></div>
      </div>
    )
  }

  if (state === 'unauthenticated' || state === 'error') {
    return <Navigate to="/login" replace />
  }

  return <>{children}</>
}
