import { tap, expect } from '@git.zone/tstest/tapbundle';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createConcurrentConnections, performSmtpHandshake, closeSmtpConnection } from '../../helpers/utils.js';

let testServer: ITestServer;
const CONCURRENT_COUNT = 10;
const TEST_PORT = 2527;

tap.test('setup - start SMTP server', async () => {
  testServer = await startTestServer({
    port: 2526
  });
  
  await new Promise(resolve => setTimeout(resolve, 1000));
  expect(testServer.port).toEqual(2526);
});

tap.test('CM-02: Multiple Simultaneous Connections - server handles concurrent connections', async () => {
  const startTime = Date.now();
  
  try {
    // Create multiple concurrent connections
    console.log(`🔄 Creating ${CONCURRENT_COUNT} concurrent connections...`);
    const sockets = await createConcurrentConnections(
      testServer.hostname,
      testServer.port,
      CONCURRENT_COUNT
    );
    
    expect(sockets).toBeArray();
    expect(sockets.length).toEqual(CONCURRENT_COUNT);
    
    // Verify all connections are active
    let activeCount = 0;
    for (const socket of sockets) {
      if (socket && !socket.destroyed) {
        activeCount++;
      }
    }
    expect(activeCount).toEqual(CONCURRENT_COUNT);
    
    // Perform handshake on all connections
    console.log('🤝 Performing handshake on all connections...');
    const handshakePromises = sockets.map(socket => 
      performSmtpHandshake(socket).catch(err => ({ error: err.message }))
    );
    
    const results = await Promise.all(handshakePromises);
    const successCount = results.filter(r => Array.isArray(r)).length;
    
    expect(successCount).toBeGreaterThan(0);
    console.log(`✅ ${successCount}/${CONCURRENT_COUNT} connections completed handshake`);
    
    // Close all connections
    console.log('🔚 Closing all connections...');
    await Promise.all(
      sockets.map(socket => closeSmtpConnection(socket).catch(() => {}))
    );
    
    const duration = Date.now() - startTime;
    console.log(`✅ Multiple connection test completed in ${duration}ms`);
    
  } catch (error) {
    console.error('❌ Multiple connection test failed:', error);
    throw error;
  }
});

// TODO: Enable this test when connection limits are implemented in the server
// tap.test('CM-02: Connection limit enforcement - verify max connections', async () => {
//   const maxConnections = 5;
//   
//   // Start a new server with lower connection limit
//   const limitedServer = await startTestServer({ port: TEST_PORT });
//   
//   await new Promise(resolve => setTimeout(resolve, 1000));
//   
//   try {
//     // Try to create more connections than allowed
//     const attemptCount = maxConnections + 5;
//     console.log(`🔄 Attempting ${attemptCount} connections (limit: ${maxConnections})...`);
//     
//     const connectionPromises = [];
//     for (let i = 0; i < attemptCount; i++) {
//       connectionPromises.push(
//         createConcurrentConnections(limitedServer.hostname, limitedServer.port, 1)
//           .then(() => ({ success: true, index: i }))
//           .catch(err => ({ success: false, index: i, error: err.message }))
//       );
//     }
//     
//     const results = await Promise.all(connectionPromises);
//     const successfulConnections = results.filter(r => r.success).length;
//     const failedConnections = results.filter(r => !r.success).length;
//     
//     console.log(`✅ Successful connections: ${successfulConnections}`);
//     console.log(`❌ Failed connections: ${failedConnections}`);
//     
//     // Some connections should fail due to limit
//     expect(failedConnections).toBeGreaterThan(0);
//     
//   } finally {
//     await stopTestServer(limitedServer);
//   }
// });

tap.test('cleanup - stop SMTP server', async () => {
  await stopTestServer(testServer);
  console.log('✅ Test server stopped');
});

export default tap.start();