/**
 * PolicyViolation.tsx - FDS §9.8 Vendor Policy Violation Email
 *
 * Sent to vendor on admin action: warning / freeze / ban.
 */

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 type PolicyAction = 'WARNING' | 'FREEZE' | 'BAN';

export interface PolicyViolationProps {
  vendorName: string;
  action: PolicyAction;
  reason: string;
  nextSteps: string;
  supportEmail?: string;
  appealUrl?: string;
  locale?: Locale;
}

const actionColors: Record<PolicyAction, string> = {
  WARNING: '#d97706', // warning-600
  FREEZE: '#1d4ed8', // brand-primary-700
  BAN: '#991b1b', // custom red
};

const copy = {
  he: {
    preview: (action: PolicyAction) =>
      action === 'WARNING'
        ? 'קיבלת אזהרה מ-Multideal'
        : action === 'FREEZE'
          ? 'החשבון שלך הוקפא זמנית'
          : 'החשבון שלך הושעה',
    heading: (action: PolicyAction) =>
      action === 'WARNING' ? 'אזהרה' : action === 'FREEZE' ? 'הקפאת חשבון' : 'השעיית חשבון',
    reasonLabel: 'סיבה',
    nextStepsLabel: 'מה לעשות עכשיו',
    support: 'לפנייה לתמיכה:',
    appeal: 'ערעור',
  },
  en: {
    preview: (action: PolicyAction) =>
      action === 'WARNING'
        ? 'You have received a warning from Multideal'
        : action === 'FREEZE'
          ? 'Your account has been temporarily frozen'
          : 'Your account has been suspended',
    heading: (action: PolicyAction) =>
      action === 'WARNING'
        ? 'Policy Warning'
        : action === 'FREEZE'
          ? 'Account Frozen'
          : 'Account Suspended',
    reasonLabel: 'Reason',
    nextStepsLabel: 'What to do next',
    support: 'Contact support:',
    appeal: 'Submit an appeal',
  },
} as const;

export default function PolicyViolation({
  vendorName: _vendorName,
  action,
  reason,
  nextSteps,
  supportEmail = 'support@multideal.co.il',
  appealUrl,
  locale = 'he',
}: PolicyViolationProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);
  const fontFamily = locale === 'he' ? fonts.he : fonts.en;
  const headerBg = actionColors[action] ?? tokens.warning600;

  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(action)}</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: headerBg,
                padding: spacing.padHeaderLg,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text
                style={{
                  fontSize: spacing.px20,
                  fontWeight: 700,
                  color: tokens.brandOnPrimary,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.heading(action)}
              </Text>
            </Section>

            {/* Body */}
            <Section
              style={{
                padding: spacing.padSectionLg,
                direction: dir,
                textAlign: locale === 'he' ? 'right' : 'left',
              }}
            >
              <Text style={labelStyle}>{t.reasonLabel}</Text>
              <Text
                style={{
                  fontSize: spacing.px15,
                  color: tokens.textPrimary,
                  fontFamily,
                  margin: '0',
                  lineHeight: '1.6',
                  backgroundColor: tokens.neutral100,
                  padding: spacing.px12,
                  borderRadius: '6px',
                }}
              >
                {reason}
              </Text>

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

              <Text style={labelStyle}>{t.nextStepsLabel}</Text>
              <Text
                style={{
                  fontSize: spacing.px15,
                  color: tokens.textSecondary,
                  fontFamily,
                  margin: '0',
                  lineHeight: '1.6',
                }}
              >
                {nextSteps}
              </Text>

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

              <Text
                style={{
                  fontSize: spacing.px13,
                  color: tokens.textMuted,
                  fontFamily,
                  margin: '0',
                }}
              >
                {t.support}{' '}
                <Link
                  href={`mailto:${supportEmail}`}
                  style={{ color: tokens.textLink, textDecoration: 'none' }}
                >
                  {supportEmail}
                </Link>
              </Text>

              {appealUrl ? (
                <div style={{ marginTop: spacing.px16 }}>
                  <Link
                    href={appealUrl}
                    style={{
                      backgroundColor: tokens.neutral200,
                      color: tokens.textPrimary,
                      padding: spacing.padTag,
                      borderRadius: '6px',
                      fontFamily,
                      fontSize: spacing.px14,
                      fontWeight: 500,
                      textDecoration: 'none',
                      display: 'inline-block',
                    }}
                  >
                    {t.appeal}
                  </Link>
                </div>
              ) : null}
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
