/**
 * DealRejected.tsx - FDS §9.5 Deal Rejected Email
 *
 * Sent to vendor when admin rejects a deal.
 * Includes rejection reason + optional detail, and a link to edit/resubmit.
 */

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

export interface DealRejectedProps {
  dealTitle: string;
  rejectionReason: string;
  rejectionDetail?: string;
  dashboardUrl: string;
  vendorName: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: (title: string) => `העסקה "${title}" נדחתה - ניתן לערוך ולהגיש מחדש`,
    heading: 'העסקה נדחתה',
    body: (title: string) => `העסקה "${title}" לא אושרה. פרטים למטה - ניתן לערוך ולהגיש מחדש.`,
    reasonLabel: 'סיבת הדחייה',
    detailLabel: 'פרטים נוספים',
    cta: 'ערוך והגש מחדש',
  },
  en: {
    preview: (title: string) => `Your deal "${title}" was rejected - you can edit and resubmit`,
    heading: 'Deal Rejected',
    body: (title: string) =>
      `"${title}" was not approved. See the reason below - you can edit and resubmit.`,
    reasonLabel: 'Rejection Reason',
    detailLabel: 'Additional Detail',
    cta: 'Edit and resubmit',
  },
} as const;

export default function DealRejected({
  dealTitle,
  rejectionReason,
  rejectionDetail,
  dashboardUrl,
  vendorName: _vendorName,
  locale = 'he',
}: DealRejectedProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;

  const labelStyle: React.CSSProperties = {
    fontSize: spacing.px11,
    color: tokens.textMuted,
    fontFamily,
    textTransform: 'uppercase',
    letterSpacing: '0.06em',
    marginBottom: '4px',
    marginTop: spacing.px16,
  };

  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.warning600,
                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.marginBottom20,
                  lineHeight: '1.6',
                }}
              >
                {t.body(dealTitle)}
              </Text>

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

              <Text style={labelStyle}>{t.reasonLabel}</Text>
              <Text
                style={{
                  fontSize: spacing.px15,
                  color: tokens.textPrimary,
                  fontFamily,
                  fontWeight: 500,
                  margin: '0',
                }}
              >
                {rejectionReason}
              </Text>

              {rejectionDetail ? (
                <>
                  <Text style={labelStyle}>{t.detailLabel}</Text>
                  <Text
                    style={{
                      fontSize: spacing.px14,
                      color: tokens.textSecondary,
                      fontFamily,
                      margin: '0',
                      backgroundColor: tokens.neutral100,
                      padding: spacing.px12,
                      borderRadius: '6px',
                      lineHeight: '1.6',
                    }}
                  >
                    {rejectionDetail}
                  </Text>
                </>
              ) : null}

              <div
                style={{
                  margin: spacing.marginTop28,
                  textAlign: locale === 'he' ? 'right' : 'left',
                }}
              >
                <Link
                  href={dashboardUrl}
                  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>
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
