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

export const berthSpecTemplate: 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: 'berthInfo', type: 'text', position: { x: 20, y: 45 }, width: 80, height: 25, fontSize: 9 },
      { name: 'dimensions', type: 'text', position: { x: 110, y: 45 }, width: 80, height: 25, fontSize: 9 },
      { name: 'pricing', type: 'text', position: { x: 20, y: 75 }, width: 80, height: 20, fontSize: 9 },
      { name: 'tenure', type: 'text', position: { x: 110, y: 75 }, width: 80, height: 20, fontSize: 9 },
      { name: 'infrastructure', type: 'text', position: { x: 20, y: 100 }, width: 170, height: 25, fontSize: 9 },
      { name: 'waitingList', type: 'text', position: { x: 20, y: 130 }, width: 170, height: 50, fontSize: 8 },
      { name: 'maintenanceLog', type: 'text', position: { x: 20, y: 185 }, width: 170, height: 75, fontSize: 8 },
      { name: 'generatedAt', type: 'text', position: { x: 20, y: 275 }, width: 170, height: 6, fontSize: 7 },
    ],
  ],
};

export function buildBerthSpecInputs(
  berth: Record<string, unknown>,
  waitingList: Record<string, unknown>[],
  maintenance: Record<string, unknown>[],
  linkedInterests: Record<string, unknown>[],
  port: Record<string, unknown>,
): Record<string, string> {
  const berthInfo = [
    `Mooring: ${berth.mooringNumber}`,
    berth.area ? `Area: ${berth.area}` : null,
    `Status: ${berth.status ?? 'available'}`,
    berth.nominalBoatSize ? `Nominal boat size: ${berth.nominalBoatSize}` : null,
    berth.bowFacing ? `Bow facing: ${berth.bowFacing}` : null,
  ]
    .filter(Boolean)
    .join('\n');

  const dimensions = [
    berth.lengthFt
      ? `Length: ${berth.lengthFt}ft${berth.lengthM ? ` / ${berth.lengthM}m` : ''}`
      : null,
    berth.widthFt
      ? `Beam: ${berth.widthFt}ft${berth.widthM ? ` / ${berth.widthM}m` : ''}${berth.widthIsMinimum ? ' (min)' : ''}`
      : null,
    berth.draftFt
      ? `Draft: ${berth.draftFt}ft${berth.draftM ? ` / ${berth.draftM}m` : ''}`
      : null,
    berth.waterDepth
      ? `Water depth: ${berth.waterDepth}ft${berth.waterDepthM ? ` / ${berth.waterDepthM}m` : ''}${berth.waterDepthIsMinimum ? ' (min)' : ''}`
      : null,
  ]
    .filter(Boolean)
    .join('\n') || 'Dimensions not specified';

  const pricing = berth.price
    ? `Price: ${berth.priceCurrency ?? 'USD'} ${Number(berth.price).toLocaleString()}`
    : 'Price: TBD';

  const tenure = [
    `Tenure type: ${berth.tenureType ?? 'permanent'}`,
    berth.tenureYears ? `Tenure years: ${berth.tenureYears}` : null,
    berth.tenureStartDate ? `Start: ${berth.tenureStartDate}` : null,
    berth.tenureEndDate ? `End: ${berth.tenureEndDate}` : null,
  ]
    .filter(Boolean)
    .join('\n');

  const infrastructure = [
    berth.mooringType ? `Mooring type: ${berth.mooringType}` : null,
    berth.powerCapacity ? `Power: ${berth.powerCapacity}${berth.voltage ? ` / ${berth.voltage}V` : ''}` : null,
    berth.cleatType ? `Cleat: ${berth.cleatType}${berth.cleatCapacity ? ` (${berth.cleatCapacity})` : ''}` : null,
    berth.bollardType
      ? `Bollard: ${berth.bollardType}${berth.bollardCapacity ? ` (${berth.bollardCapacity})` : ''}`
      : null,
    berth.sidePontoon ? `Side pontoon: ${berth.sidePontoon}` : null,
    berth.access ? `Access: ${berth.access}` : null,
  ]
    .filter(Boolean)
    .join('  |  ') || 'Infrastructure details not specified';

  const waitingListText =
    waitingList.length > 0
      ? waitingList
          .map(
            (w) =>
              `${w.position}. ${w.clientName ?? 'Unknown'}${w.priority === 'high' ? ' [HIGH]' : ''}${w.notes ? `  — ${w.notes}` : ''}`,
          )
          .join('\n')
      : 'No clients on waiting list';

  const maintenanceText =
    maintenance.length > 0
      ? maintenance
          .map(
            (m) =>
              `${m.performedDate}  [${m.category}]  ${m.description}${m.cost ? `  Cost: ${m.costCurrency ?? 'USD'} ${Number(m.cost).toLocaleString()}` : ''}`,
          )
          .join('\n')
      : 'No maintenance records';

  return {
    portName: (port?.name as string) ?? 'Port Nimara',
    title: `Berth Specification — Mooring ${berth.mooringNumber}`,
    berthInfo,
    dimensions,
    pricing,
    tenure,
    infrastructure,
    waitingList: `Waiting List (${waitingList.length}):\n${waitingListText}`,
    maintenanceLog: `Maintenance Log (last ${maintenance.length}):\n${maintenanceText}`,
    generatedAt: `Generated: ${new Date().toLocaleString('en-GB')}`,
  };
}
