/**
 * ReviewRequest.tsx — post-purchase review solicitation email.
 */

import {
  Html,
  Head,
  Body,
  Container,
  Section,
  Text,
  Link,
  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';

export interface ReviewRequestProps {
  buyerName?: string;
  dealTitle: string;
  reviewUrl: string;
  siteUrl: string;
  unsubscribeUrl: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'איך היה הדיל?',
    heading: (name?: string) => (name ? `${name}, איך היה?` : 'איך היה?'),
    body: (title: string) => `איך היה הניסיון ב-"${title}"? דירוג שלך עוזר לעסקים ולקונים אחרים.`,
    cta: 'כתיבת ביקורת',
    siteLink: 'לכל הדילים',
    unsub: 'הסרה מרשימת התפוצה',
  },
  en: {
    preview: 'How was the deal?',
    heading: (name?: string) => (name ? `${name}, how was it?` : 'How was it?'),
    body: (title: string) =>
      `How was your experience with "${title}"? Your rating helps businesses and other buyers.`,
    cta: 'Write a review',
    siteLink: 'Browse deals',
    unsub: 'Unsubscribe',
  },
} as const;

export function ReviewRequest({
  buyerName,
  dealTitle,
  reviewUrl,
  siteUrl,
  unsubscribeUrl,
  locale = 'he',
}: ReviewRequestProps) {
  const c = copy[locale] ?? copy.he;
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;

  return (
    <Html lang={locale} dir={dir}>
      <Head />
      <Preview>{c.preview}</Preview>
      <Body
        style={{
          backgroundColor: tokens.neutral50,
          fontFamily: fontFamily,
          margin: 0,
          padding: spacing.marginBlock32,
        }}
      >
        <Container
          style={{
            maxWidth: spacing.cardMaxWide,
            backgroundColor: tokens.neutral0,
            borderRadius: '8px',
            padding: '0',
            overflow: 'hidden',
          }}
        >
          {/* Header */}
          <Section
            style={{
              backgroundColor: tokens.brandPrimary700,
              padding: spacing.padHeaderWide,
              textAlign: locale === 'he' ? 'right' : 'left',
            }}
          >
            <Text
              style={{
                fontSize: spacing.px22,
                fontWeight: 700,
                color: tokens.brandOnPrimary,
                margin: 0,
                fontFamily,
              }}
            >
              {c.heading(buyerName)}
            </Text>
          </Section>

          {/* Body */}
          <Section
            style={{
              padding: spacing.padSectionLg,
              direction: dir,
              textAlign: locale === 'he' ? 'right' : 'left',
            }}
          >
            <Text
              style={{
                fontSize: spacing.px16,
                color: tokens.textSecondary,
                lineHeight: '1.6',
                margin: spacing.marginBottom20,
                fontFamily,
              }}
            >
              {c.body(dealTitle)}
            </Text>

            <div style={{ textAlign: locale === 'he' ? 'right' : 'left' }}>
              <Link
                href={reviewUrl}
                style={{
                  display: 'inline-block',
                  backgroundColor: tokens.brandPrimary600,
                  color: tokens.brandOnPrimary,
                  padding: spacing.padButtonSm,
                  borderRadius: '6px',
                  textDecoration: 'none',
                  fontWeight: 600,
                  fontFamily,
                  fontSize: spacing.px15,
                }}
              >
                {c.cta}
              </Link>
            </div>
          </Section>

          {/* Footer */}
          <Section
            style={{
              padding: spacing.padBodyWide,
              backgroundColor: tokens.surfaceRaised,
              borderTop: `1px solid ${tokens.borderDefault}`,
            }}
          >
            <Hr style={{ borderColor: tokens.borderDefault, margin: spacing.marginBottom12 }} />
            <Text
              style={{
                fontSize: spacing.px12,
                color: tokens.textMuted,
                textAlign: 'center',
                fontFamily,
              }}
            >
              <Link href={siteUrl} style={{ color: tokens.textMuted }}>
                {c.siteLink}
              </Link>
              {'  ·  '}
              <Link href={unsubscribeUrl} style={{ color: tokens.textMuted }}>
                {c.unsub}
              </Link>
            </Text>
          </Section>
        </Container>
      </Body>
    </Html>
  );
}

export default ReviewRequest;
