import { render, screen, fireEvent } from '@testing-library/react';

const mockMutate = jest.fn();
const mockReset = jest.fn();

jest.mock('@/services/tickets', () => ({
  useOpenTicket: () => ({ mutate: mockMutate, isPending: false, isError: false, reset: mockReset }),
}));

jest.mock('next/navigation', () => ({
  ...jest.requireActual('next/navigation'),
  useRouter: () => ({ push: jest.fn() }),
}));

jest.mock('next-intl', () => ({
  useLocale: () => 'fa',
  useTranslations: (namespace: string) => (key: string) => {
    const messages = jest.requireActual('../../../messages/fa.json') as Record<string, Record<string, string>>;
    return messages[namespace]?.[key] ?? key;
  },
}));

import { ThemeProvider } from '../../theme';
import ContactSupportDialog from './ContactSupportDialog';

function renderDialog() {
  render(
    <ThemeProvider>
      <ContactSupportDialog open onClose={() => {}} role="customer" defaultCategory="support" />
    </ThemeProvider>,
  );
}

describe('<ContactSupportDialog/> component', () => {
  beforeEach(() => {
    mockMutate.mockClear();
  });

  it('opens a ticket with the typed message on submit', () => {
    renderDialog();
    fireEvent.change(screen.getByLabelText(/پیام/), { target: { value: 'سوال من درباره ویزیت' } });
    fireEvent.click(screen.getByRole('button', { name: 'ارسال' }));
    expect(mockMutate).toHaveBeenCalledTimes(1);
    expect(mockMutate).toHaveBeenCalledWith(
      expect.objectContaining({ category: 'support', body: 'سوال من درباره ویزیت' }),
      expect.anything(),
    );
  });

  it('disables submit until a message is typed', () => {
    renderDialog();
    expect(screen.getByRole('button', { name: 'ارسال' })).toBeDisabled();
  });
});
