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: 2579,
    tlsEnabled: false,
    authRequired: false
  });
  expect(testServer).toBeTruthy();
  expect(testServer.port).toEqual(2579);
});

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

  // Email with basic UTF-8 characters
  const email = new Email({
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'UTF-8 Test: café, naïve, résumé',
    text: 'This email contains UTF-8 characters: café, naïve, résumé, piñata',
    html: '<p>HTML with UTF-8: <strong>café</strong>, <em>naïve</em>, résumé, piñata</p>'
  });

  const result = await smtpClient.sendMail(email);
  expect(result).toBeDefined();
  expect(result.messageId).toBeDefined();
  
  console.log('Successfully sent email with basic UTF-8 characters');

  await smtpClient.close();
});

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

  // Email with European characters
  const email = new Email({
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'European: ñ, ü, ø, å, ß, æ',
    text: [
      'German: Müller, Größe, Weiß',
      'Spanish: niño, señor, España',
      'French: français, crème, être',
      'Nordic: København, Göteborg, Ålesund',
      'Polish: Kraków, Gdańsk, Wrocław'
    ].join('\n'),
    html: `
      <h1>European Characters Test</h1>
      <ul>
        <li>German: Müller, Größe, Weiß</li>
        <li>Spanish: niño, señor, España</li>
        <li>French: français, crème, être</li>
        <li>Nordic: København, Göteborg, Ålesund</li>
        <li>Polish: Kraków, Gdańsk, Wrocław</li>
      </ul>
    `
  });

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

  await smtpClient.close();
});

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

  // Email with Asian characters
  const email = new Email({
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'Asian: 你好, こんにちは, 안녕하세요',
    text: [
      'Chinese (Simplified): 你好世界',
      'Chinese (Traditional): 你好世界',
      'Japanese: こんにちは世界',
      'Korean: 안녕하세요 세계',
      'Thai: สวัสดีโลก',
      'Hindi: नमस्ते संसार'
    ].join('\n'),
    html: `
      <h1>Asian Characters Test</h1>
      <table>
        <tr><td>Chinese (Simplified):</td><td>你好世界</td></tr>
        <tr><td>Chinese (Traditional):</td><td>你好世界</td></tr>
        <tr><td>Japanese:</td><td>こんにちは世界</td></tr>
        <tr><td>Korean:</td><td>안녕하세요 세계</td></tr>
        <tr><td>Thai:</td><td>สวัสดีโลก</td></tr>
        <tr><td>Hindi:</td><td>नमस्ते संसार</td></tr>
      </table>
    `
  });

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

  await smtpClient.close();
});

tap.test('CEP-06: Emojis and symbols', async () => {
  console.log('Testing emojis and symbols');
  
  const smtpClient = createSmtpClient({
    host: testServer.hostname,
    port: testServer.port,
    secure: false,
    connectionTimeout: 5000,
    debug: true
  });

  // Email with emojis and symbols
  const email = new Email({
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'Emojis: 🎉 🚀 ✨ 🌈',
    text: [
      'Faces: 😀 😃 😄 😁 😆 😅 😂',
      'Objects: 🎉 🚀 ✨ 🌈 ⭐ 🔥 💎',
      'Animals: 🐶 🐱 🐭 🐹 🐰 🦊 🐻',
      'Food: 🍎 🍌 🍇 🍓 🥝 🍅 🥑',
      'Symbols: ✓ ✗ ⚠ ♠ ♣ ♥ ♦',
      'Math: ∑ ∏ ∫ ∞ ± × ÷ ≠ ≤ ≥'
    ].join('\n'),
    html: `
      <h1>Emojis and Symbols Test 🎉</h1>
      <p>Faces: 😀 😃 😄 😁 😆 😅 😂</p>
      <p>Objects: 🎉 🚀 ✨ 🌈 ⭐ 🔥 💎</p>
      <p>Animals: 🐶 🐱 🐭 🐹 🐰 🦊 🐻</p>
      <p>Food: 🍎 🍌 🍇 🍓 🥝 🍅 🥑</p>
      <p>Symbols: ✓ ✗ ⚠ ♠ ♣ ♥ ♦</p>
      <p>Math: ∑ ∏ ∫ ∞ ± × ÷ ≠ ≤ ≥</p>
    `
  });

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

  await smtpClient.close();
});

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

  // Email with mixed international content
  const email = new Email({
    from: 'sender@example.com',
    to: ['recipient@example.com'],
    subject: 'Mixed: Hello 你好 مرحبا こんにちは 🌍',
    text: [
      'English: Hello World!',
      'Chinese: 你好世界！',
      'Arabic: مرحبا بالعالم!',
      'Japanese: こんにちは世界！',
      'Russian: Привет мир!',
      'Greek: Γεια σας κόσμε!',
      'Mixed: Hello 世界 🌍 مرحبا こんにちは!'
    ].join('\n'),
    html: `
      <h1>International Mix 🌍</h1>
      <div style="font-family: Arial, sans-serif;">
        <p><strong>English:</strong> Hello World!</p>
        <p><strong>Chinese:</strong> 你好世界！</p>
        <p><strong>Arabic:</strong> مرحبا بالعالم!</p>
        <p><strong>Japanese:</strong> こんにちは世界！</p>
        <p><strong>Russian:</strong> Привет мир!</p>
        <p><strong>Greek:</strong> Γεια σας κόσμε!</p>
        <p><strong>Mixed:</strong> Hello 世界 🌍 مرحبا こんにちは!</p>
      </div>
    `
  });

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

  await smtpClient.close();
});

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

export default tap.start();