'use client';

import { useSession } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';

export default function Home() {
  const { data: session, status } = useSession();
  const router = useRouter();

  useEffect(() => {
    if (status === 'loading') return;

    // Redirect based on user role, or to public dashboard if not logged in
    if (session) {
      const userRole = (session.user as any)?.role;
      if (userRole === 'AGENT') {
        router.replace('/agent/dashboard');
      } else {
        router.replace('/user/dashboard');
      }
    } else {
      // Not logged in - go to public user dashboard
      router.replace('/user/dashboard');
    }
  }, [session, status, router]);

  // Loading state while determining redirect
  return (
    <div className="min-h-screen bg-gradient-to-b from-[#c4d9d4] to-[#f0f5fc] flex items-center justify-center">
      <div className="text-center">
        <img
          src="/assets/logo.svg"
          alt="RE-Quest"
          className="h-10 mx-auto mb-4"
        />
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d] mx-auto"></div>
        <p className="mt-4 text-[#00293d]/70">Loading...</p>
      </div>
    </div>
  );
}
