import type { ReactNode } from 'react';
import { usePage } from '@inertiajs/react';
import Link from '@rutatiina/ui/components/common/link';
import { Button } from '@rutatiina/ui/components/ui/button';
import { useTrans } from '@rutatiina/ui/lib/translations';

type Shared = { organizations?: { current?: { name: string } | null } };

/** A standalone page for printing or saving as PDF, with the institution name and print controls. */
export function PrintSheet({
  title,
  children,
  back,
}: {
  title: string;
  children: ReactNode;
  back?: string;
}) {
  const __ = useTrans();
  const shared = usePage<Shared>().props;
  return (
    <div className="mx-auto max-w-4xl space-y-6 p-6 text-sm print:max-w-none print:p-0">
      <div className="flex gap-3 print:hidden">
        <Link href={back ?? '/savings'}>{__('Back to savings')}</Link>
        <Button type="button" size="sm" onClick={() => window.print()}>
          {__('Print or save as PDF')}
        </Button>
      </div>
      <header className="border-b pb-3">
        <p className="text-lg font-semibold">
          {shared.organizations?.current?.name ?? ''}
        </p>
        <h1 className="text-xl font-semibold">{__(title)}</h1>
      </header>
      {children}
    </div>
  );
}

/** A two-column list of labelled values. */
export function Facts({ rows }: { rows: [string, ReactNode][] }) {
  const __ = useTrans();
  return (
    <dl className="grid grid-cols-2 gap-x-6 gap-y-1">
      {rows.map(([k, v]) => (
        <div className="contents" key={k}>
          <dt className="text-muted-foreground">{__(k)}</dt>
          <dd>{v}</dd>
        </div>
      ))}
    </dl>
  );
}
