/**
 * BuyerWelcomeDay3.tsx — sent 3 days after signup with curated deals.
 */

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';
import { formatAgorotWhole } from '@/lib/money';

export interface BuyerWelcomeDay3Deal {
  title: string;
  slug: string;
  priceAgorot: number;
  originalPriceAgorot: number;
}

export interface BuyerWelcomeDay3Props {
  buyerName?: string;
  deals: BuyerWelcomeDay3Deal[];
  siteUrl: string;
  unsubscribeUrl: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'יש דילים חדשים באזורך',
    heading: 'הדילים שחיכו לך',
    body: '3 דילים שבחרנו בשבילך:',
    dealLink: 'לצפייה',
    unsub: 'הסרה מרשימת התפוצה',
    greeting: (name?: string) => (name ? `היי ${name},` : 'היי,'),
  },
  en: {
    preview: 'New deals near you',
    heading: 'Deals waiting for you',
    body: '3 deals we picked for you:',
    dealLink: 'View deal',
    unsub: 'Unsubscribe',
    greeting: (name?: string) => (name ? `Hi ${name},` : 'Hi,'),
  },
} as const;

export function BuyerWelcomeDay3({
  buyerName,
  deals,
  siteUrl,
  unsubscribeUrl,
  locale = 'he',
}: BuyerWelcomeDay3Props) {
  const c = copy[locale] ?? copy.he;
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;
  const topDeals = deals.slice(0, 3);

  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.px22,
                fontWeight: 700,
                color: tokens.textPrimary,
                margin: '0 0 8px',
                fontFamily,
              }}
            >
              {c.greeting(buyerName)}
            </Text>
            <Text
              style={{
                fontSize: spacing.px16,
                color: tokens.textSecondary,
                lineHeight: '1.6',
                fontFamily,
              }}
            >
              {c.body}
            </Text>
          </Section>

          {topDeals.map((deal) => (
            <Section
              key={deal.slug}
              style={{
                margin: spacing.marginBlock16,
                padding: spacing.px16,
                backgroundColor: tokens.surfaceRaised,
                borderRadius: '6px',
                border: `1px solid ${tokens.borderDefault}`,
              }}
            >
              <Text
                style={{
                  fontSize: spacing.px16,
                  fontWeight: 600,
                  color: tokens.textPrimary,
                  margin: '0 0 4px',
                  fontFamily,
                }}
              >
                {deal.title}
              </Text>
              <Text
                style={{
                  fontSize: spacing.px14,
                  color: tokens.textSecondary,
                  margin: '0 0 8px',
                  fontFamily,
                }}
              >
                {formatAgorotWhole(deal.priceAgorot)}{' '}
                <span style={{ textDecoration: 'line-through', color: tokens.textMuted }}>
                  {formatAgorotWhole(deal.originalPriceAgorot)}
                </span>
              </Text>
              <Link
                href={`${siteUrl}/deals/${deal.slug}`}
                style={{
                  color: tokens.textLink,
                  fontSize: spacing.px14,
                  fontWeight: 600,
                  textDecoration: 'none',
                  fontFamily,
                }}
              >
                {c.dealLink} ←
              </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 BuyerWelcomeDay3;
