import { jsPDF } from 'jspdf'
import type { Invoice } from '../stores/invoices'
import type { Client } from '../stores/clients'
import { renderInvoicePdf, type BusinessInfo } from './invoicePdfRenderer'
import { getTemplateById } from './invoiceTemplates'

/**
 * Invoice item interface for line items on the invoice
 */
export interface InvoiceItem {
  id?: number
  description: string
  quantity: number
  rate: number
  amount: number
}

/**
 * Generate a PDF invoice using a template
 * @param invoice - The invoice data
 * @param client - The client data
 * @param items - The invoice line items
 * @param templateId - Template ID to use (defaults to 'clean-minimal')
 * @param businessInfo - Optional business identity info
 * @returns The generated jsPDF document
 */
export function generateInvoicePdf(
  invoice: Invoice,
  client: Client,
  items: InvoiceItem[],
  templateId: string = 'clean',
  businessInfo?: BusinessInfo,
): jsPDF {
  const config = getTemplateById(templateId)
  const info: BusinessInfo = businessInfo || { name: '', address: '', email: '', phone: '', logo: '' }
  return renderInvoicePdf(config, invoice, client, items, info)
}
