/**
 * Unit tests for ExistingCustomerModal
 */

import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { IntlProvider } from 'react-intl';
import PropTypes from 'prop-types';
import ExistingCustomerModal from '../ExistingCustomerModal';
import { CHANNELS, CUSTOMER_MODAL_EXISTING } from '../constants';

const mockMessages = {
  'app.v2Components.TestAndPreviewSlidebox.customerCreationModalTitle': 'Add new test customer',
  'app.v2Components.TestAndPreviewSlidebox.existingCustomerModalDescription': 'This user profile already exists in the system.',
  'app.v2Components.TestAndPreviewSlidebox.saveButton': 'Save',
  'app.v2Components.TestAndPreviewSlidebox.cancelButton': 'Cancel',
  'app.v2Components.TestAndPreviewSlidebox.customerEmail': 'Email',
  'app.v2Components.TestAndPreviewSlidebox.customerMobileNumber': 'Mobile Number',
  'app.v2Components.TestAndPreviewSlidebox.customerID': 'Customer ID',
};

const TestWrapper = ({ children }) => (
  <IntlProvider locale="en" messages={mockMessages}>
    {children}
  </IntlProvider>
);
TestWrapper.propTypes = { children: PropTypes.node };

describe('ExistingCustomerModal', () => {
  const defaultProps = {
    customerModal: [true, CUSTOMER_MODAL_EXISTING],
    onCloseCustomerModal: jest.fn(),
    customerData: { name: 'John Doe', email: 'john@example.com', mobile: '', customerId: 'cust-123' },
    channel: CHANNELS.EMAIL,
    onSave: jest.fn(),
  };

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

  it('renders modal with customer details when visible', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal {...defaultProps} />
      </TestWrapper>
    );
    expect(screen.getByText(/add new test customer/i)).toBeTruthy();
    expect(screen.getByText(/this user profile already exists/i)).toBeTruthy();
    expect(screen.getByText('John Doe')).toBeTruthy();
    expect(screen.getByText(/john@example.com/)).toBeTruthy();
    expect(screen.getByText(/cust-123/)).toBeTruthy();
    expect(screen.getByRole('button', { name: /save/i })).toBeTruthy();
    expect(screen.getByRole('button', { name: /cancel/i })).toBeTruthy();
  });

  it('shows mobile when channel is SMS', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal
          {...defaultProps}
          channel={CHANNELS.SMS}
          customerData={{ name: 'Jane', email: '', mobile: '9123456789', customerId: 'cust-456' }}
        />
      </TestWrapper>
    );
    expect(screen.getByText(/9123456789/)).toBeTruthy();
  });

  it('calls onCloseCustomerModal when Cancel is clicked', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal {...defaultProps} />
      </TestWrapper>
    );
    fireEvent.click(screen.getByRole('button', { name: /cancel/i }));
    expect(defaultProps.onCloseCustomerModal).toHaveBeenCalled();
  });

  it('calls onSave when Save is clicked', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal {...defaultProps} />
      </TestWrapper>
    );
    fireEvent.click(screen.getByRole('button', { name: /save/i }));
    expect(defaultProps.onSave).toHaveBeenCalled();
    expect(defaultProps.onSave.mock.calls[0][0]).toEqual({});
    expect(typeof defaultProps.onSave.mock.calls[0][1]).toBe('function');
  });

  it('shows dash for missing name', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal {...defaultProps} customerData={{ name: '', email: 'a@b.co', mobile: '', customerId: 'c1' }} />
      </TestWrapper>
    );
    expect(screen.getByText('-')).toBeTruthy();
  });

  it('does not show email row when channel is EMAIL but customerData.email is empty', () => {
    render(
      <TestWrapper>
        <ExistingCustomerModal
          {...defaultProps}
          customerData={{ name: 'No Email', email: '', mobile: '', customerId: 'c2' }}
        />
      </TestWrapper>
    );
    expect(screen.getByText('No Email')).toBeTruthy();
    expect(screen.getByText('Customer ID')).toBeTruthy();
    expect(screen.getByText('c2')).toBeTruthy();
  });
});
