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

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

// Test encoder/decoder at a basic level
tap.test('Basic encoder/decoder test', async () => {
  // Create a simple encoder
  const encoder = new FacturXEncoder();
  
  // Verify it has the correct methods
  expect(encoder).toBeTypeOf('object');
  expect(encoder.createFacturXXml).toBeTypeOf('function');
  expect(encoder.createZugferdXml).toBeTypeOf('function'); // For backward compatibility
  
  // Create a simple decoder
  const decoder = new FacturXDecoder('<?xml version="1.0" encoding="UTF-8"?><test><name>Test</name></test>');
  
  // Verify it has the correct method
  expect(decoder).toBeTypeOf('object');
  expect(decoder.getLetterData).toBeTypeOf('function');
  
  // Create a simple XInvoice instance
  const xInvoice = new XInvoice();
  
  // Verify it has the correct methods
  expect(xInvoice).toBeTypeOf('object');
  expect(xInvoice.addXmlString).toBeTypeOf('function');
  expect(xInvoice.getParsedXmlData).toBeTypeOf('function');
});

// Test ZUGFeRD XML format validation
tap.test('ZUGFeRD XML format validation', async () => {
  // Create a sample XML string directly
  const sampleXml = `<?xml version="1.0" encoding="UTF-8"?>
  <rsm:CrossIndustryInvoice 
    xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
    xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100">
    <rsm:ExchangedDocument>
      <ram:ID>LL-INV-48765</ram:ID>
    </rsm:ExchangedDocument>
  </rsm:CrossIndustryInvoice>`;
  
  // Create an XInvoice instance
  const xInvoice = new XInvoice();
  
  // Detect the format
  const format = xInvoice['identifyXmlFormat'](sampleXml);
  
  // Check that the format is correctly identified as ZUGFeRD/CII
  expect(format).toEqual('ZUGFeRD/CII');
});

// Test invoice data extraction
tap.test('Invoice data extraction from ZUGFeRD XML', async () => {
  // Create a sample XML string directly
  const sampleXml = `<?xml version="1.0" encoding="UTF-8"?>
  <rsm:CrossIndustryInvoice 
    xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
    xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100">
    <rsm:ExchangedDocument>
      <ram:ID>${testLetterData.content.invoiceData.id}</ram:ID>
    </rsm:ExchangedDocument>
    <rsm:SupplyChainTradeTransaction>
      <ram:ApplicableHeaderTradeAgreement>
        <ram:SellerTradeParty>
          <ram:Name>${testLetterData.content.invoiceData.billedBy.name}</ram:Name>
        </ram:SellerTradeParty>
        <ram:BuyerTradeParty>
          <ram:Name>${testLetterData.content.invoiceData.billedTo.name}</ram:Name>
        </ram:BuyerTradeParty>
      </ram:ApplicableHeaderTradeAgreement>
    </rsm:SupplyChainTradeTransaction>
  </rsm:CrossIndustryInvoice>`;
  
  // Create an XInvoice instance and parse the XML
  const xInvoice = new XInvoice();
  await xInvoice.addXmlString(sampleXml);
  
  // Parse the XML to an invoice object
  const parsedInvoice = await xInvoice.getParsedXmlData();
  
  // Check that core information was extracted correctly
  expect(parsedInvoice.InvoiceNumber).not.toEqual('');
  expect(parsedInvoice.Seller.Name).not.toEqual('');
});

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