import { fireEvent, render, screen } from '@testing-library/react';
import { ThemeProvider } from '../../theme';
import type { NurseServiceVariant } from '@/services/catalog/types';

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

import VariantCard from './VariantCard';

const baseVariant: NurseServiceVariant = {
  id: 1,
  serviceCategoryId: 1,
  categoryNameFa: 'مراقبت از سالمند',
  categoryNameEn: 'Elderly Care',
  price: '2800000', // 280,000 Toman
  priceUnit: 'per_hour',
  sessionCount: null,
  displayName: 'Elderly Care · Live-in',
  isActive: true,
  options: [],
};

function renderCard(variant: NurseServiceVariant, onEdit = jest.fn(), onToggleActive = jest.fn()) {
  render(
    <ThemeProvider>
      <VariantCard variant={variant} onEdit={onEdit} onToggleActive={onToggleActive} />
    </ThemeProvider>,
  );
  return { onEdit, onToggleActive };
}

describe('<VariantCard/> component', () => {
  it('renders the display name, category, and price', () => {
    renderCard(baseVariant);
    expect(screen.getByText('Elderly Care · Live-in')).toBeInTheDocument();
    expect(screen.getByText('Elderly Care')).toBeInTheDocument();
    expect(screen.getByText(/280,000/)).toBeInTheDocument();
  });

  it('shows the active chip and the deactivate action for an active variant', () => {
    renderCard(baseVariant);
    expect(screen.getByText('active_chip')).toBeInTheDocument();
    expect(screen.getByText('deactivate')).toBeInTheDocument();
    expect(screen.queryByText('inactive_hint')).not.toBeInTheDocument();
  });

  it('shows the deactivated distinction (chip, hint, reactivate action) for an inactive variant', () => {
    renderCard({ ...baseVariant, isActive: false });
    expect(screen.getByText('inactive_chip')).toBeInTheDocument();
    expect(screen.getByText('inactive_hint')).toBeInTheDocument();
    expect(screen.getByText('activate')).toBeInTheDocument();
  });

  it('never renders a delete affordance', () => {
    renderCard(baseVariant);
    expect(screen.queryByText('delete')).not.toBeInTheDocument();
  });

  it('fires onEdit and onToggleActive from the row actions', () => {
    const { onEdit, onToggleActive } = renderCard(baseVariant);
    fireEvent.click(screen.getByText('edit'));
    expect(onEdit).toHaveBeenCalledTimes(1);
    fireEvent.click(screen.getByText('deactivate'));
    expect(onToggleActive).toHaveBeenCalledTimes(1);
  });
});
