import { Link, Text, render } from '@react-email/components';
import * as React from 'react';

import { brandingPrimaryColor, renderShell, safeUrl, type BrandingShell } from '@/lib/email/shell';

export interface InquirySalesNotificationData {
  fullName: string;
  email: string;
  phone: string;
  mooringNumber: string | null;
  crmUrl: string;
  portName?: string;
}

interface RenderOpts {
  branding?: BrandingShell | null;
  subject?: string | null;
}

function SalesNotificationBody({
  fullName,
  email,
  phone,
  mooringDisplay,
  crmUrl,
  portName,
  accent,
}: {
  fullName: string;
  email: string;
  phone: string;
  mooringDisplay: string;
  crmUrl: string;
  portName: string;
  accent: string;
}) {
  const detailStyle = { margin: '0 0 0', fontSize: '16px' } as const;
  return (
    <>
      <Text style={{ marginBottom: '10px', fontSize: '16px' }}>Dear Administrator,</Text>
      <Text style={{ marginBottom: '10px', fontSize: '16px' }}>
        {fullName} has expressed their interest in <strong>{portName}</strong>. Here are their
        details:
      </Text>
      <Text style={detailStyle}>
        <strong>Name:</strong> {fullName}
      </Text>
      <Text style={detailStyle}>
        <strong>Email:</strong> {email}
      </Text>
      <Text style={detailStyle}>
        <strong>Telephone:</strong> {phone}
      </Text>
      <Text style={{ margin: '0 0 16px 0', fontSize: '16px' }}>
        <strong>Berths Selected:</strong> {mooringDisplay}
      </Text>
      <Text style={{ marginBottom: '10px', fontSize: '16px' }}>
        Please visit the{' '}
        <Link href={safeUrl(crmUrl)} style={{ color: accent, textDecoration: 'underline' }}>
          {portName} CRM
        </Link>{' '}
        to view more information.
      </Text>
      <Text style={{ fontSize: '16px' }}>
        Thank you,
        <br />
        {portName} CRM
      </Text>
    </>
  );
}

export async function inquirySalesNotification(
  data: InquirySalesNotificationData,
  overrides?: RenderOpts,
) {
  const portName = data.portName ?? 'Port Nimara';
  const mooringDisplay = data.mooringNumber || 'None';
  const subject = overrides?.subject?.trim() ? overrides.subject : `New Interest - ${portName}`;
  const accent = brandingPrimaryColor(overrides?.branding);

  const body = await render(
    <SalesNotificationBody
      fullName={data.fullName}
      email={data.email}
      phone={data.phone}
      mooringDisplay={mooringDisplay}
      crmUrl={data.crmUrl}
      portName={portName}
      accent={accent}
    />,
    { pretty: false },
  );

  const text = [
    'Dear Administrator,',
    '',
    `${data.fullName} has expressed their interest in ${portName}. Here are their details:`,
    '',
    `Name: ${data.fullName}`,
    `Email: ${data.email}`,
    `Telephone: ${data.phone}`,
    `Berths Selected: ${mooringDisplay}`,
    '',
    `Please visit the ${portName} CRM (${data.crmUrl}) to view more information.`,
    '',
    'Thank you',
    `${portName} CRM`,
  ].join('\n');

  return {
    subject,
    html: renderShell({ title: subject, body, branding: overrides?.branding }),
    text,
  };
}
