import React from 'react';
import { configureStore } from '@capillarytech/vulcan-react-sdk/utils';
import { initialReducer } from '../../../../initialReducer';
import { Provider } from 'react-redux';
import { mountWithIntl } from '../../../../../app/helpers/intl-enzym-test-helpers';
import { SmsTraiEdit } from '../index';
import { mockData } from './mockData';
import history from '../../../../utils/history';
jest.mock('../../../../services/api', () => ({
  request: jest.fn(),
}));
jest.mock('../../../../utils/smsCharCountV2', () => ({
  updateCharCount: ()=>({
    chars_sms: 160,
    chars_used: 29,
    optoutUrlPresent: true,
    optouturlLength: 9,
    parts: 1,
    responseState: "SUCCESS",
    unicode: false,
    char_list:[]
  })}));
jest.mock('../../../TagList/index.js', () => ({
  __esModule: true,
  default: (props) => (
    <div className="tag-mock" {...props}>
      TagList
    </div>
  ),
}));
let store;
beforeAll(() => {
  store = configureStore({}, initialReducer, history);
});

// Mock Date to return a fixed date for consistent snapshots
// This prevents RcsPreviewContent timestamp from changing on each test run
const FIXED_DATE = new Date('2024-01-15T10:30:00'); // Fixed date: Jan 15, 2024, 10:30 AM
const OriginalDate = global.Date;

beforeEach(() => {
  // Mock Date constructor to return fixed date
  global.Date = jest.fn((...args) => {
    if (args.length === 0) {
      return FIXED_DATE;
    }
    return new OriginalDate(...args);
  });
  // Preserve Date static methods
  global.Date.now = jest.fn(() => FIXED_DATE.getTime());
  Object.setPrototypeOf(global.Date, OriginalDate);
  Object.assign(global.Date, OriginalDate);
});

afterEach(() => {
  // Restore original Date after each test
  global.Date = OriginalDate;
});


describe('Creatives SmsTraiEdit test/>', () => {
  let renderedComponent;
  const getTemplateDetails = jest.fn();
  const resetEditTemplate = jest.fn();
  const clearEditResponse = jest.fn();
  const editTemplate = jest.fn();
  const fetchSchemaForEntity = jest.fn(()=>({}));
  const formatMessage = jest.fn();
  const handleClose = jest.fn();
  const getFormSubscriptionData = jest.fn();
  const params = {
    id: mockData.templateDetails._id,
  };
  const location = mockData.location;
  const templateDetails = mockData.templateDetails;
  beforeEach(() => {
    renderedComponent = mountWithIntl(
      <Provider store={store}>
        <SmsTraiEdit
          actions={{
            getTemplateDetails,
            resetEditTemplate,
            clearEditResponse,
            editTemplate,
          }}
          globalActions={{ fetchSchemaForEntity }}
          handleClose={handleClose}
          getFormSubscriptionData={getFormSubscriptionData}
          params={params}
          templateDetails={templateDetails}
          location={location}
          intl={{ formatMessage }}
          isFullMode={true}
        />
      </Provider>,
    );
  });
  it('should render', () => {
    const event = { target: { value: "test1", id: "1" } };
    expect(renderedComponent).toMatchSnapshot();
    renderedComponent.find('textarea').at(0).simulate('change', event);
    renderedComponent.update();
    expect(renderedComponent).toMatchSnapshot();
  })
  it('should save', () => {
    renderedComponent.find('button.create-dlt-msg').props().onClick();
    renderedComponent.update();
    expect(renderedComponent).toMatchSnapshot();
  });
});
