import React from 'react';
import { mountWithIntl, shallowWithIntl } from '../../../../helpers/intl-enzym-test-helpers';
import FormActions from './FormActions';
import CapButton from '@capillarytech/cap-ui-library/CapButton';
import CapError from '@capillarytech/cap-ui-library/CapError';
import CapRow from '@capillarytech/cap-ui-library/CapRow';

describe('FormActions', () => {
  const mockFormatMessage = jest.fn((message) => message.defaultMessage || message);
  const mockOnSave = jest.fn();

  const mockMessages = {
    saveTemplate: {
      defaultMessage: 'Save Template',
    },
  };

  const defaultProps = {
    onSave: mockOnSave,
    isSaveDisabled: false,
    errorText: '',
    accountErrorText: '',
    formatMessage: mockFormatMessage,
    messages: mockMessages,
  };

  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('Rendering', () => {
    it('should render correctly with default props', () => {
      const wrapper = shallowWithIntl(<FormActions {...defaultProps} />);
      expect(wrapper).toMatchSnapshot();
    });

    it('should render save button', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} />);
      const saveButton = wrapper.find(CapButton);
      expect(saveButton.exists()).toBe(true);
      expect(saveButton.prop('type')).toBe('primary');
      expect(saveButton.prop('disabled')).toBe(false);
    });

    it('should render save button with correct text', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} />);
      const saveButton = wrapper.find(CapButton);
      expect(saveButton.exists()).toBe(true);
      expect(mockFormatMessage).toHaveBeenCalledWith(mockMessages.saveTemplate);
    });

    it('should disable save button when isSaveDisabled is true', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} isSaveDisabled={true} />);
      const saveButton = wrapper.find(CapButton);
      expect(saveButton.prop('disabled')).toBe(true);
    });

    it('should enable save button when isSaveDisabled is false', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} isSaveDisabled={false} />);
      const saveButton = wrapper.find(CapButton);
      expect(saveButton.prop('disabled')).toBe(false);
    });
  });

  describe('Error Messages', () => {
    it('should not render error text when errorText is empty', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} errorText="" />);
      const errorRows = wrapper.find(CapRow).findWhere((node) => {
        const error = node.find(CapError);
        return error.exists() && error.prop('className') === 'webpush-template-error';
      });
      expect(errorRows.length).toBe(0);
    });

    it('should render error text when errorText is provided', () => {
      const errorText = 'Template save failed';
      const wrapper = mountWithIntl(<FormActions {...defaultProps} errorText={errorText} />);
      const error = wrapper.find(CapError).find('.webpush-template-error');
      expect(error.exists()).toBe(true);
      expect(error.length).toBeGreaterThan(0);
    });

    it('should not render account error text when accountErrorText is empty', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} accountErrorText="" />);
      const errorRows = wrapper.find(CapRow).findWhere((node) => {
        const error = node.find(CapError);
        return error.exists() && error.prop('className') === 'webpush-template-account-error';
      });
      expect(errorRows.length).toBe(0);
    });

    it('should render account error text when accountErrorText is provided', () => {
      const accountErrorText = 'Account error occurred';
      const wrapper = mountWithIntl(
        <FormActions {...defaultProps} accountErrorText={accountErrorText} />
      );
      const error = wrapper.find(CapError).find('.webpush-template-account-error');
      expect(error.exists()).toBe(true);
      expect(error.length).toBeGreaterThan(0);
    });

    it('should render both error texts when both are provided', () => {
      const wrapper = mountWithIntl(
        <FormActions
          {...defaultProps}
          errorText="Template error"
          accountErrorText="Account error"
        />
      );
      const templateError = wrapper.find(CapError).find('.webpush-template-error');
      const accountError = wrapper.find(CapError).find('.webpush-template-account-error');
      expect(templateError.exists()).toBe(true);
      expect(accountError.exists()).toBe(true);
    });
  });

  describe('Save Button Interaction', () => {
    it('should call onSave when save button is clicked', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} />);
      const saveButton = wrapper.find(CapButton);
      saveButton.simulate('click');
      expect(mockOnSave).toHaveBeenCalledTimes(1);
    });

    it('should not call onSave when button is disabled and clicked', () => {
      const wrapper = mountWithIntl(<FormActions {...defaultProps} isSaveDisabled={true} />);
      const saveButton = wrapper.find(CapButton);
      saveButton.simulate('click');
      // Even if disabled, the click event might fire, but the button should be disabled
      expect(saveButton.prop('disabled')).toBe(true);
      expect(defaultProps.onSave).not.toHaveBeenCalled();
    });
  });

  describe('Default Props', () => {
    it('should use default errorText when not provided', () => {
      const wrapper = mountWithIntl(
        <FormActions
          onSave={mockOnSave}
          isSaveDisabled={false}
          formatMessage={mockFormatMessage}
          messages={mockMessages}
        />
      );
      const error = wrapper.find(CapError).find('.webpush-template-error');
      expect(error.exists()).toBe(false);
    });

    it('should use default accountErrorText when not provided', () => {
      const wrapper = mountWithIntl(
        <FormActions
          onSave={mockOnSave}
          isSaveDisabled={false}
          formatMessage={mockFormatMessage}
          messages={mockMessages}
        />
      );
      const error = wrapper.find(CapError).find('.webpush-template-account-error');
      expect(error.exists()).toBe(false);
    });
  });
});

