import { useEffect } from 'react'
import { useSearchParams, useNavigate } from 'react-router-dom'

export default function AuthCallback({ onLogin }) {
  const [searchParams] = useSearchParams()
  const navigate = useNavigate()

  useEffect(() => {
    const token = searchParams.get('token')

    if (token) {
      onLogin(token)
      navigate('/', { replace: true })
    } else {
      // No token received, redirect to login with error
      navigate('/login?error=oauth_failed', { replace: true })
    }
  }, [searchParams, onLogin, navigate])

  return (
    <div className="min-h-screen bg-gray-900 flex items-center justify-center">
      <div className="text-center">
        <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500 mx-auto mb-4"></div>
        <p className="text-gray-400">Anmeldung wird verarbeitet...</p>
      </div>
    </div>
  )
}
