import { Navigate, Outlet } from 'react-router-dom';

import { useUserContext } from '@/contexts/UserContext';

export function AuthLayout() {
  const { isAuthenticated, isLoading } = useUserContext();

  if (isLoading) {
    return (
      <div className="flex min-h-screen items-center justify-center">
        <div className="h-6 w-6 animate-spin rounded-full border-2 border-primary border-t-transparent" />
      </div>
    );
  }

  if (isAuthenticated) {
    return <Navigate to="/" replace />;
  }
  return (
    <div className="relative flex min-h-screen flex-col items-center justify-center overflow-hidden bg-[#fafafa] px-4 py-12 sm:px-6">
      {/* Background pattern */}
      <div
        aria-hidden
        className="pointer-events-none absolute inset-0"
        style={{
          backgroundImage: 'radial-gradient(circle, oklch(0.88 0 0 / 0.5) 1px, transparent 1px)',
          backgroundSize: '24px 24px',
        }}
      />

      {/* Subtle top glow */}
      <div
        aria-hidden
        className="pointer-events-none absolute -top-40 left-1/2 size-[600px] -translate-x-1/2 rounded-full bg-primary/[0.03] blur-[100px]"
      />

      {/* Content island */}
      <div className="relative z-10 w-full max-w-[440px]">
        {/* Card */}
        <div className="rounded-2xl border border-border/60 bg-card px-6 py-8 shadow-[0_1px_3px_rgba(0,0,0,0.04),0_8px_32px_rgba(0,0,0,0.04)] sm:px-8 sm:py-10">
          <Outlet />
        </div>

        {/* Footer */}
        <p className="mt-8 text-center text-[12px] text-muted-foreground/70">
          &copy; {new Date().getFullYear()} GitDataAi. All rights reserved.
        </p>
      </div>
    </div>
  );
}
