/**
 * VoucherExpiry.tsx — reminder sent when a voucher is about to expire.
 */

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 VoucherExpiryProps {
  buyerName?: string;
  dealTitle: string;
  expiryDateStr: string;
  redeemUrl: string;
  siteUrl: string;
  unsubscribeUrl: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: "הוואוצ'ר שלך פג עוד 3 ימים",
    heading: (name?: string) => (name ? `${name}, אל תפספס!` : 'אל תפספס!'),
    body: (title: string, expiry: string) => `הדיל "${title}" פג ב-${expiry}. מהרו לממש.`,
    cta: 'מימוש הדיל',
    siteLink: 'לכל הדילים שלי',
    unsub: 'הסרה מרשימת התפוצה',
  },
  en: {
    preview: 'Your voucher expires in 3 days',
    heading: (name?: string) => (name ? `${name}, don't miss out!` : "Don't miss out!"),
    body: (title: string, expiry: string) =>
      `Your deal "${title}" expires on ${expiry}. Redeem it now.`,
    cta: 'Redeem deal',
    siteLink: 'My deals',
    unsub: 'Unsubscribe',
  },
} as const;

export function VoucherExpiry({
  buyerName,
  dealTitle,
  expiryDateStr,
  redeemUrl,
  siteUrl,
  unsubscribeUrl,
  locale = 'he',
}: VoucherExpiryProps) {
  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',
          }}
        >
          {/* Warning header strip */}
          <Section
            style={{
              backgroundColor: tokens.warning600,
              padding: spacing.padHero,
              textAlign: locale === 'he' ? 'right' : 'left',
            }}
          >
            <Text
              style={{
                fontSize: spacing.px18,
                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.marginBottom24,
                fontFamily,
              }}
            >
              {c.body(dealTitle, expiryDateStr)}
            </Text>

            <div style={{ textAlign: locale === 'he' ? 'right' : 'left' }}>
              <Link
                href={redeemUrl}
                style={{
                  display: 'inline-block',
                  backgroundColor: tokens.warning600,
                  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 VoucherExpiry;
