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

import { logger } from '@/lib/logger';

/**
 * Generates a PDF from a @pdfme template and input data.
 *
 * @param template  The @pdfme template definition (basePdf + schemas)
 * @param inputs    Array of input objects matching the template schema fields
 * @returns         Raw PDF bytes
 */
export async function generatePdf(
  template: Template,
  inputs: Record<string, string>[],
): Promise<Uint8Array> {
  try {
    const pdf = await generate({ template, inputs });
    return pdf;
  } catch (err) {
    logger.error({ err }, 'PDF generation failed');
    throw new Error('Failed to generate PDF');
  }
}
