'use client';

import { RotateCcw } from 'lucide-react';
import { useEffect, useState } from 'react';

import { ErrorIllustration } from '@/components/illustrations';
import { Button } from '@/components/ui/button';

/**
 * Full-screen state when the session could not be restored for a transient reason
 * (rate limit, server error, offline). Waits out `retryAfterSeconds` before enabling retry.
 */
export function SessionUnavailableScreen({ retryAfterSeconds, onRetry }: { retryAfterSeconds: number | null; onRetry: () => void }) {
  const [remaining, setRemaining] = useState(Math.max(0, Math.ceil(retryAfterSeconds ?? 0)));
  useEffect(() => {
    if (remaining <= 0) return;
    const timer = window.setTimeout(() => setRemaining((value) => value - 1), 1000);
    return () => window.clearTimeout(timer);
  }, [remaining]);
  const minutes = Math.floor(remaining / 60);
  const wait = minutes > 0 ? `${minutes}m ${String(remaining % 60).padStart(2, '0')}s` : `${remaining}s`;

  return (
    <div className="fixed inset-0 z-[60] grid place-items-center bg-background px-6" role="alert">
      <div className="grid max-w-sm justify-items-center gap-4 text-center">
        <ErrorIllustration size={112} />
        <h1 className="font-display text-h2 font-bold">We couldn&apos;t restore your session</h1>
        <p className="text-small text-muted-foreground">
          {remaining > 0
            ? `The server is busy right now. You can try again in ${wait}.`
            : 'The server could not be reached. You are still signed in, so trying again usually fixes it.'}
        </p>
        <Button type="button" onClick={onRetry} disabled={remaining > 0}>
          <RotateCcw className="h-4 w-4" aria-hidden="true" />
          {remaining > 0 ? `Try again in ${wait}` : 'Try again'}
        </Button>
      </div>
    </div>
  );
}

/** Full-screen placeholder while the session restores. No unauthenticated content flashes behind it. */
export function LoadingScreen({ label = 'Loading your session', fullscreen = true }: { label?: string; fullscreen?: boolean }) {
  return (
    <div
      className={fullscreen ? 'fixed inset-0 z-[60] grid place-items-center bg-background px-6' : 'relative grid min-h-[50vh] place-items-center px-6'}
      role="status"
      aria-live="polite"
    >
      <div className={fullscreen ? 'absolute inset-x-0 top-0 h-0.5 overflow-hidden bg-primary-soft' : 'hidden'} aria-hidden="true">
        <div className="h-full w-full origin-left bg-brand-gradient motion-safe:animate-[progress-indeterminate_1.2s_cubic-bezier(0.2,0.8,0.2,1)_infinite]" />
      </div>
      <div className="grid justify-items-center gap-5">
        <div className="relative grid h-16 w-16 place-items-center" aria-hidden="true">
          <span className="absolute inset-0 rounded-2xl bg-primary/25 motion-safe:animate-[ring-pulse_1.6s_cubic-bezier(0.2,0.8,0.2,1)_infinite]" />
          <span className="relative grid h-14 w-14 place-items-center rounded-2xl bg-brand-gradient text-primary-foreground shadow-md">
            <svg viewBox="0 0 24 24" className="h-7 w-7" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
              <path d="M4 8.5 12 4l8 4.5v7L12 20l-8-4.5v-7Z" />
              <path d="M12 12v8" />
              <path d="M12 12 4 8.5" />
              <path d="m12 12 8-3.5" />
              <path d="M16 7.5 9.5 11" />
            </svg>
          </span>
        </div>
        <span className="text-small text-muted-foreground">{label}</span>
      </div>
    </div>
  );
}
