import * as plugins from '../plugins.js';

/**
 * Base decoder class for all invoice XML formats.
 * Provides common functionality and interfaces for different format decoders.
 */
export abstract class BaseDecoder {
  protected xmlString: string;
  
  constructor(xmlString: string) {
    if (!xmlString) {
      throw new Error('No XML string provided to decoder');
    }
    
    this.xmlString = xmlString;
  }
  
  /**
   * Abstract method that each format-specific decoder must implement.
   * Converts XML into a structured letter object based on the XML format.
   */
  public abstract getLetterData(): Promise<plugins.tsclass.business.ILetter>;
  
  /**
   * Creates a default letter object with minimal data.
   * Used as a fallback when parsing fails.
   */
  protected createDefaultLetter(): plugins.tsclass.business.ILetter {
    // Create a default seller
    const seller: plugins.tsclass.business.IContact = {
      name: 'Unknown Seller',
      type: 'company',
      description: 'Unknown Seller',  // Required by IContact interface
      address: {
        streetName: 'Unknown',
        houseNumber: '0',  // Required by IAddress interface
        city: 'Unknown',
        country: 'Unknown',
        postalCode: 'Unknown',
      },
    };
    
    // Create a default buyer
    const buyer: plugins.tsclass.business.IContact = {
      name: 'Unknown Buyer',
      type: 'company',
      description: 'Unknown Buyer',  // Required by IContact interface
      address: {
        streetName: 'Unknown',
        houseNumber: '0',  // Required by IAddress interface
        city: 'Unknown',
        country: 'Unknown',
        postalCode: 'Unknown',
      },
    };
    
    // Create default invoice data
    const invoiceData: plugins.tsclass.finance.IInvoice = {
      id: 'Unknown',
      status: null,
      type: 'debitnote',
      billedBy: seller,
      billedTo: buyer,
      deliveryDate: Date.now(),
      dueInDays: 30,
      periodOfPerformance: null,
      printResult: null,
      currency: 'EUR' as plugins.tsclass.finance.TCurrency,
      notes: [],
      items: [
        {
          name: 'Unknown Item',
          unitQuantity: 1,
          unitNetPrice: 0,
          vatPercentage: 0,
          position: 0,
          unitType: 'units',
        }
      ],
      reverseCharge: false,
    };
    
    // Return a default letter
    return {
      versionInfo: {
        type: 'draft',
        version: '1.0.0',
      },
      type: 'invoice',
      date: Date.now(),
      subject: 'Unknown Invoice',
      from: seller,
      to: buyer,
      content: {
        invoiceData: invoiceData,
        textData: null,
        timesheetData: null,
        contractData: null,
      },
      needsCoverSheet: false,
      objectActions: [],
      pdf: null,
      incidenceId: null,
      language: null,
      legalContact: null,
      logoUrl: null,
      pdfAttachments: null,
      accentColor: null,
    };
  }
}