export interface ElectronPrintToPDFOptions {
  pageSize: {
    width: number;  // Microns: 6in * 25,400 = 152,400
    height: number; // Microns: 9in * 25,400 = 228,600
  };
  marginsType: 0; // 0 = None (CSS @page handles margins)
  printBackground: boolean;
  preferCSSPageSize?: boolean;
}

export const BOOK_6X9_PRINT_OPTIONS: ElectronPrintToPDFOptions = {
  pageSize: {
    width: 152400,  // 6 inches in microns
    height: 228600, // 9 inches in microns
  },
  marginsType: 0,
  printBackground: true,
  preferCSSPageSize: true,
};

export interface PdfRenderResult {
  pdfBuffer: Buffer;
  pageCount: number;
  widthMm: number;
  heightMm: number;
}

/**
 * Renders HTML string to PDF buffer using Electron webContents printToPDF API.
 */
export async function renderHtmlTo6x9Pdf(
  htmlContent: string,
  cssStyles: string,
  electronWebContentsPrintToPDF: (options: ElectronPrintToPDFOptions) => Promise<Buffer>
): Promise<Buffer> {
  // Wrap HTML into full document container with print media styles
  const fullDocumentHtml = `
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <style>
          ${cssStyles}
        </style>
      </head>
      <body>
        ${htmlContent}
      </body>
    </html>
  `;

  // Note: Caller in Obsidian plugin will load HTML into a hidden Electron WebContents window or offscreen renderer
  // and invoke printToPDF(BOOK_6X9_PRINT_OPTIONS).
  const pdfBuffer = await electronWebContentsPrintToPDF(BOOK_6X9_PRINT_OPTIONS);
  return pdfBuffer;
}
