import type { Template } from '@pdfme/common';

export const clientSummaryTemplate: Template = {
  basePdf: 'BLANK_PDF' as unknown as string,
  schemas: [
    [
      {
        name: 'portName',
        type: 'text',
        position: { x: 20, y: 15 },
        width: 100,
        height: 10,
        fontSize: 16,
      },
      {
        name: 'title',
        type: 'text',
        position: { x: 20, y: 30 },
        width: 170,
        height: 8,
        fontSize: 14,
      },
      {
        name: 'clientInfo',
        type: 'text',
        position: { x: 20, y: 45 },
        width: 80,
        height: 40,
        fontSize: 9,
      },
      {
        name: 'contacts',
        type: 'text',
        position: { x: 110, y: 45 },
        width: 80,
        height: 40,
        fontSize: 9,
      },
      {
        name: 'yachts',
        type: 'text',
        position: { x: 20, y: 90 },
        width: 170,
        height: 25,
        fontSize: 9,
      },
      {
        name: 'interests',
        type: 'text',
        position: { x: 20, y: 120 },
        width: 170,
        height: 80,
        fontSize: 8,
      },
      {
        name: 'recentActivity',
        type: 'text',
        position: { x: 20, y: 205 },
        width: 170,
        height: 60,
        fontSize: 8,
      },
      {
        name: 'generatedAt',
        type: 'text',
        position: { x: 20, y: 275 },
        width: 170,
        height: 6,
        fontSize: 7,
      },
    ],
  ],
};

export interface YachtSummary {
  name: string;
  lengthFt: string | null;
  widthFt: string | null;
  draftFt: string | null;
  lengthM: string | null;
  widthM: string | null;
  draftM: string | null;
}

export function buildClientSummaryInputs(
  client: Record<string, unknown>,
  contacts: Record<string, unknown>[],
  yachtList: YachtSummary[],
  interestList: Record<string, unknown>[],
  activity: Record<string, unknown>[],
  port: Record<string, unknown>,
): Record<string, string> {
  const clientInfo = [
    `Name: ${client.fullName ?? 'N/A'}`,
    client.nationality ? `Nationality: ${client.nationality}` : null,
    client.source ? `Source: ${client.source}` : null,
    `Added: ${new Date(client.createdAt as string | Date).toLocaleDateString('en-GB')}`,
  ]
    .filter(Boolean)
    .join('\n');

  const contactsText =
    contacts.length > 0
      ? contacts
          .map(
            (c) =>
              `${(c.channel as string).charAt(0).toUpperCase() + (c.channel as string).slice(1)}${c.isPrimary ? ' (primary)' : ''}: ${c.value}${c.label ? ` [${c.label}]` : ''}`,
          )
          .join('\n')
      : 'No contacts on file';

  const yachtsText =
    yachtList.length > 0
      ? `Owned/Linked Yachts:\n${yachtList
          .map((y) => {
            const dims = [
              y.lengthFt ? `${y.lengthFt}ft` : y.lengthM ? `${y.lengthM}m` : null,
              y.widthFt ? `${y.widthFt}ft beam` : null,
              y.draftFt ? `${y.draftFt}ft draft` : null,
            ]
              .filter(Boolean)
              .join(' · ');
            return `• ${y.name}${dims ? ` (${dims})` : ''}`;
          })
          .join('\n')}`
      : 'No yachts linked to this client';

  const interestsText =
    interestList.length > 0
      ? interestList
          .map(
            (i) =>
              `• ${i.pipelineStage ?? 'open'}${i.berthMooringNumber ? ` — Berth ${i.berthMooringNumber}` : ''}${i.leadCategory ? ` [${i.leadCategory}]` : ''}  (${new Date(i.createdAt as string | Date).toLocaleDateString('en-GB')})`,
          )
          .join('\n')
      : 'No pipeline interests on file';

  const activityText =
    activity.length > 0
      ? activity
          .map(
            (a) =>
              `${new Date(a.createdAt as string | Date).toLocaleDateString('en-GB')}  ${a.action}  ${a.entityType}${a.fieldChanged ? `  (${a.fieldChanged})` : ''}`,
          )
          .join('\n')
      : 'No recent activity';

  return {
    portName: (port?.name as string) ?? 'Port Nimara',
    title: `Client Summary — ${client.fullName ?? ''}`,
    clientInfo,
    contacts: contactsText,
    yachts: yachtsText,
    interests: `Pipeline Interests:\n${interestsText}`,
    recentActivity: `Recent Activity:\n${activityText}`,
    generatedAt: `Generated: ${new Date().toLocaleString('en-GB')}`,
  };
}
