import type { ReactNode } from "react";
import { DataEmptyState } from "./data-empty-state";

type IconComponent = (props: { className?: string }) => ReactNode;

// Same tile as PickerEmptyState (size-16) so pickers and tables read as one family.
const TILE_TONE_CLASS = {
  neutral: "bg-foreground-platinum-green/12 text-foreground-platinum-green",
  accent: "bg-foreground-red-orange/12 text-brand-red-orange",
} as const;

// The app-wide first-run empty state for a data table. Built on DataEmptyState's
// showcase layout with a FIXED backdrop: a faded, masked ghost of a table sits
// behind a frosted message card. That shell — the ghost background and the card —
// is baked in here so every empty table across the app reads as one surface;
// callers vary only the icon, copy and CTAs. Presentational and i18n-free: the
// caller resolves every string through `t(...)` and passes it in.

/**
 * Standard empty state for a data table that has loaded fine but has no rows yet
 * (a genuinely empty collection — NOT a filtered-to-nothing view, which should
 * keep a plain "no matches" message).
 *
 * The ghost-table background and the frosted card are fixed; only `icon`,
 * `title`, `description` and the CTAs change per table, so all empty tables share
 * one look.
 *
 * @param icon - Bare icon component; wrapped here in the shared size-16 tile.
 *   Optional.
 * @param tone - Tile colour: `neutral` (platinum, default) or `accent`
 *   (red-orange, for showcase states).
 * @param title - Heading, already translated.
 * @param description - Supporting line under the title, already translated.
 * @param action - Primary CTA (typically a `<Button>`), shown first (left).
 * @param secondaryAction - Optional secondary CTA shown after the primary.
 * @param children - Extra interactions below the CTAs (e.g. a "how it works"
 *   link).
 * @remarks Purely presentational — no data fetching or side effects. Distinct
 *   from `RequireData`, which gates loading / error / missing-reference-data.
 */
export function TableEmptyState({
  icon,
  tone = "neutral",
  title,
  description,
  action,
  secondaryAction,
  help,
  children,
  className,
}: {
  icon?: IconComponent;
  tone?: keyof typeof TILE_TONE_CLASS;
  title: string;
  description?: ReactNode;
  action?: ReactNode;
  secondaryAction?: ReactNode;
  /** The "How it works" entry — see DataEmptyState's `help`. */
  help?: ReactNode;
  children?: ReactNode;
  className?: string;
}) {
  const Icon = icon;
  return (
    <DataEmptyState
      mock={<TableGhost />}
      icon={
        Icon ? (
          <span
            className={`flex size-16 items-center justify-center rounded-2xl ${TILE_TONE_CLASS[tone]}`}
          >
            <Icon className="size-8" />
          </span>
        ) : undefined
      }
      title={title}
      description={description}
      action={action}
      secondaryAction={secondaryAction}
      help={help}
      className={className}
    >
      {children}
    </DataEmptyState>
  );
}

// The fixed ghost table behind the message card: a header row plus body rows of
// generic cells (a leading tile, a two-line label, and trailing chips). Static
// and decorative — no pulse — so DataEmptyState's fade + mask read it as "this is
// what will be here", not as loading.
function TableGhost() {
  return (
    <div
      aria-hidden
      className="overflow-hidden rounded-2xl border border-surface-stroke bg-surface-base"
    >
      <div className="flex items-center gap-4 border-b border-surface-stroke px-5 py-3.5">
        <div className="h-3 w-28 rounded-full bg-surface-stroke" />
        <div className="h-3 w-20 rounded-full bg-surface-stroke" />
        <div className="ml-auto h-3 w-16 rounded-full bg-surface-stroke" />
      </div>
      {Array.from({ length: 6 }).map((_, i) => (
        <div
          key={i}
          className="flex items-center gap-4 border-b border-surface-stroke px-5 py-4 last:border-b-0"
        >
          <div className="size-9 shrink-0 rounded-xl bg-surface-base-muted" />
          <div className="space-y-1.5">
            <div className="h-3 w-32 rounded-full bg-surface-stroke" />
            <div className="h-2.5 w-44 rounded-full bg-surface-base-muted" />
          </div>
          <div className="ml-auto flex items-center gap-2">
            <div className="h-5 w-16 rounded-full bg-surface-base-muted" />
            <div className="h-5 w-14 rounded-full bg-foreground-platinum-green/12" />
          </div>
        </div>
      ))}
    </div>
  );
}
