"use client";

import type { ReactNode } from "react";
import { CheckIcon } from "./icons";
import { LOOK } from "./live/look";
import { SelectionCircle } from "./selected-check";

// One of a few answers to a question, as a card: what it is called, in a line
// what it means, and a mark that says whether it is the one chosen. Larger
// and friendlier than a chip, for a question whose answers should be read
// (which format, which rule set, which export). The whole card is the button.
//
// Presentational only: the parent owns the selection, so it serves single-
// and multi-select alike. Sizing is the caller's (`className`), so it stands
// in a flex strip, a grid or a narrow drawer.
//
// Two layouts of the one part:
//
//   • "stacked" (the default) — an icon in a round badge beside the title,
//     the detail under both, the mark at the top right. For a grid of cards
//     with some text each.
//   • "row" — an optional picture at the left, title and detail beside it,
//     the mark at the right, all on one line's height. For a short list of
//     answers under each other.

// The selected-state classes per tone. "brand" is the red-orange look (the
// original); "neutral" borrows the insights top SegmentedPicker's language
// (foreground-default on a muted surface) for a quieter selection.
const SELECTED_TONE = {
  brand: {
    card: "border-foreground-red-orange bg-foreground-red-orange/[0.04]",
    check: "bg-foreground-red-orange text-white",
    icon: "bg-foreground-red-orange/10 text-brand-red-orange",
  },
  neutral: {
    card: "border-foreground-default bg-surface-base-muted",
    check: "bg-foreground-default text-surface-base",
    icon: "bg-foreground-default/10 text-foreground-default",
  },
} as const;

// The same two tones for the "row" layout, which marks with the shared
// `SelectionCircle`: its ring carries the tone while the card is not chosen.
const ROW_TONE = {
  brand: {
    card: "border-foreground-red-orange bg-foreground-red-orange/10",
    circle: "var(--foreground-red-orange)",
  },
  neutral: {
    card: "border-foreground-default bg-surface-base-muted",
    circle: "var(--foreground-default)",
  },
} as const;

type Shared = {
  title: string;
  /** What choosing it means: a line in a row, a few under a stacked card. */
  detail?: ReactNode;
  selected: boolean;
  onSelect: () => void;
  disabled?: boolean;
  /** The selected look: red-orange ("brand", the default) or "neutral". */
  tone?: "brand" | "neutral";
  /** Sizing and layout in the parent; the card has none of its own. */
  className?: string;
};

export type SelectableCardProps = Shared &
  (
    | {
        layout?: "stacked";
        /** Stands in a round badge beside the title. */
        icon: ReactNode;
      }
    | {
        layout: "row";
        /** Stands at the left as it is given: a small picture of the answer. */
        picture?: ReactNode;
      }
  );

/**
 * A card that can be chosen.
 *
 * @remarks
 * In the "row" layout the title alone is the button's accessible name (the
 * detail describes); a stacked card is named by all of its text.
 */
export function SelectableCard(props: SelectableCardProps) {
  const {
    title,
    detail,
    selected,
    onSelect,
    disabled = false,
    tone = "brand",
    className,
  } = props;

  if (props.layout === "row") {
    const rowTone = ROW_TONE[tone];
    return (
      <button
        type="button"
        onClick={onSelect}
        aria-pressed={selected}
        aria-label={title}
        disabled={disabled}
        className={`flex w-full items-center gap-4 rounded-2xl border px-4 py-3.5 text-left transition-colors disabled:cursor-not-allowed disabled:opacity-50 ${
          selected
            ? rowTone.card
            : "border-surface-stroke bg-surface-base hover:border-foreground-muted"
        } ${className ?? ""}`}
      >
        {props.picture}
        <span className="min-w-0 flex-1">
          <span className={`block ${LOOK.name}`}>{title}</span>
          {detail != null && (
            <span className={`mt-0.5 block ${LOOK.caption}`}>{detail}</span>
          )}
        </span>
        <SelectionCircle active={selected} color={rowTone.circle} />
      </button>
    );
  }

  const toneClasses = SELECTED_TONE[tone];
  return (
    <button
      type="button"
      aria-pressed={selected}
      disabled={disabled}
      onClick={onSelect}
      className={`relative rounded-2xl border p-4 text-left transition disabled:cursor-not-allowed disabled:opacity-50 ${
        selected
          ? toneClasses.card
          : "border-surface-stroke bg-surface-base hover:border-surface-stroke-muted"
      } ${className ?? ""}`}
    >
      {/* Selection marker, top-right: an empty ring when unselected (signals the
          card is selectable), filling to a toned check when selected. */}
      <span
        aria-hidden
        className={`absolute right-3 top-3 flex size-5 items-center justify-center rounded-full transition-colors ${
          selected
            ? toneClasses.check
            : "border border-surface-stroke bg-surface-base"
        }`}
      >
        {selected && <CheckIcon className="size-3" />}
      </span>

      <div className="flex items-center gap-3">
        <span
          className={`flex size-9 shrink-0 items-center justify-center rounded-full transition-colors ${
            selected ? toneClasses.icon : "bg-surface-base-muted text-foreground-muted"
          }`}
        >
          {props.icon}
        </span>
        <span className="body-prominent text-foreground-default">{title}</span>
      </div>
      {detail != null && (
        <div className="mt-3 body-small text-foreground-muted">{detail}</div>
      )}
    </button>
  );
}
