/**
 * MagicLink.tsx - Guest → User conversion magic link email.
 *
 * Sent when a guest checks out; allows account creation with purchase history intact.
 */

import {
  Html,
  Head,
  Body,
  Container,
  Section,
  Text,
  Link,
  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 { formatEmailDateTime } from '@/lib/format';

export interface MagicLinkProps {
  name?: string;
  magicLinkUrl: string;
  expiryIso: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'קישור כניסה לחשבון Multideal שלך',
    heading: 'החשבון שלך מוכן',
    body: (name?: string) =>
      name
        ? `היי ${name}, לחץ/י על הכפתור למטה להפעלת החשבון שלך ב-Multideal.`
        : 'לחץ/י על הכפתור למטה להפעלת החשבון שלך ב-Multideal.',
    cta: 'הפעל את החשבון',
    expiry: (expiryIso: string) => `הקישור בתוקף עד ${formatEmailDateTime(expiryIso, 'he')}`,
    security: 'אם לא ביקשת קישור זה, התעלם/י ממנו.',
  },
  en: {
    preview: 'Your Multideal account login link',
    heading: 'Your account is ready',
    body: (name?: string) =>
      name
        ? `Hi ${name}, click the button below to activate your Multideal account.`
        : 'Click the button below to activate your Multideal account.',
    cta: 'Activate my account',
    expiry: (expiryIso: string) => `This link expires ${formatEmailDateTime(expiryIso, 'en')}`,
    security: "If you didn't request this link, you can safely ignore it.",
  },
} as const;

export default function MagicLink({
  name,
  magicLinkUrl,
  expiryIso,
  locale = 'he',
}: MagicLinkProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;

  return (
    <Html lang={locale} dir={dir}>
      <Head />
      <Preview>{t.preview}</Preview>
      <Body style={{ backgroundColor: tokens.neutral100, fontFamily }}>
        <Container style={{ padding: spacing.padPage }}>
          <div
            style={{
              maxWidth: spacing.cardMaxNarrow,
              margin: '0 auto',
              backgroundColor: tokens.surfaceBase,
              borderRadius: '8px',
              border: `1px solid ${tokens.borderDefault}`,
              overflow: 'hidden',
            }}
          >
            {/* Header */}
            <Section
              style={{
                backgroundColor: tokens.brandPrimary700,
                padding: spacing.padHeaderLg,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text
                style={{
                  fontSize: spacing.px20,
                  fontWeight: 700,
                  color: tokens.brandOnPrimary,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.heading}
              </Text>
            </Section>

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

              <div
                style={{
                  textAlign: locale === 'he' ? 'right' : 'left',
                  marginBottom: spacing.px24,
                }}
              >
                <Link
                  href={magicLinkUrl}
                  style={{
                    backgroundColor: tokens.brandPrimary700,
                    color: tokens.brandOnPrimary,
                    padding: spacing.padButton,
                    borderRadius: '6px',
                    fontFamily,
                    fontSize: spacing.px15,
                    fontWeight: 600,
                    textDecoration: 'none',
                    display: 'inline-block',
                  }}
                >
                  {t.cta}
                </Link>
              </div>

              <Text
                style={{
                  fontSize: spacing.px12,
                  color: tokens.textMuted,
                  fontFamily,
                  margin: '0 0 6px',
                }}
              >
                {t.expiry(expiryIso)}
              </Text>
              <Text
                style={{
                  fontSize: spacing.px12,
                  color: tokens.textMuted,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.security}
              </Text>
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
