import React, { useEffect } from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from '../hooks/AuthProvider';
import { Skeleton } from '@/client/components/ui/skeleton';
import { Card, CardContent } from '@/client/components/ui/card';

export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  const { isAuthenticated, isLoading } = useAuth();
  const navigate = useNavigate();
  
  useEffect(() => {
    // 只有在加载完成且未认证时才重定向
    if (!isLoading && !isAuthenticated) {
      navigate('/login', { replace: true });
    }
  }, [isAuthenticated, isLoading, navigate]);
  
  // 显示加载状态，直到认证检查完成
  if (isLoading) {
    return (
      <div className="flex justify-center items-center min-h-screen bg-gradient-to-br from-slate-50 to-slate-100">
        <Card className="border-0 shadow-lg">
          <CardContent className="flex flex-col items-center space-y-4 py-12 px-8">
            <div className="relative">
              <Skeleton className="h-12 w-12 rounded-full" />
              <div className="absolute inset-0 rounded-full bg-primary/20 animate-pulse" />
            </div>
            <div className="space-y-2 text-center">
              <Skeleton className="h-4 w-32" />
              <Skeleton className="h-3 w-24" />
            </div>
          </CardContent>
        </Card>
      </div>
    );
  }
  
  // 如果未认证且不再加载中，不显示任何内容（等待重定向）
  if (!isAuthenticated) {
    return null;
  }
  
  return children;
};