'use client';

import { useRouter } from 'next/navigation';
import { useAuthContext } from '@/providers/AuthProvider';
import UserProfileSettings from '@/components/UserProfileSettings';
import { useEffect } from 'react';

export default function ProfilePage() {
    const router = useRouter();
    const { user, loading, updateProfile } = useAuthContext();

    useEffect(() => {
        if (!loading && !user) {
            router.replace('/login');
        }
    }, [user, loading, router]);

    if (loading || !user) return null;

    return (
        <UserProfileSettings
            user={user}
            onUpdate={async (updates) => {
                try {
                    await updateProfile(updates);
                } catch (err) {
                    // Handled inside updateProfile (console.error)
                }
            }}
            onBack={() => router.push('/dashboard')}
        />
    );
}
