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

describe('SMS Delivery Service', () => {
  describe('sendSMS', () => {
    it('should send SMS successfully', async () => {
      const result = await smsService.sendSMS({
        to: '+1234567890',
        message: 'Test SMS message',
      });

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

    it('should send SMS with custom from number', async () => {
      const result = await smsService.sendSMS({
        to: '+1234567890',
        message: 'Test message',
        from: '+0987654321',
      });

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

  describe('sendBulkSMS', () => {
    it('should send bulk SMS successfully', async () => {
      const recipients = ['+1111111111', '+2222222222', '+3333333333'];

      const result = await smsService.sendBulkSMS(recipients, 'Bulk SMS test');

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

    it('should track failed SMS in bulk send', async () => {
      const recipients = ['+1111111111'];

      const result = await smsService.sendBulkSMS(recipients, 'Test');

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

  describe('getSMSStatus', () => {
    it('should return SMS delivery status', async () => {
      const status = await smsService.getSMSStatus('msg-123');

      expect(status).toHaveProperty('status');
      expect(status).toHaveProperty('timestamp');
      expect(['sent', 'delivered', 'failed', 'undelivered']).toContain(status.status);
    });
  });

  describe('getSMSCampaignStats', () => {
    it('should return campaign statistics', async () => {
      const stats = await smsService.getSMSCampaignStats(1);

      expect(stats).toHaveProperty('sent');
      expect(stats).toHaveProperty('delivered');
      expect(stats).toHaveProperty('failed');
      expect(stats).toHaveProperty('undelivered');
      expect(stats).toHaveProperty('bounced');
      expect(stats).toHaveProperty('optedOut');

      expect(stats.sent).toBeGreaterThan(0);
      expect(stats.delivered).toBeLessThanOrEqual(stats.sent);
    });
  });

  describe('optOutPhoneNumber', () => {
    it('should opt-out phone number successfully', async () => {
      const result = await smsService.optOutPhoneNumber('+1234567890');

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

  describe('optInPhoneNumber', () => {
    it('should opt-in phone number successfully', async () => {
      const result = await smsService.optInPhoneNumber('+1234567890');

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

  describe('isPhoneNumberOptedOut', () => {
    it('should check if phone number is opted out', async () => {
      const result = await smsService.isPhoneNumberOptedOut('+1234567890');

      expect(typeof result).toBe('boolean');
    });
  });

  describe('getSMSRateLimitStatus', () => {
    it('should return rate limit status', async () => {
      const status = await smsService.getSMSRateLimitStatus();

      expect(status).toHaveProperty('remaining');
      expect(status).toHaveProperty('limit');
      expect(status).toHaveProperty('resetAt');

      expect(status.remaining).toBeGreaterThanOrEqual(0);
      expect(status.limit).toBeGreaterThan(0);
      expect(status.remaining).toBeLessThanOrEqual(status.limit);
    });
  });
});
