/**
 * MonthlySettlement.tsx - FDS §9.7 Monthly Settlement Email (to vendor)
 *
 * MONOSPACE RECEIPT-PRINTER AESTHETIC.
 * ASCII art logo at top: //MULTIDEAL//
 * Cold, factual, with a small ❤ at the end.
 */

import React from 'react';
import { Html, Head, Body, Container, Section, Text, 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 MonthlySettlementProps {
  periodLabel: string;
  revenue: string;
  customers: number;
  returnedCount: number;
  commission: string;
  payout: string;
  insights: string[];
  vendorName: string;
  locale?: Locale;
}

const copy = {
  he: {
    preview: (period: string) => `דוח חיובים חודשי - ${period}`,
    revenue: 'הכנסות מעסקאות',
    customers: 'לקוחות שרכשו',
    returned: 'חזרו לקנות',
    commission: 'עמלת Multideal',
    payout: 'תשלום אליך',
    insightsTitle: 'תובנות',
    currency: '₪',
    mission: 'Multideal תומכת בכלכלה המקומית - כל עסקה מחברת שכן לעסק.',
  },
  en: {
    preview: (period: string) => `Monthly Settlement Report - ${period}`,
    revenue: 'Deal Revenue',
    customers: 'Customers',
    returned: 'Returned to Buy',
    commission: 'Multideal Commission',
    payout: 'Your Payout',
    insightsTitle: 'Insights',
    currency: '₪',
    mission:
      'Multideal supports the local economy - every deal connects a neighbour to a business.',
  },
} as const;

const ASCII_LOGO = '//MULTIDEAL//';
const DIVIDER = '--------------------------------';

export default function MonthlySettlement({
  periodLabel,
  revenue,
  customers,
  returnedCount,
  commission,
  payout,
  insights,
  vendorName: _vendorName,
  locale = 'he',
}: MonthlySettlementProps) {
  const t = copy[locale];
  const dir = dirForLocale(locale);

  // Monospace receipt - always LTR layout so columns align, regardless of locale.
  // The content language is still set via lang attribute for screen readers.
  const monoStyle: React.CSSProperties = {
    fontFamily: fonts.mono,
    fontSize: spacing.px14,
    lineHeight: '1.8',
    color: tokens.textPrimary,
    backgroundColor: tokens.neutral50,
    whiteSpace: 'pre',
  };

  const dimStyle: React.CSSProperties = {
    ...monoStyle,
    color: tokens.textMuted,
  };

  const boldStyle: React.CSSProperties = {
    ...monoStyle,
    fontWeight: 700,
  };

  const bigPayoutStyle: React.CSSProperties = {
    ...boldStyle,
    fontSize: spacing.px20,
    color: tokens.brandPrimary700,
  };

  // Pad a label to fixed width so values align (30 chars)
  function row(label: string, value: string): string {
    const maxLabel = 22;
    const padded = label.length >= maxLabel ? label : label + '.'.repeat(maxLabel - label.length);
    return `${padded} ${value}`;
  }

  return (
    <Html lang={locale} dir={dir}>
      <Head />
      <Preview>{t.preview(periodLabel)}</Preview>
      <Body style={{ backgroundColor: tokens.neutral200, fontFamily: fonts.mono }}>
        <Container style={{ padding: spacing.padPage }}>
          <div
            style={{
              maxWidth: spacing.cardMaxNarrow,
              margin: '0 auto',
              backgroundColor: tokens.neutral50,
              borderRadius: '4px',
              border: `1px solid ${tokens.neutral300}`,
              overflow: 'hidden',
            }}
          >
            <Section style={{ padding: spacing.padSectionLg }}>
              {/* ASCII logo */}
              <Text
                style={{
                  ...boldStyle,
                  fontSize: spacing.px22,
                  letterSpacing: '0.15em',
                  margin: '0 0 4px',
                }}
              >
                {ASCII_LOGO}
              </Text>

              {/* Period header */}
              <Text style={{ ...dimStyle, margin: '0 0 4px' }}>{DIVIDER}</Text>
              <Text style={{ ...monoStyle, margin: '0 0 4px' }}>{periodLabel}</Text>
              <Text style={{ ...dimStyle, margin: spacing.marginBottom16 }}>{DIVIDER}</Text>

              {/* Revenue metrics */}
              <Text style={{ ...monoStyle, margin: '0 0 4px' }}>
                {row(t.revenue, `${t.currency}${revenue}`)}
              </Text>
              <Text style={{ ...monoStyle, margin: '0 0 4px' }}>
                {row(t.customers, String(customers))}
              </Text>
              <Text style={{ ...monoStyle, margin: '0 0 4px' }}>
                {row(t.returned, String(returnedCount))}
              </Text>

              <Text style={{ ...dimStyle, margin: spacing.marginTop12Bottom4 }}>{DIVIDER}</Text>

              {/* Commission & payout */}
              <Text style={{ ...monoStyle, margin: '0 0 4px' }}>
                {row(t.commission, `${t.currency}${commission}`)}
              </Text>

              <Text style={{ ...dimStyle, margin: '4px 0 4px' }}>{DIVIDER}</Text>

              {/* Payout - larger */}
              <Text style={{ ...bigPayoutStyle, margin: spacing.marginInsetTop4Bottom16 }}>
                {row(t.payout, `${t.currency}${payout}`)}
              </Text>

              {/* Insights */}
              {insights.length > 0 ? (
                <>
                  <Text style={{ ...dimStyle, margin: '0 0 4px' }}>{DIVIDER}</Text>
                  <Text style={{ ...boldStyle, margin: '0 0 4px' }}>{t.insightsTitle}</Text>
                  {insights.map((insight) => (
                    <Text key={insight} style={{ ...monoStyle, margin: '0 0 2px' }}>
                      {'> '}
                      {insight}
                    </Text>
                  ))}
                  <Text style={{ ...dimStyle, margin: '4px 0 0' }}>{DIVIDER}</Text>
                </>
              ) : (
                <Text style={{ ...dimStyle, margin: '0 0 0' }}>{DIVIDER}</Text>
              )}

              {/* Mission closing line */}
              <Text
                style={{
                  ...dimStyle,
                  fontSize: spacing.px12,
                  margin: spacing.marginTop16,
                  lineHeight: '1.5',
                }}
              >
                {t.mission}
              </Text>

              {/* Small heart */}
              <Text
                style={{
                  fontFamily: fonts.mono,
                  fontSize: spacing.px18,
                  color: tokens.brandPrimary700,
                  margin: spacing.marginTop12,
                }}
              >
                ❤
              </Text>
            </Section>
          </div>
        </Container>
      </Body>
    </Html>
  );
}
