// components/common/AuthGuard.jsx - Simpler Version
import React from 'react'
import { Navigate, Outlet } from 'react-router-dom'
import { useAuth } from '../../contexts/AuthContext'

const AuthGuard = () => {
  const { user, loading, isAuthenticated } = useAuth()

  // Show loading spinner while checking authentication
  if (loading) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-gray-50">
        <div className="text-center">
          <div className="animate-spin rounded-full h-16 w-16 border-b-2 border-green-600 mx-auto mb-4"></div>
          <p className="text-gray-600 text-lg">Loading...</p>
          <p className="text-gray-500 text-sm mt-2">Please wait while we verify your authentication</p>
        </div>
      </div>
    )
  }

  // If not authenticated, redirect to home
  if (!isAuthenticated()) {
    return <Navigate to="/home" replace />
  }

  // Render nested routes
  return <Outlet />
}

export default AuthGuard