import * as plugins from './plugins.js';
import type { LanguageModelV3 } from '@ai-sdk/provider';
import type { ModelMessage } from 'ai';

let smartpdfInstance: InstanceType<typeof plugins.smartpdf.SmartPdf> | null = null;

async function ensureSmartpdf(): Promise<InstanceType<typeof plugins.smartpdf.SmartPdf>> {
  if (!smartpdfInstance) {
    smartpdfInstance = new plugins.smartpdf.SmartPdf();
    await smartpdfInstance.start();
  }
  return smartpdfInstance;
}

export interface IDocumentOptions {
  model: LanguageModelV3;
  systemMessage?: string;
  userMessage: string;
  pdfDocuments: Uint8Array[];
  messageHistory?: ModelMessage[];
}

export async function analyzeDocuments(options: IDocumentOptions): Promise<string> {
  const pdf = await ensureSmartpdf();

  const imagePages: Uint8Array[] = [];
  for (const doc of options.pdfDocuments) {
    const pages = await pdf.convertPDFToPngBytes(doc);
    imagePages.push(...pages);
  }

  // Filter out empty buffers
  const validPages = imagePages.filter(page => page && page.length > 0);

  const result = await plugins.generateText({
    model: options.model,
    system: options.systemMessage,
    messages: [
      ...(options.messageHistory ?? []),
      {
        role: 'user',
        content: [
          { type: 'text', text: options.userMessage },
          ...validPages.map(page => ({
            type: 'image' as const,
            image: page,
            mimeType: 'image/png' as const,
          })),
        ],
      },
    ],
  });
  return result.text;
}

export async function stopSmartpdf(): Promise<void> {
  if (smartpdfInstance) {
    await smartpdfInstance.stop();
    smartpdfInstance = null;
  }
}
