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 3461 DSN - DSN extension advertised', async (tools) => {
  const done = tools.defer();
  
  try {
    const socket = net.createConnection({
      host: 'localhost',
      port: TEST_PORT,
      timeout: 30000
    });
    
    await new Promise<void>((resolve, reject) => {
      socket.once('connect', resolve);
      socket.once('error', reject);
    });
    
    // Read greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO
    socket.write('EHLO testclient\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Check if DSN extension is advertised
    const advertisesDsn = ehloResponse.toLowerCase().includes('dsn');
    console.log('DSN extension advertised:', advertisesDsn);
    
    // Parse extensions
    const lines = ehloResponse.split('\r\n');
    const extensions = lines
      .filter(line => line.startsWith('250-') || (line.startsWith('250 ') && lines.indexOf(line) > 0))
      .map(line => line.substring(4).split(' ')[0].toUpperCase());
    
    console.log('Server extensions:', extensions);
    
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
    socket.end();
    done.resolve();
  } catch (error) {
    console.error('Socket error:', error);
    done.reject(error);
  }
});

tap.test('RFC 3461 DSN - MAIL FROM with DSN parameters', async (tools) => {
  const done = tools.defer();
  
  try {
    const socket = net.createConnection({
      host: 'localhost',
      port: TEST_PORT,
      timeout: 30000
    });
    
    await new Promise<void>((resolve, reject) => {
      socket.once('connect', resolve);
      socket.once('error', reject);
    });
    
    // Read greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO
    socket.write('EHLO testclient\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Test MAIL FROM with DSN parameters (RFC 3461)
    socket.write('MAIL FROM:<sender@example.com> RET=FULL ENVID=test-envelope-123\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    // Server should either accept (250) or reject with proper error
    const accepted = mailResponse.includes('250');
    const properlyRejected = mailResponse.includes('501') || mailResponse.includes('555');
    
    expect(accepted || properlyRejected).toEqual(true);
    console.log(`DSN parameters in MAIL FROM ${accepted ? 'accepted' : 'rejected'}`);
    
    if (accepted) {
      // Reset to test other parameters
      socket.write('RSET\r\n');
      const resetResponse = await waitForResponse(socket, '250');
      console.log('Server response:', resetResponse);
      
      // Test with RET=HDRS
      socket.write('MAIL FROM:<sender@example.com> RET=HDRS\r\n');
      const mailHdrsResponse = await waitForResponse(socket);
      console.log('Server response:', mailHdrsResponse);
      
      const hdrsAccepted = mailHdrsResponse.includes('250');
      console.log(`RET=HDRS parameter ${hdrsAccepted ? 'accepted' : 'rejected'}`);
    }
    
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
    socket.end();
    done.resolve();
  } catch (error) {
    console.error('Socket error:', error);
    done.reject(error);
  }
});

tap.test('RFC 3461 DSN - RCPT TO with DSN parameters', async (tools) => {
  const done = tools.defer();
  
  try {
    const socket = net.createConnection({
      host: 'localhost',
      port: TEST_PORT,
      timeout: 30000
    });
    
    await new Promise<void>((resolve, reject) => {
      socket.once('connect', resolve);
      socket.once('error', reject);
    });
    
    // Read greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO
    socket.write('EHLO testclient\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Send MAIL FROM
    socket.write('MAIL FROM:<sender@example.com>\r\n');
    const mailResponse = await waitForResponse(socket, '250');
    console.log('Server response:', mailResponse);
    
    // Test RCPT TO with DSN parameters
    socket.write('RCPT TO:<recipient@example.com> NOTIFY=SUCCESS,FAILURE ORCPT=rfc822;recipient@example.com\r\n');
    const rcptResponse = await waitForResponse(socket);
    console.log('Server response:', rcptResponse);
    
    // Server should either accept (250) or reject with proper error
    const accepted = rcptResponse.includes('250');
    const properlyRejected = rcptResponse.includes('501') || rcptResponse.includes('555');
    
    expect(accepted || properlyRejected).toEqual(true);
    console.log(`DSN parameters in RCPT TO ${accepted ? 'accepted' : 'rejected'}`);
    
    if (accepted) {
      // Reset to test other notify values
      socket.write('RSET\r\n');
      const resetResponse = await waitForResponse(socket, '250');
      console.log('Server response:', resetResponse);
      
      // Send MAIL FROM again
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      const mail2Response = await waitForResponse(socket, '250');
      console.log('Server response:', mail2Response);
      
      // Test NOTIFY=NEVER
      socket.write('RCPT TO:<recipient@example.com> NOTIFY=NEVER\r\n');
      const rcptNeverResponse = await waitForResponse(socket);
      console.log('Server response:', rcptNeverResponse);
      
      const neverAccepted = rcptNeverResponse.includes('250');
      console.log(`NOTIFY=NEVER parameter ${neverAccepted ? 'accepted' : 'rejected'}`);
    }
    
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
    socket.end();
    done.resolve();
  } catch (error) {
    console.error('Socket error:', error);
    done.reject(error);
  }
});

tap.test('RFC 3461 DSN - Complete DSN-enabled email', 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');
      
      // Try with DSN parameters
      socket.write('MAIL FROM:<sender@example.com> RET=FULL ENVID=test123\r\n');
      const mailResponse = await waitForResponse(socket);
      
      if (mailResponse.includes('250')) {
        // DSN parameters accepted, continue with DSN RCPT
        socket.write('RCPT TO:<recipient@example.com> NOTIFY=SUCCESS,FAILURE,DELAY\r\n');
        const rcptResponse = await waitForResponse(socket);
        
        if (!rcptResponse.includes('250')) {
          // Fallback to plain RCPT if DSN parameters not supported
          console.log('DSN RCPT parameters not supported, using plain RCPT TO');
          socket.write('RCPT TO:<recipient@example.com>\r\n');
          await waitForResponse(socket, '250');
        }
      } else if (mailResponse.includes('501') || mailResponse.includes('555')) {
        // DSN not supported, use plain MAIL FROM
        console.log('DSN parameters not supported, using plain MAIL FROM');
        socket.write('MAIL FROM:<sender@example.com>\r\n');
        await waitForResponse(socket, '250');
        
        socket.write('RCPT TO:<recipient@example.com>\r\n');
        await waitForResponse(socket, '250');
      }
      
      // Send DATA
      socket.write('DATA\r\n');
      await waitForResponse(socket, '354');
      
      // Send email content
      const email = [
        `From: sender@example.com`,
        `To: recipient@example.com`,
        `Subject: RFC 3461 DSN Compliance Test`,
        `Date: ${new Date().toUTCString()}`,
        `Message-ID: <dsn-test-${Date.now()}@example.com>`,
        '',
        'This email tests RFC 3461 DSN (Delivery Status Notification) compliance.',
        'The server should handle DSN parameters according to RFC 3461.',
        '.',
        ''
      ].join('\r\n');
      
      socket.write(email);
      await waitForResponse(socket, '250');
      
      console.log('DSN-enabled email accepted');
      
      // 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 3461 DSN - Invalid DSN parameter handling', 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');
      
      // Test with invalid RET value
      socket.write('MAIL FROM:<sender@example.com> RET=INVALID\r\n');
      const mailResponse = await waitForResponse(socket);
      
      // Should reject with 501 or similar
      const properlyRejected = mailResponse.includes('501') || 
                              mailResponse.includes('555') ||
                              mailResponse.includes('500');
      
      if (properlyRejected) {
        console.log('Invalid RET parameter properly rejected');
        expect(true).toEqual(true);
      } else if (mailResponse.includes('250')) {
        // Server ignores unknown parameters (also acceptable)
        console.log('Server ignores invalid DSN parameters');
      }
      
      // Reset and test invalid NOTIFY
      socket.write('RSET\r\n');
      await waitForResponse(socket, '250');
      
      socket.write('MAIL FROM:<sender@example.com>\r\n');
      await waitForResponse(socket, '250');
      
      // Test with invalid NOTIFY value
      socket.write('RCPT TO:<recipient@example.com> NOTIFY=INVALID\r\n');
      const rcptResponse = await waitForResponse(socket);
      
      const rcptRejected = rcptResponse.includes('501') || 
                          rcptResponse.includes('555') ||
                          rcptResponse.includes('500');
      
      if (rcptRejected) {
        console.log('Invalid NOTIFY parameter properly rejected');
      } else if (rcptResponse.includes('250')) {
        console.log('Server ignores invalid NOTIFY parameter');
      }
      
      // 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();