import { headers } from 'next/headers';
import { redirect } from 'next/navigation';
import { createRouteGuard } from '@/lib/auth/createRouteGuard';
import { AuthContainer } from '@/ui/AuthContainer';

interface AuthLayoutProps {
  children: React.ReactNode;
}

/**
 * Auth Layout
 *
 * Provides authentication route protection for all auth routes.
 * Uses RouteGuard to enforce access control server-side.
 *
 * Behavior:
 * - Unauthenticated users can access auth pages (login, signup, etc.)
 * - Authenticated users are redirected away from auth pages
 */
export default async function AuthLayout({ children }: AuthLayoutProps) {
  const headerStore = await headers();
  const pathname = headerStore.get('x-pathname') || '/';
  
  const guard = createRouteGuard();
  const result = await guard.enforce({ pathname });
  if (result.type === 'redirect') {
    redirect(result.to);
  }
  
  return <AuthContainer>{children}</AuthContainer>;
}
