/**
 * PaymentRefund.tsx - Refund confirmation email.
 *
 * Sent to the customer (user or guest) when a payment has been refunded.
 */

import React from 'react';
import {
  Html,
  Head,
  Body,
  Container,
  Section,
  Text,
  Hr,
  Preview,
} from '@/server/email/primitives.js';
import { dirForLocale, type Locale } from '@/lib/i18n/index.js';
import { fonts, spacing, tokens } from '@/server/email/tokens/index.js';
import { formatEmailDateTime, formatPaymentMethodLabel } from '@/lib/format';

export interface PaymentRefundProps {
  transactionId: string;
  dealTitle: string;
  refundAmount: string;
  refundDateIso: string;
  paymentMethod: { last4: string; brand: string };
  estimatedArrivalDays?: number;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'אישור החזר כספי מ-Multideal',
    heading: 'אישור החזר כספי',
    transactionId: 'מספר עסקה',
    deal: 'עסקה',
    refundAmount: 'סכום שהוחזר',
    paymentMethod: 'אמצעי תשלום',
    refundDate: 'תאריך ההחזר',
    arrivalNote: (days: number) => `ההחזר יגיע תוך ${days} ימי עסקים.`,
    currency: '₪',
    footer: 'לשאלות, פנה/י לתמיכת Multideal.',
  },
  en: {
    preview: 'Your Multideal refund confirmation',
    heading: 'Refund Confirmation',
    transactionId: 'Transaction ID',
    deal: 'Deal',
    refundAmount: 'Refund Amount',
    paymentMethod: 'Payment Method',
    refundDate: 'Refund Date',
    arrivalNote: (days: number) => `Your refund should arrive within ${days} business days.`,
    currency: '₪',
    footer: 'For questions, contact Multideal support.',
  },
} as const;

export default function PaymentRefund({
  transactionId,
  dealTitle,
  refundAmount,
  refundDateIso,
  paymentMethod,
  estimatedArrivalDays = 5,
  locale = 'he',
}: PaymentRefundProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;

  const labelStyle: React.CSSProperties = {
    fontSize: spacing.px11,
    color: tokens.textMuted,
    fontFamily,
    textTransform: 'uppercase',
    letterSpacing: '0.06em',
    marginBottom: '2px',
    marginTop: spacing.px16,
  };

  const valueStyle: React.CSSProperties = {
    fontSize: spacing.px15,
    color: tokens.textPrimary,
    fontFamily,
    margin: '0',
  };

  return (
    <Html lang={locale} dir={dir}>
      <Head />
      <Preview>{t.preview}</Preview>
      <Body style={{ backgroundColor: tokens.neutral100, fontFamily }}>
        <Container style={{ padding: spacing.padPage }}>
          <div
            style={{
              maxWidth: spacing.cardMaxNarrow,
              margin: '0 auto',
              backgroundColor: tokens.surfaceBase,
              borderRadius: '8px',
              border: `1px solid ${tokens.borderDefault}`,
              overflow: 'hidden',
            }}
          >
            {/* Header */}
            <Section
              style={{
                backgroundColor: tokens.brandPrimary700,
                padding: spacing.padHeaderLg,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text
                style={{
                  fontSize: spacing.px20,
                  fontWeight: 700,
                  color: tokens.brandOnPrimary,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.heading}
              </Text>
            </Section>

            {/* Body */}
            <Section
              style={{
                padding: spacing.padSectionLg,
                direction: dir,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text style={labelStyle}>{t.transactionId}</Text>
              <Text
                style={{
                  ...valueStyle,
                  fontSize: spacing.px13,
                  color: tokens.textMuted,
                  fontFamily: fonts.mono,
                }}
              >
                {transactionId}
              </Text>

              <Hr style={{ borderColor: tokens.borderDefault, margin: spacing.marginBlock16 }} />

              <Text style={labelStyle}>{t.deal}</Text>
              <Text style={valueStyle}>{dealTitle}</Text>

              <Text style={labelStyle}>{t.refundAmount}</Text>
              <Text
                style={{
                  ...valueStyle,
                  fontSize: spacing.px26,
                  fontWeight: 700,
                  color: tokens.success600,
                }}
              >
                {t.currency}
                {refundAmount}
              </Text>

              <Text style={labelStyle}>{t.paymentMethod}</Text>
              <Text style={valueStyle}>
                {formatPaymentMethodLabel(paymentMethod.brand, paymentMethod.last4)}
              </Text>

              <Text style={labelStyle}>{t.refundDate}</Text>
              <Text style={valueStyle}>{formatEmailDateTime(refundDateIso, locale)}</Text>

              <Hr style={{ borderColor: tokens.borderDefault, margin: spacing.marginBlock16 }} />

              <Text
                style={{
                  fontSize: spacing.px13,
                  color: tokens.textMuted,
                  fontFamily,
                  margin: '0 0 6px',
                }}
              >
                {t.arrivalNote(estimatedArrivalDays)}
              </Text>
              <Text
                style={{
                  fontSize: spacing.px13,
                  color: tokens.textMuted,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.footer}
              </Text>
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
