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 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');
      
      for (const line of lines) {
        if (expectedCode) {
          if (line.startsWith(expectedCode + ' ')) {
            clearTimeout(timer);
            socket.removeListener('data', handler);
            resolve(buffer);
            return;
          }
        } else {
          // Look for any complete response
          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('SPF Checking - Authorized IP from local domain', async (tools) => {
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  try {
    // Wait for greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO
    socket.write('EHLO localhost\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Send MAIL FROM with example.com domain
    socket.write('MAIL FROM:<test@example.com>\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    if (mailResponse.includes('250')) {
      console.log('Local domain sender accepted (SPF pass or neutral)');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      const rcptResponse = await waitForResponse(socket);
      console.log('Server response:', rcptResponse);
      
      if (rcptResponse.includes('250')) {
        console.log('Email accepted - SPF likely passed or neutral');
        expect(true).toEqual(true);
      }
    } else if (mailResponse.includes('550') || mailResponse.includes('553')) {
      console.log('Local domain sender rejected (SPF fail)');
      expect(true).toEqual(true); // Either result shows SPF processing
    }
    
    // Send QUIT
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
  } finally {
    socket.destroy();
  }
});

tap.test('SPF Checking - External domain sender', async (tools) => {
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  try {
    // Wait for 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 with well-known external domain
    socket.write('MAIL FROM:<test@google.com>\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    if (mailResponse.includes('250')) {
      console.log('External domain sender accepted');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      const rcptResponse = await waitForResponse(socket);
      console.log('Server response:', rcptResponse);
      
      const accepted = rcptResponse.includes('250');
      const rejected = rcptResponse.includes('550') || rcptResponse.includes('553');
      
      console.log(`External domain: accepted=${accepted}, rejected=${rejected}`);
      expect(accepted || rejected).toEqual(true);
    } else if (mailResponse.includes('550') || mailResponse.includes('553')) {
      console.log('External domain sender rejected (SPF fail)');
      expect(true).toEqual(true); // Shows SPF is working
    }
    
    // Send QUIT
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
  } finally {
    socket.destroy();
  }
});

tap.test('SPF Checking - Known SPF fail domain', async (tools) => {
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  try {
    // Wait for 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 with domain that should fail SPF
    socket.write('MAIL FROM:<test@spf-fail-test.example>\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    if (mailResponse.includes('250')) {
      console.log('SPF fail domain accepted (server may not enforce SPF)');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      const rcptResponse = await waitForResponse(socket);
      console.log('Server response:', rcptResponse);
      
      // Either accepted or rejected is valid
      const response = rcptResponse.includes('250') || rcptResponse.includes('550') || rcptResponse.includes('553');
      expect(response).toEqual(true);
    } else if (mailResponse.includes('550') || mailResponse.includes('553')) {
      console.log('SPF fail domain properly rejected');
      expect(true).toEqual(true);
    }
    
    // Send QUIT
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
  } finally {
    socket.destroy();
  }
});

tap.test('SPF Checking - IPv4 literal in HELO', async (tools) => {
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  try {
    // Wait for greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO with IP literal
    socket.write('EHLO [127.0.0.1]\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Send MAIL FROM with IP literal
    socket.write('MAIL FROM:<test@[127.0.0.1]>\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    // Server should handle IP literals appropriately
    const accepted = mailResponse.includes('250');
    const rejected = mailResponse.includes('550') || mailResponse.includes('553');
    
    console.log(`IP literal sender: accepted=${accepted}, rejected=${rejected}`);
    expect(accepted || rejected).toEqual(true);
    
    // Send QUIT
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
  } finally {
    socket.destroy();
  }
});

tap.test('SPF Checking - Subdomain sender', async (tools) => {
  const socket = net.createConnection({
    host: 'localhost',
    port: TEST_PORT,
    timeout: 30000
  });
  
  try {
    // Wait for greeting
    const greeting = await waitForResponse(socket, '220');
    console.log('Server response:', greeting);
    
    // Send EHLO
    socket.write('EHLO subdomain.example.com\r\n');
    const ehloResponse = await waitForResponse(socket, '250');
    console.log('Server response:', ehloResponse);
    
    // Send MAIL FROM with subdomain
    socket.write('MAIL FROM:<test@subdomain.example.com>\r\n');
    const mailResponse = await waitForResponse(socket);
    console.log('Server response:', mailResponse);
    
    if (mailResponse.includes('250')) {
      console.log('Subdomain sender accepted');
      
      // Send RCPT TO
      socket.write('RCPT TO:<recipient@example.com>\r\n');
      const rcptResponse = await waitForResponse(socket);
      console.log('Server response:', rcptResponse);
      
      const accepted = rcptResponse.includes('250');
      console.log(`Subdomain SPF test: ${accepted ? 'passed' : 'failed'}`);
      expect(true).toEqual(true);
    } else if (mailResponse.includes('550') || mailResponse.includes('553')) {
      console.log('Subdomain sender rejected');
      expect(true).toEqual(true);
    }
    
    // Send QUIT
    socket.write('QUIT\r\n');
    await waitForResponse(socket, '221');
  } finally {
    socket.destroy();
  }
});

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

export default tap.start();