/**
 * BuyerWelcome.tsx — sent after buyer account creation / first purchase.
 */

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 BuyerWelcomeProps {
  buyerName?: string;
  siteUrl: string;
  unsubscribeUrl: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'ברוך הבא למולטידיל — דילים מוזלים',
    heading: (n?: string) => (n ? `ברוך הבא, ${n}!` : 'ברוך הבא!'),
    body: 'אנחנו שמחים שהצטרפת. כל שבוע תקבל דילים מובחרים מהעסקים הכי טובים באזורך — במחיר מוזל.',
    cta: 'לצפייה בדילים',
    unsub: 'הסרה מרשימת התפוצה',
  },
  en: {
    preview: 'Welcome to Multideal — discounted deals',
    heading: (n?: string) => (n ? `Welcome, ${n}!` : 'Welcome!'),
    body: "We're glad you joined. Every week you'll receive curated deals from the best local businesses — at a discount.",
    cta: 'Browse deals',
    unsub: 'Unsubscribe',
  },
} as const;

export function BuyerWelcome({
  buyerName,
  siteUrl,
  unsubscribeUrl,
  locale = 'he',
}: BuyerWelcomeProps) {
  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: spacing.padSectionLg,
          }}
        >
          <Section>
            <Text
              style={{
                fontSize: spacing.px24,
                fontWeight: 700,
                color: tokens.textPrimary,
                marginBottom: '8px',
                fontFamily,
              }}
            >
              {c.heading(buyerName)}
            </Text>
            <Text
              style={{
                fontSize: spacing.px16,
                color: tokens.textSecondary,
                lineHeight: '1.6',
                fontFamily,
              }}
            >
              {c.body}
            </Text>
          </Section>
          <Section style={{ textAlign: 'center', marginTop: spacing.px24 }}>
            <Link
              href={siteUrl}
              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>
          </Section>
          <Hr style={{ borderColor: tokens.borderDefault, margin: spacing.marginSection }} />
          <Text
            style={{
              fontSize: spacing.px12,
              color: tokens.textMuted,
              textAlign: 'center',
              fontFamily,
            }}
          >
            <Link href={unsubscribeUrl} style={{ color: tokens.textMuted }}>
              {c.unsub}
            </Link>
          </Text>
        </Container>
      </Body>
    </Html>
  );
}

export default BuyerWelcome;
