'use client';

import { Loader2 } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';

import OnboardingWizard from '@/components/onboarding/OnboardingWizard';
import { useCurrentDriver } from '@/hooks/useDriverService';
import { useAuth } from '@/lib/auth/AuthContext';
 
export default function OnboardingPage() {
  const router = useRouter();
  const { session } = useAuth();
  const { data: driver, isLoading } = useCurrentDriver();

  const shouldRedirectToLogin = !session;
  const shouldRedirectToDashboard = !isLoading && Boolean(driver);

  useEffect(() => {
    if (shouldRedirectToLogin) {
      router.replace('/auth/login?returnTo=/onboarding');
      return;
    }
    if (shouldRedirectToDashboard) {
      router.replace('/dashboard');
    }
  }, [router, shouldRedirectToLogin, shouldRedirectToDashboard]);

  if (shouldRedirectToLogin) {
    return null;
  }

  if (isLoading) {
    return (
      <main className="min-h-screen bg-deep-graphite flex items-center justify-center">
        <Loader2 className="w-8 h-8 text-primary-blue animate-spin" />
      </main>
    );
  }

  if (shouldRedirectToDashboard) {
    return null;
  }

  return (
    <main className="min-h-screen bg-deep-graphite">
      <OnboardingWizard />
    </main>
  );
}