'use client';

import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/components/id/auth-provider';

export default function AdminLayout({ children }: { children: React.ReactNode }) {
  const router = useRouter();
  const { user, isLoading, isPinLocked, hasStoredSession } = useAuth();

  useEffect(() => {
    if (isLoading) return;
    if (!user && !isPinLocked && !hasStoredSession) {
      router.replace('/auth/login');
      return;
    }
    if (user && !user.canAccessAdmin) {
      router.replace('/');
    }
  }, [hasStoredSession, isLoading, isPinLocked, router, user]);

  return children;
}
