import { tap, expect } from '@push.rocks/tapbundle';
import * as plugins from '../ts/plugins.js';
import { SzPlatformService } from '../ts/platformservice.js';
import { MtaService } from '../ts/mta/classes.mta.js';
import { EmailService } from '../ts/email/classes.emailservice.js';
import { BounceManager } from '../ts/email/classes.bouncemanager.js';
import DcRouter from '../ts/dcrouter/classes.dcrouter.js';

// Test the new integration architecture
tap.test('should be able to create an independent MTA service', async (tools) => {
  // Create an independent MTA service
  const mta = new MtaService(undefined, {
    smtp: {
      port: 10025, // Use a different port for testing
      hostname: 'test.example.com'
    }
  });
  
  // Verify it was created properly without a platform service reference
  expect(mta).toBeTruthy();
  expect(mta.platformServiceRef).toBeUndefined();
  
  // Even without a platform service, it should have its own SMTP rule engine
  expect(mta.smtpRuleEngine).toBeTruthy();
});

tap.test('should be able to create an EmailService with an existing MTA', async (tools) => {
  // Create a platform service first
  const platformService = new SzPlatformService();
  
  // Create a shared bounce manager
  const bounceManager = new BounceManager();
  
  // Create an independent MTA service
  const mta = new MtaService(undefined, {
    smtp: {
      port: 10025, // Use a different port for testing
    }
  });
  
  // Manually set the bounce manager for testing
  // @ts-ignore - adding property for testing
  mta.bounceManager = bounceManager;
  
  // Create an email service that uses the independent MTA
  // @ts-ignore - passing a third argument to the constructor
  const emailService = new EmailService(platformService, {}, mta);
  
  // Manually set the mtaService property
  emailService.mtaService = mta;
  
  // Verify relationships
  expect(emailService.mtaService === mta).toBeTrue();
  expect(emailService.bounceManager).toBeTruthy();
  
  // MTA should not have a direct platform service reference
  expect(mta.platformServiceRef).toBeUndefined();
  
  // But it should have access to bounce manager
  // @ts-ignore - accessing property for testing
  expect(mta.bounceManager === bounceManager).toBeTrue();
});

tap.test('MTA service should have SMTP rule engine', async (tools) => {
  // Create an independent MTA service
  const mta = new MtaService(undefined, {
    smtp: {
      port: 10025, // Use a different port for testing
    }
  });
  
  // Verify the MTA has an SMTP rule engine
  expect(mta.smtpRuleEngine).toBeTruthy();
});

tap.test('platform service should support having an MTA service', async (tools) => {
  // Create a platform service with default config
  const platformService = new SzPlatformService();
  
  // Create MTA - don't await start() to avoid binding to ports
  platformService.mtaService = new MtaService(platformService, {
    smtp: {
      port: 10025, // Use a different port for testing
    }
  });
  
  // Create email service using the platform
  platformService.emailService = new EmailService(platformService);
  
  // Verify the MTA has a reference to the platform service
  expect(platformService.mtaService).toBeTruthy();
  expect(platformService.mtaService.platformServiceRef).toBeTruthy();
});

tap.test('stop', async () => {
  await tap.stopForcefully();
});

// Export for tapbundle execution
export default tap.start();