import {
  HOST_HAPTIC,
  HAPTIC_CATEGORY_OPTIONS,
  CATEGORY_OPTIONS_MAP,
} from '../constants';

// Mock API calls
jest.mock('../../../services/api', () => ({
  createWhatsappTemplate: jest.fn(),
  uploadFile: jest.fn(),
  getAllTemplates: jest.fn(),
}));

describe('HAPTIC WhatsApp Vendor Integration', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('Constants and Configuration', () => {
    it('should import HOST_HAPTIC constant', () => {
      expect(HOST_HAPTIC).toBe('hapticwhatsappbulk');
    });

    it('should have HAPTIC_CATEGORY_OPTIONS defined', () => {
      expect(HAPTIC_CATEGORY_OPTIONS).toBeDefined();
      expect(Array.isArray(HAPTIC_CATEGORY_OPTIONS)).toBe(true);
      expect(HAPTIC_CATEGORY_OPTIONS.length).toBeGreaterThan(0);
    });

    it('should include HAPTIC vendor in CATEGORY_OPTIONS_MAP', () => {
      expect(CATEGORY_OPTIONS_MAP[HOST_HAPTIC]).toBeDefined();
      expect(CATEGORY_OPTIONS_MAP[HOST_HAPTIC]).toBe(HAPTIC_CATEGORY_OPTIONS);
    });

    it('should have correct HAPTIC category options', () => {
      const hapticOptions = CATEGORY_OPTIONS_MAP[HOST_HAPTIC];
      expect(hapticOptions).toContainEqual(
        expect.objectContaining({
          value: 'MARKETING',
        })
      );
      expect(hapticOptions).toContainEqual(
        expect.objectContaining({
          value: 'UTILITY',
        })
      );
    });
  });

  describe('Utility Functions', () => {
    it('should create HAPTIC payload with correct field names', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const mockHapticAccount = {
        selectedWhatsappAccount: {
          sourceAccountIdentifier: 'waba-123',
          configs: {
            accessToken: 'haptic-token',
            promotionalMessagingSSID: 'phone-456',
          },
          hostName: HOST_HAPTIC,
        },
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
        language: 'en',
      };

      const payload = createHapticPayload(templateData, mockHapticAccount);

      expect(payload).toEqual({
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
        language: 'en',
        wabaId: 'waba-123',
        access_token: 'haptic-token',
        PhoneId: 'phone-456',
        hostName: HOST_HAPTIC,
        status: 'unsubmitted',
      });
    });

    it('should create standard payload with correct field names', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const mockStandardAccount = {
        selectedWhatsappAccount: {
          sourceAccountIdentifier: 'account-123',
          configs: {
            accessToken: 'standard-token',
            promotionalMessagingSSID: 'phone-456',
          },
          hostName: 'gupshupwhatsappbulk',
        },
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
        language: 'en',
      };

      const payload = createHapticPayload(templateData, mockStandardAccount);

      expect(payload).toEqual({
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
        language: 'en',
        accountId: 'account-123',
        accessToken: 'standard-token',
        phoneId: 'phone-456',
        hostName: 'gupshupwhatsappbulk',
        status: 'unsubmitted',
      });
    });

    it('should handle missing account data', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
      };

      const payload = createHapticPayload(templateData, null);

      expect(payload).toEqual({
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
      });
    });
  });

  describe('Edge Cases', () => {
    it('should handle empty string values', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const accountDataWithEmptyStrings = {
        selectedWhatsappAccount: {
          sourceAccountIdentifier: '',
          configs: {
            accessToken: '',
            promotionalMessagingSSID: '',
          },
          hostName: HOST_HAPTIC,
        },
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
      };

      const payload = createHapticPayload(templateData, accountDataWithEmptyStrings);

      expect(payload.wabaId).toBe('');
      expect(payload.access_token).toBe('');
      expect(payload.PhoneId).toBe('');
    });

    it('should handle null values', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const accountDataWithNulls = {
        selectedWhatsappAccount: {
          sourceAccountIdentifier: null,
          configs: {
            accessToken: null,
            promotionalMessagingSSID: null,
          },
          hostName: HOST_HAPTIC,
        },
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
      };

      const payload = createHapticPayload(templateData, accountDataWithNulls);

      expect(payload.wabaId).toBeNull();
      expect(payload.access_token).toBeNull();
      expect(payload.PhoneId).toBeNull();
    });

    it('should handle undefined values', () => {
      const createHapticPayload = (templateData, accountData) => {
        const { selectedWhatsappAccount } = accountData || {};

        if (!selectedWhatsappAccount) {
          return templateData;
        }

        const { sourceAccountIdentifier, configs = {}, hostName } = selectedWhatsappAccount;
        const { accessToken, promotionalMessagingSSID } = configs;

        const payload = { ...templateData };

        if (hostName === HOST_HAPTIC) {
          // HAPTIC uses different field names
          payload.wabaId = sourceAccountIdentifier;
          payload.access_token = accessToken;
          payload.PhoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        } else {
          // Standard vendors
          payload.accountId = sourceAccountIdentifier;
          payload.accessToken = accessToken;
          payload.phoneId = promotionalMessagingSSID;
          payload.hostName = hostName;
        }

        // Add default status for new templates
        payload.status = payload.status || 'unsubmitted';

        return payload;
      };

      const accountDataWithUndefined = {
        selectedWhatsappAccount: {
          sourceAccountIdentifier: undefined,
          configs: {
            accessToken: undefined,
            promotionalMessagingSSID: undefined,
          },
          hostName: HOST_HAPTIC,
        },
      };

      const templateData = {
        name: 'Test Template',
        category: 'MARKETING',
        content: 'Hello {{1}}',
      };

      const payload = createHapticPayload(templateData, accountDataWithUndefined);

      expect(payload.wabaId).toBeUndefined();
      expect(payload.access_token).toBeUndefined();
      expect(payload.PhoneId).toBeUndefined();
    });
  });
});
