// @design-system: domain/RefundActionDialog
/**
 * RefundActionDialog — confirmation dialog before customer accepts vendor offer.
 */

import { useT } from '@/lib/i18n/react';
import type { StringBundle } from '@/lib/i18n/types';
import { formatAgorotShekels } from '@/lib/money';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@/components/ui/overlays/AlertDialog';
import type { CaseOfferView } from '@/server/support/types';

export interface RefundActionDialogProps {
  open: boolean;
  outcome: CaseOfferView['outcome'];
  amountCents: number | null;
  onConfirm: () => void;
  onCancel: () => void;
  processing?: boolean;
}

function confirmBodyKey(outcome: CaseOfferView['outcome']): keyof StringBundle['cases']['decide'] {
  if (outcome === 'replacement') return 'confirm_accept_body_replacement';
  if (outcome === 'deny') return 'confirm_accept_body_deny';
  return 'confirm_accept_body_refund';
}

export function RefundActionDialog({
  open,
  outcome,
  amountCents,
  onConfirm,
  onCancel,
  processing = false,
}: RefundActionDialogProps) {
  const t = useT('cases');
  const tDecide = t('decide') as unknown as StringBundle['cases']['decide'];

  const amountDisplay = amountCents !== null ? formatAgorotShekels(amountCents) : null;
  const bodyKey = confirmBodyKey(outcome);

  return (
    <AlertDialog open={open} onOpenChange={(o) => !o && onCancel()}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{tDecide.confirm_accept_title}</AlertDialogTitle>
          <AlertDialogDescription>
            {amountDisplay && (
              <span className="text-brand-primary-600 mb-1 block text-lg font-bold">
                {amountDisplay}
              </span>
            )}
            {tDecide[bodyKey]}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel onClick={onCancel} disabled={processing}>
            {tDecide.cancel}
          </AlertDialogCancel>
          <AlertDialogAction onClick={onConfirm} disabled={processing}>
            {processing ? tDecide.processing : tDecide.accept}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
