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

export const invoiceTemplate: Template = {
  basePdf: 'BLANK_PDF' as unknown as string,
  schemas: [
    [
      // Header fields
      {
        name: 'portName',
        type: 'text',
        position: { x: 20, y: 15 },
        width: 100,
        height: 10,
        fontSize: 16,
      },
      {
        name: 'invoiceTitle',
        type: 'text',
        position: { x: 140, y: 15 },
        width: 50,
        height: 10,
        fontSize: 16,
      },
      {
        name: 'invoiceNumber',
        type: 'text',
        position: { x: 140, y: 27 },
        width: 50,
        height: 6,
        fontSize: 10,
      },
      {
        name: 'invoiceDate',
        type: 'text',
        position: { x: 140, y: 35 },
        width: 50,
        height: 6,
        fontSize: 10,
      },
      {
        name: 'dueDate',
        type: 'text',
        position: { x: 140, y: 43 },
        width: 50,
        height: 6,
        fontSize: 10,
      },
      // Client info
      {
        name: 'clientInfo',
        type: 'text',
        position: { x: 20, y: 55 },
        width: 100,
        height: 20,
        fontSize: 10,
      },
      // Line items as text block
      {
        name: 'lineItems',
        type: 'text',
        position: { x: 20, y: 85 },
        width: 170,
        height: 120,
        fontSize: 9,
      },
      // Totals
      {
        name: 'totals',
        type: 'text',
        position: { x: 110, y: 215 },
        width: 80,
        height: 30,
        fontSize: 10,
      },
      // Notes
      {
        name: 'notes',
        type: 'text',
        position: { x: 20, y: 250 },
        width: 170,
        height: 20,
        fontSize: 8,
      },
    ],
  ],
};

export function buildInvoiceInputs(
  invoice: Record<string, unknown>,
  lineItems: Record<string, unknown>[],
  port: Record<string, unknown>,
): Record<string, string> {
  const itemLines = lineItems
    .map(
      (li, i) =>
        `${i + 1}. ${li.description}  |  Qty: ${li.quantity}  |  Unit: ${invoice.currency} ${Number(li.unitPrice).toFixed(2)}  |  Total: ${invoice.currency} ${Number(li.total).toFixed(2)}`,
    )
    .join('\n');

  let totalsText = `Subtotal: ${invoice.currency} ${Number(invoice.subtotal).toFixed(2)}`;
  if (Number(invoice.discountAmount) > 0) {
    totalsText += `\nDiscount (${invoice.discountPct}%): -${invoice.currency} ${Number(invoice.discountAmount).toFixed(2)}`;
  }
  if (Number(invoice.feeAmount) > 0) {
    totalsText += `\nFee (${invoice.feePct}%): +${invoice.currency} ${Number(invoice.feeAmount).toFixed(2)}`;
  }
  totalsText += `\n─────────────\nTOTAL: ${invoice.currency} ${Number(invoice.total).toFixed(2)}`;

  return {
    portName: (port?.name as string) ?? 'Port Nimara',
    invoiceTitle: 'INVOICE',
    invoiceNumber: invoice.invoiceNumber as string,
    invoiceDate: `Date: ${new Date(invoice.createdAt as string | Date).toLocaleDateString('en-GB')}`,
    dueDate: `Due: ${invoice.dueDate}`,
    clientInfo: `${invoice.clientName}\n${invoice.billingEmail ?? ''}\n${invoice.billingAddress ?? ''}`.trim(),
    lineItems: itemLines || 'No line items',
    totals: totalsText,
    notes: invoice.notes ? `Notes: ${invoice.notes}` : '',
  };
}
