/**
 * EmailVerify.tsx - Email change verification email.
 *
 * Sent when a user (or vendor) changes their email address.
 * Contains a verification link that must be clicked to confirm the new address.
 */

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 EmailVerifyProps {
  verifyUrl: string;
  expiryIso: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: 'אמת את כתובת האימייל שלך ב-Multideal',
    heading: 'אימות כתובת אימייל',
    body: 'לחץ/י על הכפתור למטה לאימות כתובת האימייל החדשה שלך.',
    cta: 'אמת את האימייל',
    expiry: (iso: string) => `הקישור בתוקף עד ${formatEmailDateTime(iso, 'he')}`,
    security: 'אם לא ביקשת שינוי אימייל, אנא פנה לתמיכה מיידית.',
  },
  en: {
    preview: 'Verify your Multideal email address',
    heading: 'Email Address Verification',
    body: 'Click the button below to verify your new email address.',
    cta: 'Verify email',
    expiry: (iso: string) => `This link expires ${formatEmailDateTime(iso, 'en')}`,
    security: "If you didn't request an email change, contact support immediately.",
  },
} as const;

export default function EmailVerify({ verifyUrl, expiryIso, locale = 'he' }: EmailVerifyProps) {
  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',
            }}
          >
            <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>

            <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}
              </Text>

              <div
                style={{
                  marginBottom: spacing.px24,
                  textAlign: locale === 'he' ? 'right' : 'left',
                }}
              >
                <Link
                  href={verifyUrl}
                  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>
  );
}
