import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as plugins from '../../../ts/plugins.js';
import * as net from 'net';
import { startTestServer, stopTestServer } from '../../helpers/server.loader.ts'
import type { ITestServer } from '../../helpers/server.loader.js';

const TEST_PORT = 2525;
let testServer: ITestServer;

// Helper function to wait for SMTP response
const waitForResponse = (socket: net.Socket, expectedCode?: string, timeout = 5000): Promise<string> => {
  return new Promise((resolve, reject) => {
    let buffer = '';
    const timer = setTimeout(() => {
      socket.removeListener('data', handler);
      reject(new Error(`Timeout waiting for ${expectedCode || 'any'} response`));
    }, timeout);
    
    const handler = (data: Buffer) => {
      buffer += data.toString();
      const lines = buffer.split('\r\n');
      
      // Check if we have a complete response
      for (const line of lines) {
        if (expectedCode) {
          if (line.startsWith(expectedCode + ' ')) {
            clearTimeout(timer);
            socket.removeListener('data', handler);
            resolve(buffer);
            return;
          }
        } else {
          // Any complete response line
          if (line.match(/^\d{3} /)) {
            clearTimeout(timer);
            socket.removeListener('data', handler);
            resolve(buffer);
            return;
          }
        }
      }
    };
    
    socket.on('data', handler);
  });
};

tap.test('setup - start test server', async (toolsArg) => {
  testServer = await startTestServer({ port: TEST_PORT });
  await toolsArg.delayFor(1000);
});

tap.test('RFC 5322 - Message format with required headers', async (tools) => {
  const done = tools.defer();
  
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  socket.on('error', (err) => {
    console.error('Socket error:', err);
    done.reject(err);
  });
  
  socket.on('connect', async () => {
    try {
      // Wait for greeting
      await waitForResponse(socket, '220');
      
      // Send EHLO
      socket.write('EHLO testclient\r\n');
      await waitForResponse(socket, '250');
      
      // Send MAIL FROM
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // RFC 5322 compliant email with all required headers
      const messageId = `<test.${Date.now()}@example.com>`;
      const date = new Date().toUTCString();
      
      const rfc5322Email = [
        `Date: ${date}`,
        `From: "Test Sender" <sender@example.com>`,
        `To: "Test Recipient" <recipient@example.com>`,
        `Subject: RFC 5322 Compliance Test`,
        `Message-ID: ${messageId}`,
        `MIME-Version: 1.0`,
        `Content-Type: text/plain; charset=UTF-8`,
        `Content-Transfer-Encoding: 7bit`,
        '',
        'This is a test message for RFC 5322 compliance verification.',
        'It includes proper headers according to RFC 5322 specifications.',
        '',
        'Best regards,',
        'Test System',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(rfc5322Email);
      const response = await waitForResponse(socket, '250');
      
      console.log('RFC 5322 compliant message accepted');
      
      // Send QUIT
      socket.write('QUIT\r\n');
      await waitForResponse(socket, '221');
      
      socket.end();
      done.resolve();
    } catch (err) {
      console.error('Test error:', err);
      socket.end();
      done.reject(err);
    }
  });
  
  await done.promise;
});

tap.test('RFC 5322 - Folded header lines', async (tools) => {
  const done = tools.defer();
  
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  socket.on('error', (err) => {
    console.error('Socket error:', err);
    done.reject(err);
  });
  
  socket.on('connect', async () => {
    try {
      // Wait for greeting
      await waitForResponse(socket, '220');
      
      // Send EHLO
      socket.write('EHLO testclient\r\n');
      await waitForResponse(socket, '250');
      
      // Send MAIL FROM
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // Test folded header lines (RFC 5322 section 2.2.3)
      const email = [
        `Date: ${new Date().toUTCString()}`,
        `From: sender@example.com`,
        `To: recipient@example.com`,
        `Subject: This is a very long subject line that needs to be`,
        ` folded according to RFC 5322 specifications for proper`,
        ` email header formatting`,
        `Message-ID: <${Date.now()}@example.com>`,
        `References: <ref1@example.com>`,
        ` <ref2@example.com>`,
        ` <ref3@example.com>`,
        '',
        'Email with folded headers.',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(email);
      await waitForResponse(socket, '250');
      
      console.log('Folded headers message accepted');
      
      // Send QUIT
      socket.write('QUIT\r\n');
      await waitForResponse(socket, '221');
      
      socket.end();
      done.resolve();
    } catch (err) {
      console.error('Test error:', err);
      socket.end();
      done.reject(err);
    }
  });
  
  await done.promise;
});

tap.test('RFC 5322 - Multiple recipient formats', async (tools) => {
  const done = tools.defer();
  
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  socket.on('error', (err) => {
    console.error('Socket error:', err);
    done.reject(err);
  });
  
  socket.on('connect', async () => {
    try {
      // Wait for greeting
      await waitForResponse(socket, '220');
      
      // Send EHLO
      socket.write('EHLO testclient\r\n');
      await waitForResponse(socket, '250');
      
      // Send MAIL FROM
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send multiple RCPT TO
      socket.write('RCPT TO:<recipient1@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      socket.write('RCPT TO:<recipient2@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // Test various recipient formats allowed by RFC 5322
      const email = [
        `Date: ${new Date().toUTCString()}`,
        `From: "Sender Name" <sender@example.com>`,
        `To: recipient1@example.com, "Recipient Two" <recipient2@example.com>`,
        `Cc: "Carbon Copy" <cc@example.com>`,
        `Bcc: bcc@example.com`,
        `Reply-To: "Reply Address" <reply@example.com>`,
        `Subject: Multiple recipient formats test`,
        `Message-ID: <${Date.now()}@example.com>`,
        '',
        'Testing various recipient header formats.',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(email);
      await waitForResponse(socket, '250');
      
      console.log('Multiple recipient formats accepted');
      
      // Send QUIT
      socket.write('QUIT\r\n');
      await waitForResponse(socket, '221');
      
      socket.end();
      done.resolve();
    } catch (err) {
      console.error('Test error:', err);
      socket.end();
      done.reject(err);
    }
  });
  
  await done.promise;
});

tap.test('RFC 5322 - Comments in headers', async (tools) => {
  const done = tools.defer();
  
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  socket.on('error', (err) => {
    console.error('Socket error:', err);
    done.reject(err);
  });
  
  socket.on('connect', async () => {
    try {
      // Wait for greeting
      await waitForResponse(socket, '220');
      
      // Send EHLO
      socket.write('EHLO testclient\r\n');
      await waitForResponse(socket, '250');
      
      // Send MAIL FROM
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // RFC 5322 allows comments in headers using parentheses
      const email = [
        `Date: ${new Date().toUTCString()} (generated by test system)`,
        `From: sender@example.com (Test Sender)`,
        `To: recipient@example.com (Primary Recipient)`,
        `Subject: Testing comments (RFC 5322 section 3.2.2)`,
        `Message-ID: <${Date.now()}@example.com>`,
        `X-Custom-Header: value (with comment)`,
        '',
        'Email with comments in headers.',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(email);
      await waitForResponse(socket, '250');
      
      console.log('Headers with comments accepted');
      
      // Send QUIT
      socket.write('QUIT\r\n');
      await waitForResponse(socket, '221');
      
      socket.end();
      done.resolve();
    } catch (err) {
      console.error('Test error:', err);
      socket.end();
      done.reject(err);
    }
  });
  
  await done.promise;
});

tap.test('RFC 5322 - Resent headers', async (tools) => {
  const done = tools.defer();
  
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  socket.on('error', (err) => {
    console.error('Socket error:', err);
    done.reject(err);
  });
  
  socket.on('connect', async () => {
    try {
      // Wait for greeting
      await waitForResponse(socket, '220');
      
      // Send EHLO
      socket.write('EHLO testclient\r\n');
      await waitForResponse(socket, '250');
      
      // Send MAIL FROM
      socket.write('MAIL FROM:<resender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send RCPT TO
      socket.write('RCPT TO:<newrecipient@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // RFC 5322 resent headers for forwarded messages
      const email = [
        `Resent-Date: ${new Date().toUTCString()}`,
        `Resent-From: resender@example.com`,
        `Resent-To: newrecipient@example.com`,
        `Resent-Message-ID: <resent.${Date.now()}@example.com>`,
        `Date: ${new Date(Date.now() - 86400000).toUTCString()}`, // Original date (yesterday)
        `From: original@example.com`,
        `To: oldrecipient@example.com`,
        `Subject: Forwarded: Original Subject`,
        `Message-ID: <original.${Date.now() - 1000}@example.com>`,
        '',
        'This is a forwarded message with resent headers.',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(email);
      await waitForResponse(socket, '250');
      
      console.log('Resent headers message accepted');
      
      // Send QUIT
      socket.write('QUIT\r\n');
      await waitForResponse(socket, '221');
      
      socket.end();
      done.resolve();
    } catch (err) {
      console.error('Test error:', err);
      socket.end();
      done.reject(err);
    }
  });
  
  await done.promise;
});

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

export default tap.start();