import { AlertTriangle, RotateCw } from 'lucide-react';
import Link from 'next/link';
import type { ReactNode } from 'react';

import { ErrorIllustration, NoResultsIllustration } from '@/components/illustrations';
import { Button, buttonVariants } from '@/components/ui/button';
import { cn } from '@/lib/utils';

export function EmptyState({
  title,
  description,
  illustration,
  action,
  compact = false,
}: {
  title: string;
  description?: string;
  illustration?: ReactNode;
  action?: ReactNode;
  compact?: boolean;
}) {
  return (
    <div className={cn('grid justify-items-center text-center', compact ? 'gap-2 py-6' : 'gap-3 py-12')}>
      {illustration}
      <p className={cn('font-semibold text-foreground', compact ? 'text-small' : 'font-display text-h3')}>{title}</p>
      {description === undefined ? null : <p className="max-w-sm text-small text-muted-foreground">{description}</p>}
      {action === undefined ? null : <div className="mt-1">{action}</div>}
    </div>
  );
}

export function ErrorState({
  message,
  requestId,
  onRetry,
  title = 'Something went wrong',
}: {
  message: string;
  requestId?: string;
  onRetry?: () => void;
  title?: string;
}) {
  return (
    <div role="alert" className="grid justify-items-center gap-2 py-8 text-center">
      <span className="grid h-10 w-10 place-items-center rounded-full bg-danger-soft text-danger">
        <AlertTriangle className="h-5 w-5" aria-hidden="true" />
      </span>
      <p className="text-small font-semibold text-foreground">{title}</p>
      <p className="max-w-sm text-small text-muted-foreground">{message}</p>
      {onRetry === undefined ? null : (
        <Button type="button" variant="secondary" size="sm" className="mt-1" onClick={onRetry}>
          <RotateCw className="h-3.5 w-3.5" aria-hidden="true" />
          Retry
        </Button>
      )}
      {requestId === undefined ? null : <p className="font-mono text-caption text-subtle-foreground">Request {requestId}</p>}
    </div>
  );
}

function FrameState({ illustration, title, text }: { illustration: ReactNode; title: string; text: string }) {
  return (
    <div role="alert" className="grid justify-items-center rounded-xl border border-border bg-surface px-6 py-14 text-center shadow-xs">
      {illustration}
      <h1 className="mt-4 font-display text-h2 font-bold">{title}</h1>
      <p className="mt-2 max-w-md text-body text-muted-foreground">{text}</p>
      <Link href="/dashboard" className={cn(buttonVariants({ size: 'lg' }), 'mt-6')}>
        Back to dashboard
      </Link>
    </div>
  );
}

export function ForbiddenState() {
  return (
    <FrameState
      illustration={<ErrorIllustration size={120} />}
      title="You do not have access to this page"
      text="Your role does not include this permission. Ask a workspace owner or admin if you need it."
    />
  );
}

export function NotFoundState({ title = 'We could not find that page', text = 'The link may be broken, or the item may have been removed.' }: { title?: string; text?: string }) {
  return <FrameState illustration={<NoResultsIllustration size={120} />} title={title} text={text} />;
}
