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

jest.mock('next-intl', () => ({ useTranslations: () => (k: string) => k, useLocale: () => 'en' }));

import SupportAlertCard from './SupportAlertCard';
import type { SupportAlert } from '@/services/admin/types';

const OPEN_ALERT: SupportAlert = {
  id: 42,
  type: 'low_rating',
  severity: 'high',
  status: 'open',
  entityType: 'Review',
  entityId: '44',
  bookingId: 5001,
  reviewId: 44,
  ownerUserId: null,
  resolutionNote: null,
  resolvedAt: null,
  createdAt: '2026-01-01T00:00:00Z',
};

describe('<SupportAlertCard/>', () => {
  const wrap = (ui: React.ReactNode) => render(<ThemeProvider>{ui}</ThemeProvider>);

  it('renders the alert type and status labels', () => {
    wrap(<SupportAlertCard alert={OPEN_ALERT} />);
    expect(screen.getByText('atype_low_rating')).toBeInTheDocument();
    expect(screen.getByText('astatus_open')).toBeInTheDocument();
  });

  it('hides actions unless canAct', () => {
    wrap(<SupportAlertCard alert={OPEN_ALERT} />);
    expect(screen.queryByText('alert_resolve')).not.toBeInTheDocument();
  });

  it('fires assign/resolve callbacks when actionable', () => {
    const onAssignSelf = jest.fn();
    const onResolve = jest.fn();
    wrap(<SupportAlertCard alert={OPEN_ALERT} canAct onAssignSelf={onAssignSelf} onResolve={onResolve} />);
    fireEvent.click(screen.getByText('alert_assign_me'));
    fireEvent.click(screen.getByText('alert_resolve'));
    expect(onAssignSelf).toHaveBeenCalledWith(OPEN_ALERT);
    expect(onResolve).toHaveBeenCalledWith(OPEN_ALERT);
  });

  it('does not offer actions on a resolved alert', () => {
    wrap(<SupportAlertCard alert={{ ...OPEN_ALERT, status: 'resolved', resolutionNote: 'done' }} canAct />);
    expect(screen.queryByText('alert_resolve')).not.toBeInTheDocument();
    expect(screen.getByText('done')).toBeInTheDocument();
  });
});
