import { tap, expect } from '@git.zone/tstest/tapbundle';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createSmtpClient } from '../../../ts/mail/delivery/smtpclient/index.js';
import { Email } from '../../../ts/mail/core/classes.email.js';

let testServer: ITestServer;

tap.test('setup test SMTP server', async () => {
  testServer = await startTestServer({
    port: 2577,
    tlsEnabled: false,
    authRequired: false
  });
  expect(testServer).toBeTruthy();
  expect(testServer.port).toEqual(2577);
});

tap.test('CEP-04: Basic BCC handling', async () => {
  console.log('Testing basic BCC handling');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // Create email with BCC recipients
  const email = new Email({
    from: 'sender@example.com',
    to: ['visible@example.com'],
    bcc: ['hidden1@example.com', 'hidden2@example.com'],
    subject: 'BCC Test Email',
    text: 'This email tests BCC functionality'
  });

  const result = await smtpClient.sendMail(email);
  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();
  
  console.log('Successfully sent email with BCC recipients');

  await smtpClient.close();
});

tap.test('CEP-04: Multiple BCC recipients', async () => {
  console.log('Testing multiple BCC recipients');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // Create email with many BCC recipients
  const bccRecipients = Array.from({ length: 10 }, 
    (_, i) => `bcc${i + 1}@example.com`
  );
  
  const email = new Email({
    from: 'sender@example.com',
    to: ['primary@example.com'],
    bcc: bccRecipients,
    subject: 'Multiple BCC Test',
    text: 'Testing with multiple BCC recipients'
  });

  console.log(`Sending email with ${bccRecipients.length} BCC recipients...`);
  
  const startTime = Date.now();
  const result = await smtpClient.sendMail(email);
  const elapsed = Date.now() - startTime;

  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();
  
  console.log(`Processed ${bccRecipients.length} BCC recipients in ${elapsed}ms`);

  await smtpClient.close();
});

tap.test('CEP-04: BCC-only email', async () => {
  console.log('Testing BCC-only email');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // Create email with only BCC recipients (no TO or CC)
  const email = new Email({
    from: 'sender@example.com',
    bcc: ['hidden1@example.com', 'hidden2@example.com', 'hidden3@example.com'],
    subject: 'BCC-Only Email',
    text: 'This email has only BCC recipients'
  });

  const result = await smtpClient.sendMail(email);
  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();

  console.log('Successfully sent BCC-only email');

  await smtpClient.close();
});

tap.test('CEP-04: Mixed recipient types', async () => {
  console.log('Testing mixed recipient types');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // Create email with all recipient types
  const email = new Email({
    from: 'sender@example.com',
    to: ['to1@example.com', 'to2@example.com'],
    cc: ['cc1@example.com', 'cc2@example.com'],
    bcc: ['bcc1@example.com', 'bcc2@example.com'],
    subject: 'Mixed Recipients Test',
    text: 'Testing all recipient types together'
  });

  const result = await smtpClient.sendMail(email);
  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();
  
  console.log('Recipient breakdown:');
  console.log(`  TO: ${email.to?.length || 0} recipients`);
  console.log(`  CC: ${email.cc?.length || 0} recipients`);
  console.log(`  BCC: ${email.bcc?.length || 0} recipients`);

  await smtpClient.close();
});

tap.test('CEP-04: BCC with special characters in addresses', async () => {
  console.log('Testing BCC with special characters');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // BCC addresses with special characters
  const specialBccAddresses = [
    'user+tag@example.com',
    'first.last@example.com',
    'user_name@example.com'
  ];

  const email = new Email({
    from: 'sender@example.com',
    to: ['visible@example.com'],
    bcc: specialBccAddresses,
    subject: 'BCC Special Characters Test',
    text: 'Testing BCC with special character addresses'
  });

  const result = await smtpClient.sendMail(email);
  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();
  
  console.log('Successfully processed BCC addresses with special characters');

  await smtpClient.close();
});

tap.test('cleanup test SMTP server', async () => {
  if (testServer) {
    await stopTestServer(testServer);
  }
});

export default tap.start();