import type { ReactNode } from "react";
import { Copy } from "lucide-react";
import { toast } from "sonner";
import { TerraceMark } from "./brand";
import { fmtDate } from "@/lib/dates";
import { fmtPhone } from "@/lib/phone";
import { cn } from "@/lib/utils";

export function PageHeader({
  title,
  description,
  actions,
  children,
}: {
  title: string;
  description?: string;
  actions?: ReactNode;
  children?: ReactNode;
}) {
  return (
    <header className="mb-6">
      <div className="grid grid-cols-[minmax(0,1fr)] gap-4 sm:flex sm:items-start sm:justify-between">
        <div className="flex min-w-0 items-start gap-3">
          <TerraceMark className="mt-0.5 h-7 w-7 shrink-0 opacity-90" />
          <div className="min-w-0">
            <h1 className="font-display text-2xl font-bold leading-8 tracking-tight">{title}</h1>
            {description && <p className="mt-0.5 text-muted-foreground">{description}</p>}
          </div>
        </div>
        {actions && <div className="flex shrink-0 flex-wrap gap-2">{actions}</div>}
      </div>
      {children}
    </header>
  );
}
export function Card({
  title,
  actions,
  children,
  className,
  padded = true,
}: {
  title?: ReactNode;
  actions?: ReactNode;
  children: ReactNode;
  className?: string;
  padded?: boolean;
}) {
  return (
    <section
      className={cn(
        "relative min-w-0 rounded-xl border bg-card shadow-(--shadow-card)",
        !padded && "overflow-hidden",
        className,
      )}
    >
      {(title || actions) && (
        <div className="flex items-center justify-between gap-3 border-b px-4 py-3">
          <h2 className="font-display text-[15px] font-semibold">{title}</h2>
          {actions}
        </div>
      )}
      <div className={cn(padded && "p-4")}>{children}</div>
    </section>
  );
}
export function Stat({
  label,
  value,
  tone,
}: {
  label: string;
  value: ReactNode;
  tone?: "danger" | "warning" | "success" | undefined;
}) {
  return (
    <div className="rounded-xl border bg-card px-4 py-3">
      <p className="text-[12.5px] text-muted-foreground">{label}</p>
      <p
        className={cn(
          "font-display text-2xl font-bold tabular",
          tone === "danger" && "text-danger",
          tone === "warning" && "text-warning",
          tone === "success" && "text-success",
        )}
      >
        {value}
      </p>
    </div>
  );
}
function copy(v: string, what: string) {
  void navigator.clipboard.writeText(v);
  toast.success(`${what} copied`);
}
export function Mono({ children }: { children: string }) {
  return (
    <span className="inline-flex items-center gap-1 font-mono text-[12.5px]">
      {children}
      <button
        type="button"
        className="text-subtle hover:text-foreground"
        aria-label={`Copy ${children}`}
        onClick={(e) => {
          e.stopPropagation();
          e.preventDefault();
          copy(children, "ID");
        }}
      >
        <Copy className="h-3 w-3" />
      </button>
    </span>
  );
}
export function PhoneText({ value, masked = false }: { value: string; masked?: boolean }) {
  const d = fmtPhone(value);
  if (masked)
    return (
      <span className="tabular text-muted-foreground">
        {d.slice(0, 4)} •• •• {d.slice(-2)}
      </span>
    );
  return (
    <button
      type="button"
      className="tabular hover:text-primary"
      onClick={(e) => {
        e.stopPropagation();
        copy(d.replace(/ /g, ""), "Phone");
      }}
      title="Click to copy"
    >
      {d}
    </button>
  );
}
export function DateText({ value }: { value?: string | undefined }) {
  return (
    <time dateTime={value} title={value} className="whitespace-nowrap tabular">
      {fmtDate(value)}
    </time>
  );
}
