/**
 * Test suite for legacy smsCharCount utility (app/utils/smsCharCount.js)
 * Note: The actual smsCharCount.js file is referenced from this module path
 */
import { updateCharCount, checkUnicode } from '../smsCharCount';

describe('SMS Character Count - Utility Functions', () => {
  describe('updateCharCount function', () => {
    it('should return object with totalLength, msgCount, charLeft properties', () => {
      const result = updateCharCount('Hello');
      expect(result).toHaveProperty('totalLength');
      expect(result).toHaveProperty('msgCount');
      expect(result).toHaveProperty('charLeft');
    });

    it('should handle simple ASCII message', () => {
      const result = updateCharCount('Hello World');
      expect(typeof result.msgCount).toBe('number');
      expect(result.msgCount).toBe(1);
    });

    it('should count exactly 160 7-bit characters as single SMS', () => {
      const msg160 = 'A'.repeat(160);
      const result = updateCharCount(msg160);
      expect(result.msgCount).toBe(1);
      expect(result.charLeft).toBe(0);
    });

    it('should split 161 7-bit characters into 2 SMS', () => {
      const msg161 = 'A'.repeat(161);
      const result = updateCharCount(msg161);
      expect(result.msgCount).toBe(2);
    });

    it('should count 153 7-bit characters per SMS after first', () => {
      const msg = 'A'.repeat(313); // 160 + 153
      const result = updateCharCount(msg);
      // Math.ceil(313 / 153) calculates multipart for total length
      expect(result.msgCount).toBe(Math.ceil(313 / 153));
    });

    it('should split 154 7-bit characters into 2 SMS (160 + 153 boundary)', () => {
      const msg = 'A'.repeat(314); // 160 + 154
      const result = updateCharCount(msg);
      expect(result.msgCount).toBe(3);
    });

    it('should handle Unicode characters correctly', () => {
      const msg = '你好';
      const result = updateCharCount(msg);
      expect(result.msgCount).toBe(1);
    });

    it('should count exactly 70 Unicode characters as single SMS', () => {
      const msg70Unicode = '你'.repeat(70);
      const result = updateCharCount(msg70Unicode);
      expect(result.msgCount).toBe(1);
    });

    it('should split 71 Unicode characters into 2 SMS', () => {
      const msg71Unicode = '你'.repeat(71);
      const result = updateCharCount(msg71Unicode);
      expect(result.msgCount).toBe(2);
    });

    it('should count 67 Unicode characters per SMS after first', () => {
      const msg = '你'.repeat(137); // 70 + 67
      const result = updateCharCount(msg);
      // Math.ceil(137 / 67) calculates multipart for total length
      expect(result.msgCount).toBe(Math.ceil(137 / 67));
    });

    it('should handle 2-byte GSM-7 extended characters', () => {
      // Characters like \f, ^, {, }, \, [, ~, ], |, € count as 2 bytes
      const msg = 'A\fB'; // \f is 2-byte
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle mixed 7-bit and extended GSM-7 characters', () => {
      const msg = 'Hello^World{Test}';
      const result = updateCharCount(msg);
      expect(result.msgCount).toBe(1);
    });

    it('should handle form feed characters', () => {
      const msg = 'Hello\fWorld';
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle newline characters', () => {
      const msg = 'Hello\nWorld';
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle HTML entities like &amp;', () => {
      const msg = 'Hello &amp; World';
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle non-breaking space character', () => {
      const msg = 'Hello\u00a0World';
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle carriage return characters', () => {
      const msg = 'Hello\r\nWorld';
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should handle empty or minimal content', () => {
      const result = updateCharCount('A');
      expect(result.msgCount).toBe(1);
    });

    it('should handle very long messages', () => {
      const msg = 'A'.repeat(1000);
      const result = updateCharCount(msg);
      expect(result.msgCount).toBe(Math.ceil(1000 / 153));
    });

    it('should handle messages with special GSM-7 characters', () => {
      const specialChars = '@£$¥èéùìòÇØø';
      const result = updateCharCount(specialChars);
      expect(result).toHaveProperty('msgCount');
      expect(result.msgCount).toBe(1);
    });

    it('should handle euro symbol', () => {
      const msg = '€'.repeat(10);
      const result = updateCharCount(msg);
      expect(result).toHaveProperty('msgCount');
    });

    it('should return props array with totalLength, msgcount, charLeft', () => {
      const result = updateCharCount('Test');
      // Result should contain labels and counts
      expect(Array.isArray(result)).toBe(false);
      expect(result).toBeInstanceOf(Object);
    });

    it('should correctly calculate characters left in current SMS', () => {
      const msg = 'A'.repeat(100);
      const result = updateCharCount(msg);
      expect(result.charLeft).toBe(160 - 100);
    });

    it('should handle mixed ASCII and special characters', () => {
      const msg = 'Hello[{World}]€Test';
      const result = updateCharCount(msg);
      expect(result.msgCount).toBeGreaterThanOrEqual(1);
    });
  });

  describe('checkUnicode function', () => {
    it('should return false for ASCII-only text', () => {
      expect(checkUnicode('Hello World 123')).toBe(false);
    });

    it('should return true for Unicode characters', () => {
      expect(checkUnicode('你好')).toBe(true);
    });

    it('should return true for mixed ASCII and Unicode', () => {
      expect(checkUnicode('Hello你好')).toBe(true);
    });

    it('should return false for supported GSM-7 extended characters', () => {
      expect(checkUnicode('@£$¥èéù')).toBe(false);
    });

    it('should return false for empty string', () => {
      expect(checkUnicode('')).toBe(false);
    });

    it('should return false for GSM-7 extended escape characters', () => {
      expect(checkUnicode('\f^{}[]~|€')).toBe(false);
    });

    it('should return true for Japanese characters', () => {
      expect(checkUnicode('こんにちは')).toBe(true);
    });

    it('should return true for Korean characters', () => {
      expect(checkUnicode('안녕하세요')).toBe(true);
    });

    it('should return true for Cyrillic characters', () => {
      expect(checkUnicode('Привет')).toBe(true);
    });

    it('should return false for all supported extended ASCII', () => {
      const supported = 'èéùìòÇØøÅåΔΦΓΛΩΠΨΣΘΞÆæßÉÄÖÑÜäöñüà£¥¤§¿€';
      expect(checkUnicode(supported)).toBe(false);
    });

    it('should return true for emoji', () => {
      expect(checkUnicode('Hello 🌍')).toBe(true);
    });

    it('should return false for numeric characters', () => {
      expect(checkUnicode('0123456789')).toBe(false);
    });

    it('should return false for special ASCII characters', () => {
      expect(checkUnicode('!@#$%^&*()_+-=[]{}|;:,.<>?')).toBe(false);
    });

    it('should handle newline in check', () => {
      expect(checkUnicode('Hello\nWorld')).toBe(false);
    });

    it('should handle carriage return in check', () => {
      expect(checkUnicode('Hello\r\nWorld')).toBe(false);
    });

    it('should handle form feed in check', () => {
      expect(checkUnicode('Hello\fWorld')).toBe(false);
    });

    it('should handle non-breaking space in check', () => {
      expect(checkUnicode('Hello\u00a0World')).toBe(false);
    });

    it('should return true when any non-GSM7 character is present', () => {
      expect(checkUnicode('ABCDEFGHIJKLMNOPQRSTUVWXYZ你')).toBe(true);
    });

    it('should handle mixed supported and unsupported Unicode', () => {
      expect(checkUnicode('è 你好 €')).toBe(true);
    });

    it('should return false for message with only spaces', () => {
      expect(checkUnicode('     ')).toBe(false);
    });

    it('should return true for message with tabs (tabs are not in GSM-7 charset)', () => {
      // Tab character (\t) is not in the GSM-7 character set, so it returns true
      expect(checkUnicode('Hello\t\tWorld')).toBe(true);
    });
  });

  describe('Integration Tests', () => {
    it('should correctly identify and count Unicode message', () => {
      const msg = 'Hello你好World';
      const isUnicode = checkUnicode(msg);
      const counts = updateCharCount(msg);

      expect(isUnicode).toBe(true);
      if (msg.length > 70) {
        expect(counts.msgCount).toBeGreaterThan(1);
      }
    });

    it('should correctly identify and count ASCII message', () => {
      const msg = 'Hello World This is a test message';
      const isUnicode = checkUnicode(msg);
      const counts = updateCharCount(msg);

      expect(isUnicode).toBe(false);
      if (msg.length > 160) {
        expect(counts.msgCount).toBeGreaterThan(1);
      }
    });

    it('should handle long Unicode messages correctly', () => {
      const msg = '你好'.repeat(50); // 100 characters
      const isUnicode = checkUnicode(msg);
      const counts = updateCharCount(msg);

      expect(isUnicode).toBe(true);
      expect(counts.msgCount).toBeGreaterThan(1);
    });

    it('should handle long ASCII messages correctly', () => {
      const msg = 'A'.repeat(300);
      const isUnicode = checkUnicode(msg);
      const counts = updateCharCount(msg);

      expect(isUnicode).toBe(false);
      expect(counts.msgCount).toBeGreaterThan(1);
    });

    it('should handle boundary case: exactly 160 ASCII characters', () => {
      const msg = 'A'.repeat(160);
      const counts = updateCharCount(msg);

      expect(counts.msgCount).toBe(1);
      expect(counts.charLeft).toBe(0);
    });

    it('should handle boundary case: exactly 70 Unicode characters', () => {
      const msg = '你'.repeat(70);
      const counts = updateCharCount(msg);

      expect(counts.msgCount).toBe(1);
    });
  });
});
