/**
 * SoldOut.tsx - FDS §9.6 Deal Sold Out Email (to vendor)
 *
 * Sent when quantity_remaining = 0.
 * Congratulates the vendor, shows metrics, and prompts to run the deal again.
 */

import React from 'react';
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 { emailAlpha, fonts, spacing, tokens } from '@/server/email/tokens/index.js';

export interface SoldOutProps {
  dealTitle: string;
  unitsSold: number;
  revenueEarned: string;
  duplicateDealUrl: string;
  vendorName: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: (title: string) => `נגמר המלאי ל-"${title}" - כל הכבוד!`,
    heading: 'נגמר המלאי! 🎊',
    sub: (title: string) => `העסקה "${title}" נמכרה במלואה.`,
    unitsSoldLabel: 'יחידות שנמכרו',
    revenueLabel: 'הכנסה שנצברה',
    currency: '₪',
    cta: 'רוצה לעשות את זה שוב מחר?',
    ctaLink: 'שכפל את העסקה',
  },
  en: {
    preview: (title: string) => `"${title}" is sold out - congratulations!`,
    heading: 'Sold Out! 🎊',
    sub: (title: string) => `"${title}" has been fully claimed.`,
    unitsSoldLabel: 'Units sold',
    revenueLabel: 'Revenue earned',
    currency: '₪',
    cta: 'Want to do this again tomorrow?',
    ctaLink: 'Duplicate this deal',
  },
} as const;

export default function SoldOut({
  dealTitle,
  unitsSold,
  revenueEarned,
  duplicateDealUrl,
  vendorName: _vendorName,
  locale = 'he',
}: SoldOutProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;

  const metricStyle: React.CSSProperties = {
    display: 'inline-block',
    textAlign: 'center',
    padding: spacing.padCard,
    backgroundColor: tokens.surfaceRaised,
    borderRadius: '8px',
    border: `1px solid ${tokens.borderDefault}`,
    minWidth: spacing.qrSize,
  };

  return (
    <Html lang={locale} dir={dir}>
      <Head />
      <Preview>{t.preview(dealTitle)}</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.px22,
                  fontWeight: 700,
                  color: tokens.brandOnPrimary,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.heading}
              </Text>
              <Text
                style={{
                  fontSize: spacing.px15,
                  color: emailAlpha.onPrimary80,
                  fontFamily,
                  margin: '6px 0 0',
                }}
              >
                {t.sub(dealTitle)}
              </Text>
            </Section>

            {/* Metrics */}
            <Section
              style={{
                padding: spacing.padSectionLg,
                textAlign: 'center',
              }}
            >
              <table
                style={{ width: '100%', borderCollapse: 'separate', borderSpacing: spacing.px12 }}
              >
                <tbody>
                  <tr>
                    <td style={metricStyle}>
                      <div
                        style={{
                          fontSize: spacing.px32,
                          fontWeight: 700,
                          color: tokens.brandPrimary700,
                          fontFamily,
                        }}
                      >
                        {unitsSold}
                      </div>
                      <div
                        style={{
                          fontSize: spacing.px12,
                          color: tokens.textMuted,
                          fontFamily,
                          marginTop: '4px',
                        }}
                      >
                        {t.unitsSoldLabel}
                      </div>
                    </td>
                    <td style={metricStyle}>
                      <div
                        style={{
                          fontSize: spacing.px32,
                          fontWeight: 700,
                          color: tokens.brandPrimary700,
                          fontFamily,
                        }}
                      >
                        {t.currency}
                        {revenueEarned}
                      </div>
                      <div
                        style={{
                          fontSize: spacing.px12,
                          color: tokens.textMuted,
                          fontFamily,
                          marginTop: '4px',
                        }}
                      >
                        {t.revenueLabel}
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </Section>

            <Hr style={{ borderColor: tokens.borderDefault, margin: spacing.marginSides40 }} />

            {/* CTA */}
            <Section
              style={{
                padding: spacing.padHeaderLg,
                direction: dir,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text
                style={{
                  fontSize: spacing.px16,
                  color: tokens.textSecondary,
                  fontFamily,
                  margin: spacing.marginBottom16,
                  fontWeight: 500,
                }}
              >
                {t.cta}
              </Text>
              <Link
                href={duplicateDealUrl}
                style={{
                  backgroundColor: tokens.brandPrimary700,
                  color: tokens.brandOnPrimary,
                  padding: spacing.padButton,
                  borderRadius: '6px',
                  fontFamily,
                  fontSize: spacing.px15,
                  fontWeight: 600,
                  textDecoration: 'none',
                  display: 'inline-block',
                }}
              >
                {t.ctaLink}
              </Link>
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
