/**
 * Sales Module Factory
 * Configures and provides instances of service with default adapters
 */

import { SalesInvoiceApplicationService } from '../application/sales-invoice-application-service';
import { SalesInvoicePostgresAdapter } from './sales-invoice-postgres-adapter';
import { DefaultSalesAccountingPort } from './default-sales-accounting.port';
import { DefaultSalesSideEffectsPort } from './default-sales-side-effects.port';
import { DefaultSalesReferenceDataPort } from './default-sales-reference-data.port';
import { DefaultSalesIdGeneratorPort } from './default-sales-id-generator.port';

let serviceInstance: SalesInvoiceApplicationService | null = null;

/**
 * Create a configured service instance with default adapters
 */
export function createSalesInvoiceService(): SalesInvoiceApplicationService {
  const repository = new SalesInvoicePostgresAdapter();  const accounting = new DefaultSalesAccountingPort();
  const sideEffects = new DefaultSalesSideEffectsPort();
  const referenceData = new DefaultSalesReferenceDataPort();
  const idGenerator = new DefaultSalesIdGeneratorPort();

  return SalesInvoiceApplicationService.createDefault(
    repository,
    accounting,
    sideEffects,
    referenceData,
    idGenerator
  );
}

/**
 * Get or create singleton instance (for action layer use)
 */
export function getSalesInvoiceService(): SalesInvoiceApplicationService {
  if (!serviceInstance) {
    serviceInstance = createSalesInvoiceService();
  }
  return serviceInstance;
}

/**
 * Reset service (useful for testing)
 */
export function resetSalesInvoiceService(): void {
  serviceInstance = null;
}
