import { tap, expect } from '@push.rocks/tapbundle';
import * as getInvoices from './assets/getasset.js';
import { FacturXEncoder } from '../ts/formats/facturx.encoder.js';

// Sample test letter data
const testLetterData = getInvoices.letterObjects.letter1.demoLetter;

// Test generating XML from letter data
tap.test('Generate Factur-X XML from letter data', async () => {
  // Create an encoder instance
  const encoder = new FacturXEncoder();
  
  // Generate XML
  let xmlString: string | null = null;
  try {
    xmlString = await encoder.createFacturXXml(testLetterData);
  } catch (error) {
    console.error('Error creating XML:', error);
    tap.fail('Error creating XML: ' + error.message);
  }
  
  // Verify XML was created
  expect(xmlString).toBeTypeOf('string');
  
  if (xmlString) {
    // Check XML basic structure
    expect(xmlString).toInclude('<?xml version="1.0" encoding="UTF-8"?>');
    expect(xmlString).toInclude('<rsm:CrossIndustryInvoice');
    
    // Check core invoice data is included
    expect(xmlString).toInclude('<ram:ID>' + testLetterData.content.invoiceData.id + '</ram:ID>');
    
    // Check seller and buyer info
    expect(xmlString).toInclude(testLetterData.content.invoiceData.billedBy.name);
    expect(xmlString).toInclude(testLetterData.content.invoiceData.billedTo.name);
    
    // Check currency
    expect(xmlString).toInclude(testLetterData.content.invoiceData.currency);
  }
});

// Test generating XML with different invoice types
tap.test('Generate XML with different invoice types', async () => {
  // Create a modified letter with credit note type
  const creditNoteLetterData = JSON.parse(JSON.stringify(testLetterData));
  creditNoteLetterData.content.invoiceData.type = 'creditnote';
  
  // Create an encoder instance
  const encoder = new FacturXEncoder();
  
  // Generate XML
  const xmlString = await encoder.createFacturXXml(creditNoteLetterData);
  
  // Check credit note type code (should be 381)
  expect(xmlString).toInclude('<ram:TypeCode>381</ram:TypeCode>');
});

// Start the test suite
tap.start();