import { describe, it, expect } from 'vitest';
import * as brevoService from './brevoEmailService.ts';

describe('Brevo Email Service', () => {
  describe('sendBrevoEmail', () => {
    it('should send email successfully', async () => {
      const result = await brevoService.sendBrevoEmail({
        to: 'test@example.com',
        subject: 'Test Email',
        html: '<p>Test content</p>',
      });

      expect(result.success).toBe(true);
      expect(result.messageId).toBeDefined();
    });

    it('should handle email with template', async () => {
      const result = await brevoService.sendBrevoEmail({
        to: 'test@example.com',
        subject: 'Campaign Email',
        templateName: 'retention_campaign',
        variables: {
          playerName: 'John',
          offers: '20% Bonus',
        },
      });

      expect(result.success).toBe(true);
    });

    it('should handle email with attachments', async () => {
      const result = await brevoService.sendBrevoEmail({
        to: 'test@example.com',
        subject: 'Email with Attachment',
        html: '<p>See attachment</p>',
        attachments: [
          {
            name: 'test.pdf',
            content: Buffer.from('test'),
            contentType: 'application/pdf',
          },
        ],
      });

      expect(result.success).toBe(true);
    });
  });

  describe('sendBrevoBulkEmails', () => {
    it('should send bulk emails successfully', async () => {
      const recipients = [
        { email: 'user1@example.com', name: 'User 1' },
        { email: 'user2@example.com', name: 'User 2' },
      ];

      const result = await brevoService.sendBrevoBulkEmails(
        recipients,
        'Bulk Test',
        '<p>Test</p>'
      );

      expect(result.success).toBe(true);
      expect(result.sent).toBeGreaterThan(0);
    });

    it('should track failed emails in bulk send', async () => {
      const recipients = [{ email: 'valid@example.com' }];

      const result = await brevoService.sendBrevoBulkEmails(
        recipients,
        'Test',
        '<p>Test</p>'
      );

      expect(result.sent + result.failed).toBe(recipients.length);
    });
  });

  describe('sendBrevoTransactionalEmail', () => {
    it('should send transactional email', async () => {
      const result = await brevoService.sendBrevoTransactionalEmail(
        'test@example.com',
        123,
        { key: 'value' }
      );

      expect(result.success).toBe(true);
      expect(result.messageId).toBeDefined();
    });
  });

  describe('getBrevoEmailStats', () => {
    it('should return email statistics', async () => {
      const stats = await brevoService.getBrevoEmailStats(1);

      expect(stats).toHaveProperty('sent');
      expect(stats).toHaveProperty('opened');
      expect(stats).toHaveProperty('clicked');
      expect(stats).toHaveProperty('bounced');
      expect(stats).toHaveProperty('unsubscribed');
      expect(stats).toHaveProperty('complained');

      expect(stats.sent).toBeGreaterThan(0);
      expect(stats.opened).toBeLessThanOrEqual(stats.sent);
      expect(stats.clicked).toBeLessThanOrEqual(stats.opened);
    });
  });

  describe('createBrevoContact', () => {
    it('should create contact successfully', async () => {
      const result = await brevoService.createBrevoContact({
        email: 'newcontact@example.com',
        firstName: 'John',
        lastName: 'Doe',
      });

      expect(result.success).toBe(true);
      expect(result.contactId).toBeDefined();
    });

    it('should create contact with attributes', async () => {
      const result = await brevoService.createBrevoContact({
        email: 'contact@example.com',
        attributes: {
          PLAYER_TIER: 'VIP',
          SIGNUP_DATE: '2026-04-16',
        },
      });

      expect(result.success).toBe(true);
    });
  });

  describe('updateBrevoContact', () => {
    it('should update contact successfully', async () => {
      const result = await brevoService.updateBrevoContact('test@example.com', {
        firstName: 'Jane',
        attributes: { PLAYER_TIER: 'GOLD' },
      });

      expect(result.success).toBe(true);
    });
  });

  describe('addBrevoContactToList', () => {
    it('should add contact to list', async () => {
      const result = await brevoService.addBrevoContactToList('test@example.com', 1);

      expect(result.success).toBe(true);
    });
  });

  describe('removeBrevoContactFromList', () => {
    it('should remove contact from list', async () => {
      const result = await brevoService.removeBrevoContactFromList('test@example.com', 1);

      expect(result.success).toBe(true);
    });
  });
});
