import { tap, expect } from '@git.zone/tstest/tapbundle';
import { smtpClientMod } from '../ts/mail/delivery/index.js';
import type { ISmtpClientOptions, SmtpClient } from '../ts/mail/delivery/smtpclient/index.js';
import { Email } from '../ts/mail/core/classes.email.js';

/**
 * Compatibility tests for the legacy SMTP client facade
 */

tap.test('verify backward compatibility - client creation', async () => {
  // Create test configuration
  const options: ISmtpClientOptions = {
    host: 'smtp.example.com',
    port: 587,
    secure: false,
    connectionTimeout: 10000,
    domain: 'test.example.com'
  };
  
  // Create SMTP client instance using legacy constructor
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // Verify instance was created correctly
  expect(smtpClient).toBeTruthy();
  expect(smtpClient.isConnected()).toBeFalsy(); // Should start disconnected
});

tap.test('verify backward compatibility - methods exist', async () => {
  const options: ISmtpClientOptions = {
    host: 'smtp.example.com',
    port: 587,
    secure: false
  };
  
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // Verify all expected methods exist
  expect(typeof smtpClient.sendMail === 'function').toBeTruthy();
  expect(typeof smtpClient.verify === 'function').toBeTruthy();
  expect(typeof smtpClient.isConnected === 'function').toBeTruthy();
  expect(typeof smtpClient.getPoolStatus === 'function').toBeTruthy();
  expect(typeof smtpClient.updateOptions === 'function').toBeTruthy();
  expect(typeof smtpClient.close === 'function').toBeTruthy();
  expect(typeof smtpClient.on === 'function').toBeTruthy();
  expect(typeof smtpClient.off === 'function').toBeTruthy();
  expect(typeof smtpClient.emit === 'function').toBeTruthy();
});

tap.test('verify backward compatibility - options update', async () => {
  const options: ISmtpClientOptions = {
    host: 'smtp.example.com',
    port: 587,
    secure: false
  };
  
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // Test option updates don't throw
  expect(() => smtpClient.updateOptions({
    host: 'new-smtp.example.com',
    port: 465,
    secure: true
  })).not.toThrow();
  
  expect(() => smtpClient.updateOptions({
    debug: true,
    connectionTimeout: 5000
  })).not.toThrow();
});

tap.test('verify backward compatibility - connection failure handling', async () => {
  const options: ISmtpClientOptions = {
    host: 'nonexistent.invalid.domain',
    port: 587,
    secure: false,
    connectionTimeout: 1000 // Short timeout for faster test
  };
  
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // verify() should return false for invalid hosts
  const isValid = await smtpClient.verify();
  expect(isValid).toBeFalsy();
  
  // sendMail should fail gracefully for invalid hosts
  const email = new Email({
    from: 'test@example.com',
    to: 'recipient@example.com',
    subject: 'Test Email',
    text: 'This is a test email'
  });
  
  try {
    const result = await smtpClient.sendMail(email);
    expect(result.success).toBeFalsy();
    expect(result.error).toBeTruthy();
  } catch (error) {
    // Connection errors are expected for invalid domains
    expect(error).toBeTruthy();
  }
});

tap.test('verify backward compatibility - pool status', async () => {
  const options: ISmtpClientOptions = {
    host: 'smtp.example.com',
    port: 587,
    secure: false,
    pool: true,
    maxConnections: 5
  };
  
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // Get pool status
  const status = smtpClient.getPoolStatus();
  expect(status).toBeTruthy();
  expect(typeof status.total === 'number').toBeTruthy();
  expect(typeof status.active === 'number').toBeTruthy();
  expect(typeof status.idle === 'number').toBeTruthy();
  expect(typeof status.pending === 'number').toBeTruthy();
  
  // Initially should have no connections
  expect(status.total).toEqual(0);
  expect(status.active).toEqual(0);
  expect(status.idle).toEqual(0);
  expect(status.pending).toEqual(0);
});

tap.test('verify backward compatibility - event handling', async () => {
  const options: ISmtpClientOptions = {
    host: 'smtp.example.com',
    port: 587,
    secure: false
  };
  
  const smtpClient = smtpClientMod.createSmtpClient(options);
  
  // Test event listener methods don't throw
  const testListener = () => {};
  
  expect(() => smtpClient.on('test', testListener)).not.toThrow();
  expect(() => smtpClient.off('test', testListener)).not.toThrow();
  expect(() => smtpClient.emit('test')).not.toThrow();
});

tap.test('clean up after compatibility tests', async () => {
  // No-op - just to make sure everything is cleaned up properly
});

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

export default tap.start();