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

jest.mock('next-intl', () => ({
  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 EmergencyPlaybookRow from './EmergencyPlaybookRow';

describe('<EmergencyPlaybookRow/> component', () => {
  it('starts collapsed with the playbook body hidden', () => {
    render(
      <ThemeProvider>
        <EmergencyPlaybookRow onOpenTicket={() => {}} />
      </ThemeProvider>,
    );
    expect(screen.getByTestId('emergency-playbook-toggle')).toHaveAttribute('aria-expanded', 'false');
  });

  it('expands to reveal the playbook body + open-ticket action on tap', () => {
    render(
      <ThemeProvider>
        <EmergencyPlaybookRow onOpenTicket={() => {}} />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByTestId('emergency-playbook-toggle'));
    expect(screen.getByTestId('emergency-playbook-toggle')).toHaveAttribute('aria-expanded', 'true');
  });

  it('invokes onOpenTicket from the expanded open-ticket action', () => {
    const onOpenTicket = jest.fn();
    render(
      <ThemeProvider>
        <EmergencyPlaybookRow onOpenTicket={onOpenTicket} />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByTestId('emergency-playbook-toggle'));
    fireEvent.click(screen.getByRole('button', { name: /پشتیبانی/ }));
    expect(onOpenTicket).toHaveBeenCalledTimes(1);
  });
});
