/**
 * Tests for parseSenderDetailsResponse
 * Covers sender ID / delivery settings response parsing for SMS, EMAIL, WHATSAPP
 */

import { parseSenderDetailsResponse } from '../../../DeliverySettings/utils/parseSenderDetailsResponse';

describe('parseSenderDetailsResponse', () => {
  describe('when entity is missing or null', () => {
    it('should return empty domains when response is null', () => {
      expect(parseSenderDetailsResponse('SMS', null)).toEqual({ domains: [] });
    });

    it('should return empty domains when response is undefined', () => {
      expect(parseSenderDetailsResponse('SMS', undefined)).toEqual({ domains: [] });
    });

    it('should return empty domains when entity is missing', () => {
      expect(parseSenderDetailsResponse('SMS', {})).toEqual({ domains: [] });
    });
  });

  describe('when entity is object with channel key', () => {
    it('should parse SMS channel entity array', () => {
      const response = {
        entity: {
          SMS: [
            {
              id: 10,
              priority: 1,
              domainProperties: {
                domainName: 'SMS Domain 1',
                id: 100,
                contactInfo: [
                  { type: 'gsm_sender_id', valid: true, value: 'GSM1', default: true },
                  { type: 'cdma_sender_id', valid: true, value: 'CDMA1' },
                ],
                connectionProperties: {},
              },
            },
          ],
        },
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0]).toMatchObject({
        dgmId: 10,
        domainName: 'SMS Domain 1',
        domainId: 100,
        priority: 1,
      });
      expect(result.domains[0].gsmSenders).toHaveLength(1);
      expect(result.domains[0].gsmSenders[0].value).toBe('GSM1');
      expect(result.domains[0].cdmaSenders).toHaveLength(1);
      expect(result.domains[0].cdmaSenders[0].value).toBe('CDMA1');
    });

    it('should parse EMAIL channel entity array', () => {
      const response = {
        entity: {
          EMAIL: [
            {
              id: 20,
              priority: 0,
              domainProperties: {
                domainName: 'Email Domain',
                id: 200,
                contactInfo: [
                  { type: 'sender_id', valid: true, value: 'noreply@example.com' },
                  { type: 'reply_to_id', valid: true, value: 'reply@example.com' },
                ],
              },
            },
          ],
        },
      };
      const result = parseSenderDetailsResponse('EMAIL', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0]).toMatchObject({
        dgmId: 20,
        domainName: 'Email Domain',
        domainId: 200,
        priority: 0,
      });
      expect(result.domains[0].emailSenders).toHaveLength(1);
      expect(result.domains[0].emailSenders[0].value).toBe('noreply@example.com');
      expect(result.domains[0].emailRepliers).toHaveLength(1);
      expect(result.domains[0].emailRepliers[0].value).toBe('reply@example.com');
    });

    it('should parse WHATSAPP channel with sourceAccountIdentifier', () => {
      const response = {
        entity: {
          WHATSAPP: [
            {
              id: 30,
              priority: 0,
              domainProperties: {
                domainName: 'WABA Domain',
                id: 300,
                contactInfo: [],
                connectionProperties: {
                  sourceAccountIdentifier: 'waba-123',
                },
              },
            },
          ],
        },
      };
      const result = parseSenderDetailsResponse('WHATSAPP', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0].sourceAccountIdentifier).toBe('waba-123');
      expect(result.domains[0].gsmSenders).toEqual([]);
      expect(result.domains[0].cdmaSenders).toEqual([]);
    });
  });

  describe('when entity is direct array', () => {
    it('should use entity as channelSenderDetails when entity is array', () => {
      const response = {
        entity: [
          {
            id: 1,
            priority: 0,
            domainProperties: { domainName: 'Direct Domain', id: 1 },
          },
        ],
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0].domainName).toBe('Direct Domain');
    });
  });

  describe('filtering and sorting', () => {
    it('should filter contactInfo by type and valid', () => {
      const response = {
        entity: {
          SMS: [
            {
              id: 1,
              priority: 0,
              domainProperties: {
                domainName: 'D1',
                id: 1,
                contactInfo: [
                  { type: 'gsm_sender_id', valid: true, value: 'A' },
                  { type: 'gsm_sender_id', valid: false, value: 'B' },
                  { type: 'other', valid: true, value: 'C' },
                ],
              },
            },
          ],
        },
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains[0].gsmSenders).toHaveLength(1);
      expect(result.domains[0].gsmSenders[0].value).toBe('A');
    });

    it('should sort domains by priority', () => {
      const response = {
        entity: {
          SMS: [
            { id: 1, priority: 2, domainProperties: { domainName: 'Second', id: 1 } },
            { id: 2, priority: 0, domainProperties: { domainName: 'First', id: 2 } },
          ],
        },
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains[0].domainName).toBe('First');
      expect(result.domains[1].domainName).toBe('Second');
    });

    it('should dedupe by domainName keeping first occurrence', () => {
      const response = {
        entity: {
          SMS: [
            { id: 1, priority: 0, domainProperties: { domainName: 'Same', id: 1 } },
            { id: 2, priority: 1, domainProperties: { domainName: 'Same', id: 2 } },
          ],
        },
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0].domainId).toBe(1);
    });
  });

  describe('edge cases', () => {
    it('should return empty domains when channel key has no array', () => {
      const response = { entity: { SMS: null } };
      expect(parseSenderDetailsResponse('SMS', response)).toEqual({ domains: [] });
    });

    it('should return empty domains when channel key is missing and entity is not array', () => {
      const response = { entity: { EMAIL: [] } };
      expect(parseSenderDetailsResponse('SMS', response)).toEqual({ domains: [] });
    });

    it('should handle missing domainProperties with defaults', () => {
      const response = {
        entity: {
          SMS: [{ id: 1, priority: 0 }],
        },
      };
      const result = parseSenderDetailsResponse('SMS', response);
      expect(result.domains).toHaveLength(1);
      expect(result.domains[0].domainId).toBe(-1);
      expect(result.domains[0].domainName).toBeUndefined();
      expect(result.domains[0].gsmSenders).toEqual([]);
      expect(result.domains[0].cdmaSenders).toEqual([]);
    });

    it('should use wabaId when sourceAccountIdentifier missing for WHATSAPP', () => {
      const response = {
        entity: {
          WHATSAPP: [
            {
              id: 1,
              priority: 0,
              domainProperties: {
                domainName: 'W',
                id: 1,
                connectionProperties: { wabaId: 'waba-456' },
              },
            },
          ],
        },
      };
      const result = parseSenderDetailsResponse('WHATSAPP', response);
      expect(result.domains[0].sourceAccountIdentifier).toBe('waba-456');
    });
  });
});
