'use client';

import React, { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/contexts/AuthContext';
import LoadingSpinner from '@/components/LoadingSpinner';
import Image from "next/image";

export default function HomePage() {
    const router = useRouter();
    const { isAuthenticated, isLoading, user } = useAuth();

    useEffect(() => {
        if (!isLoading) {
            if (isAuthenticated) {
                if (user?.role === 'admin') {
                    router.replace('/admin/dashboard');
                } else {
                    router.replace('/dashboard');
                }
            } else {
                router.replace('/login');
            }
        }
    }, [isLoading, isAuthenticated, user, router]);

    // Display a loading spinner while determining auth state and redirecting
    return (
        <div className="flex items-center justify-center min-h-screen">
            <LoadingSpinner />
        </div>
    );
}
