import type { ReactNode } from "react";
import Link from "next/link";
import { AlertTriangle } from "lucide-react";

/**
 * The shell every section below the search card shares (LEIS-1016 … 1021).
 *
 * Six sections across the two home pages all need the same four things: a
 * heading, an optional link to the full page, and the loading / empty / error
 * states. Writing them six times is how the error copy ends up reassuring on
 * one panel and alarming on the next.
 */
export function HomeSection({
  title,
  viewAll,
  children,
  className = "",
}: {
  title: string;
  viewAll?: { href: string; label: string };
  children: ReactNode;
  className?: string;
}) {
  return (
    <section className={className} aria-label={title}>
      <div className="mb-[12px] flex flex-wrap items-baseline justify-between gap-[8px]">
        <h2 className="m-0 text-[20px] font-bold" style={{ color: "var(--fra-heading)" }}>
          {title}
        </h2>
        {viewAll && (
          <Link
            href={viewAll.href}
            className="text-[14px] font-semibold underline underline-offset-2"
            style={{ color: "var(--fra-navy-2)" }}
          >
            {viewAll.label}
          </Link>
        )}
      </div>
      {children}
    </section>
  );
}

/** A plain card, for the panels that are not lists of other cards. */
export function HomeCard({
  children,
  className = "",
}: {
  children: ReactNode;
  className?: string;
}) {
  return (
    <div
      className={`rounded-[14px] p-[20px] ${className}`}
      style={{
        backgroundColor: "var(--fra-surface)",
        border: "1px solid var(--fra-border)",
        boxShadow: "var(--fra-shadow)",
      }}
    >
      {children}
    </div>
  );
}

/**
 * Placeholder blocks while a section loads.
 *
 * `aria-hidden`, and the announcement is made ONCE by the container's own live
 * region — N boxes each announcing themselves is how a screen reader ends up
 * saying "loading" five times for one panel.
 */
export function HomeSkeleton({ rows = 2, height = 96 }: { rows?: number; height?: number }) {
  return (
    <div className="grid gap-[12px]" aria-hidden="true">
      {Array.from({ length: rows }, (_, i) => (
        <div
          key={i}
          className="animate-pulse rounded-[14px] motion-reduce:animate-none"
          style={{
            height,
            backgroundColor: "var(--fra-surface-2)",
            border: "1px solid var(--fra-border)",
          }}
        />
      ))}
    </div>
  );
}

/** Nothing here yet — stated as a next step rather than as an absence. */
export function HomeEmpty({ title, body, action }: { title: string; body: string; action?: ReactNode }) {
  return (
    <div
      className="rounded-[14px] px-[20px] py-[28px] text-center"
      style={{ border: "1px dashed var(--fra-border-strong)", backgroundColor: "var(--fra-surface)" }}
    >
      <p className="m-0 text-[15px] font-semibold" style={{ color: "var(--fra-heading)" }}>
        {title}
      </p>
      <p className="mx-auto mt-[6px] mb-0 max-w-[420px] text-[14px]" style={{ color: "var(--fra-muted)" }}>
        {body}
      </p>
      {action && <div className="mt-[14px]">{action}</div>}
    </div>
  );
}

/**
 * A section that could not load.
 *
 * Says that nothing has been lost, because the alternative reading — that the
 * requests or the bookings are gone — is the one a person jumps to. Same
 * wording as My requests, deliberately.
 */
export function HomeError({ what, onRetry }: { what: string; onRetry?: () => void }) {
  return (
    <div
      className="flex items-start gap-[12px] rounded-[14px] p-[20px]"
      style={{ border: "1px solid var(--fra-border)", backgroundColor: "var(--fra-surface)" }}
    >
      <AlertTriangle
        aria-hidden="true"
        className="mt-[2px] size-5 shrink-0"
        style={{ color: "var(--fra-rejected)" }}
      />
      <div className="min-w-0">
        <p className="m-0 text-[15px] font-semibold" style={{ color: "var(--fra-heading)" }}>
          We could not load {what}
        </p>
        <p className="mt-[4px] mb-0 text-[14px]" style={{ color: "var(--fra-muted)" }}>
          Nothing has been lost — this is a problem reaching the server.
        </p>
        {onRetry && (
          <button
            type="button"
            onClick={onRetry}
            className="mt-[12px] inline-flex min-h-[40px] items-center rounded-[10px] px-[14px] text-[14px] font-semibold"
            style={{ border: "1px solid var(--fra-border-strong)", color: "var(--fra-navy-2)" }}
          >
            Try again
          </button>
        )}
      </div>
    </div>
  );
}
