import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as qenv from '@push.rocks/qenv';
import * as smartrequest from '@push.rocks/smartrequest';
import { SmartFs, SmartFsProviderNode } from '@push.rocks/smartfs';

const testQenv = new qenv.Qenv('./', './.nogit/');
const smartfs = new SmartFs(new SmartFsProviderNode());

import * as smartai from '../ts/index.js';

let mistralProvider: smartai.MistralProvider;

tap.test('Mistral Document: should create and start Mistral provider', async () => {
  mistralProvider = new smartai.MistralProvider({
    mistralToken: await testQenv.getEnvVarOnDemand('MISTRAL_API_KEY'),
    tableFormat: 'markdown',
  });
  await mistralProvider.start();
  expect(mistralProvider).toBeInstanceOf(smartai.MistralProvider);
});

tap.test('Mistral Document: should process a PDF document', async () => {
  const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
  const pdfResponse = await smartrequest.SmartRequest.create()
    .url(pdfUrl)
    .get();

  const result = await mistralProvider.document({
    systemMessage: 'Classify the document. Only the following answers are allowed: "invoice", "bank account statement", "contract", "test document", "other". The answer should only contain the keyword for machine use.',
    userMessage: 'Classify this document.',
    messageHistory: [],
    pdfDocuments: [Buffer.from(await pdfResponse.arrayBuffer())],
  });

  console.log(`Mistral Document - Result:`, result);
  expect(result).toBeTruthy();
  expect(result.message).toBeTruthy();
});

tap.test('Mistral Document: should handle complex document analysis', async () => {
  // Test with the demo PDF if it exists
  const pdfPath = './.nogit/demo_without_textlayer.pdf';
  let pdfBuffer: Uint8Array;

  try {
    pdfBuffer = await smartfs.file(pdfPath).read();
  } catch (error) {
    // If the file doesn't exist, use the dummy PDF
    console.log('Demo PDF not found, using dummy PDF instead');
    const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
    const pdfResponse = await smartrequest.SmartRequest.create()
      .url(pdfUrl)
      .get();
    pdfBuffer = Buffer.from(await pdfResponse.arrayBuffer());
  }

  const result = await mistralProvider.document({
    systemMessage: `
      Analyze this document and provide a JSON response with the following structure:
      {
        "documentType": "string",
        "hasText": boolean,
        "summary": "string"
      }
    `,
    userMessage: 'Analyze this document.',
    messageHistory: [],
    pdfDocuments: [pdfBuffer],
  });

  console.log(`Mistral Complex Document Analysis:`, result);
  expect(result).toBeTruthy();
  expect(result.message).toBeTruthy();
});

tap.test('Mistral Document: should process multiple PDF documents', async () => {
  const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
  const pdfResponse = await smartrequest.SmartRequest.create()
    .url(pdfUrl)
    .get();

  const pdfBuffer = Buffer.from(await pdfResponse.arrayBuffer());

  const result = await mistralProvider.document({
    systemMessage: 'You are a document comparison assistant.',
    userMessage: 'Are these two documents the same? Answer yes or no.',
    messageHistory: [],
    pdfDocuments: [pdfBuffer, pdfBuffer], // Same document twice for test
  });

  console.log(`Mistral Multi-Document - Result:`, result);
  expect(result).toBeTruthy();
  expect(result.message).toBeTruthy();
});

tap.test('Mistral Document: should stop the provider', async () => {
  await mistralProvider.stop();
});

export default tap.start();
