import { expect, tap } from '@git.zone/tstest/tapbundle';
import { createTestServer } from '../../helpers/server.loader.js';
import { createTestSmtpClient } from '../../helpers/smtp.client.js';
import { Email } from '../../../ts/mail/core/classes.email.js';

tap.test('CRFC-02: Basic ESMTP Compliance', async () => {
  console.log('\n📧 Testing SMTP Client ESMTP Compliance');
  console.log('=' .repeat(60));

  const testServer = await createTestServer({});

  try {
    const smtpClient = createTestSmtpClient({
      host: testServer.hostname,
      port: testServer.port
    });

    console.log('\nTest 1: Basic EHLO negotiation');
    const email1 = new Email({
      from: 'sender@example.com',
      to: ['recipient@example.com'],
      subject: 'ESMTP test',
      text: 'Testing ESMTP'
    });

    const result1 = await smtpClient.sendMail(email1);
    console.log('  ✓ EHLO negotiation successful');
    expect(result1).toBeDefined();

    console.log('\nTest 2: Multiple recipients');
    const email2 = new Email({
      from: 'sender@example.com',
      to: ['recipient1@example.com', 'recipient2@example.com'],
      cc: ['cc@example.com'],
      bcc: ['bcc@example.com'],
      subject: 'Multiple recipients',
      text: 'Testing multiple recipients'
    });

    const result2 = await smtpClient.sendMail(email2);
    console.log('  ✓ Multiple recipients handled');
    expect(result2).toBeDefined();

    console.log('\nTest 3: UTF-8 content');
    const email3 = new Email({
      from: 'sender@example.com',
      to: ['recipient@example.com'],
      subject: 'UTF-8: café ☕ 测试',
      text: 'International text: émojis 🎉, 日本語',
      html: '<p>HTML: <strong>Zürich</strong></p>'
    });

    const result3 = await smtpClient.sendMail(email3);
    console.log('  ✓ UTF-8 content accepted');
    expect(result3).toBeDefined();

    console.log('\nTest 4: Long headers');
    const longSubject = 'This is a very long subject line that exceeds 78 characters and should be properly folded according to RFC 2822';
    const email4 = new Email({
      from: 'sender@example.com',
      to: ['recipient@example.com'],
      subject: longSubject,
      text: 'Testing header folding'
    });

    const result4 = await smtpClient.sendMail(email4);
    console.log('  ✓ Long headers handled');
    expect(result4).toBeDefined();

    console.log('\n✅ CRFC-02: ESMTP compliance tests completed');

  } finally {
    testServer.server.close();
  }
});

tap.start();