/**
 * BuyerWeeklyDigest.tsx — weekly deal digest 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';
import { formatAgorotWhole } from '@/lib/money';

export interface BuyerWeeklyDigestDeal {
  title: string;
  slug: string;
  priceAgorot: number;
  originalPriceAgorot: number;
  categorySlug?: string;
}

export interface BuyerWeeklyDigestProps {
  buyerName?: string;
  deals: BuyerWeeklyDigestDeal[];
  weekStr: string;
  siteUrl: string;
  unsubscribeUrl: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'הדילים הכי טובים השבוע',
    heading: 'עיכול שבועי',
    dealLink: 'לצפייה',
    unsub: 'הסרה מרשימת התפוצה',
    siteLink: 'לכל הדילים',
    greeting: (name: string) => `היי ${name},`,
  },
  en: {
    preview: 'The best deals this week',
    heading: 'Weekly digest',
    dealLink: 'View',
    unsub: 'Unsubscribe',
    siteLink: 'All deals',
    greeting: (name: string) => `Hi ${name},`,
  },
} as const;

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

  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}
            </Text>
            <Text
              style={{
                fontSize: spacing.px14,
                color: tokens.brandOnPrimary,
                margin: '4px 0 0',
                opacity: 0.85,
                fontFamily,
              }}
            >
              {weekStr}
            </Text>
          </Section>

          {/* Body */}
          <Section style={{ padding: spacing.padHeaderWide }}>
            {buyerName && (
              <Text
                style={{
                  fontSize: spacing.px16,
                  color: tokens.textSecondary,
                  margin: spacing.marginBottom16,
                  fontFamily,
                }}
              >
                {c.greeting(buyerName)}
              </Text>
            )}

            {topDeals.map((deal) => (
              <div
                key={deal.slug}
                style={{
                  display: 'flex',
                  justifyContent: 'space-between',
                  alignItems: 'center',
                  padding: spacing.marginBlock12,
                  borderBottom: `1px solid ${tokens.borderDefault}`,
                }}
              >
                <div>
                  <Text
                    style={{
                      fontSize: spacing.px15,
                      fontWeight: 600,
                      color: tokens.textPrimary,
                      margin: '0 0 2px',
                      fontFamily,
                    }}
                  >
                    {deal.title}
                  </Text>
                  <Text
                    style={{
                      fontSize: spacing.px13,
                      color: tokens.textSecondary,
                      margin: 0,
                      fontFamily,
                    }}
                  >
                    {formatAgorotWhole(deal.priceAgorot)}{' '}
                    <span style={{ textDecoration: 'line-through', color: tokens.textMuted }}>
                      {formatAgorotWhole(deal.originalPriceAgorot)}
                    </span>
                  </Text>
                </div>
                <Link
                  href={`${siteUrl}/deals/${deal.slug}`}
                  style={{
                    color: tokens.textLink,
                    fontSize: spacing.px13,
                    fontWeight: 600,
                    textDecoration: 'none',
                    fontFamily,
                    whiteSpace: 'nowrap',
                  }}
                >
                  {c.dealLink}
                </Link>
              </div>
            ))}

            <div style={{ textAlign: 'center', marginTop: spacing.px24 }}>
              <Link
                href={siteUrl}
                style={{
                  display: 'inline-block',
                  backgroundColor: tokens.brandPrimary600,
                  color: tokens.brandOnPrimary,
                  padding: spacing.padTagLg,
                  borderRadius: '6px',
                  textDecoration: 'none',
                  fontWeight: 600,
                  fontFamily,
                  fontSize: spacing.px14,
                }}
              >
                {c.siteLink}
              </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={unsubscribeUrl} style={{ color: tokens.textMuted }}>
                {c.unsub}
              </Link>
            </Text>
          </Section>
        </Container>
      </Body>
    </Html>
  );
}

export default BuyerWeeklyDigest;
