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

// next-intl mocked to read the REAL fa message file so the emergency playbook copy is pinned as it ships.
jest.mock('next-intl', () => ({
  useTranslations: (namespace: string) => (key: string, params?: Record<string, unknown>) => {
    const messages = jest.requireActual('../../../messages/fa.json') as Record<string, Record<string, string>>;
    let value = messages[namespace]?.[key] ?? key;
    if (params) for (const [k, v] of Object.entries(params)) value = value.replace(`{${k}}`, String(v));
    return value;
  },
}));

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

describe('<EmergencyBanner/> component', () => {
  it('surfaces a tel: click-to-call when an emergency contact phone is provided', () => {
    render(
      <ThemeProvider>
        <EmergencyBanner contactName="زهرا موسوی" contactPhone="09121234567" onOpenTicket={() => {}} />
      </ThemeProvider>,
    );
    expect(screen.getByTestId('emergency-banner')).toBeInTheDocument();
    expect(screen.getByTestId('emergency-call')).toHaveAttribute('href', 'tel:09121234567');
  });

  it('degrades to the open-ticket path (no tel: link) when the contact cannot be loaded', () => {
    render(
      <ThemeProvider>
        <EmergencyBanner onOpenTicket={() => {}} />
      </ThemeProvider>,
    );
    expect(screen.getByTestId('emergency-banner')).toBeInTheDocument();
    expect(screen.queryByTestId('emergency-call')).not.toBeInTheDocument();
  });

  it('invokes onOpenTicket from the "open a ticket" action', () => {
    const onOpenTicket = jest.fn();
    render(
      <ThemeProvider>
        <EmergencyBanner onOpenTicket={onOpenTicket} />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByRole('button'));
    expect(onOpenTicket).toHaveBeenCalledTimes(1);
  });
});
