import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ThemeProvider } from '../../theme';
import AddressCard, { type AddressCardProps } from './AddressCard';

const BASE: AddressCardProps = {
  title: 'Home',
  regionLabel: 'Tehran · District 1',
  addressLine: 'No. 5, Vali St',
  isPrimary: false,
  primaryLabel: 'Primary',
  onEdit: jest.fn(),
  onDelete: jest.fn(),
  onSetPrimary: jest.fn(),
  editLabel: 'Edit',
  deleteLabel: 'Delete',
  setPrimaryLabel: 'Set as primary',
};

function renderCard(overrides: Partial<AddressCardProps> = {}) {
  const props = { ...BASE, ...overrides };
  render(
    <ThemeProvider>
      <AddressCard {...props} />
    </ThemeProvider>,
  );
  return props;
}

describe('<AddressCard/> component', () => {
  it('renders the title, region label and street line', () => {
    renderCard();
    expect(screen.getByText('Home')).toBeInTheDocument();
    expect(screen.getByText('Tehran · District 1')).toBeInTheDocument();
    expect(screen.getByText('No. 5, Vali St')).toBeInTheDocument();
  });

  it('shows the primary badge and hides set-primary on the primary card', () => {
    renderCard({ isPrimary: true });
    expect(screen.getByText('Primary')).toBeInTheDocument();
    expect(screen.queryByText('Set as primary')).not.toBeInTheDocument();
  });

  it('offers set-primary only on a non-primary card and fires it', async () => {
    const user = userEvent.setup();
    const props = renderCard({ isPrimary: false });
    await user.click(screen.getByText('Set as primary'));
    expect(props.onSetPrimary).toHaveBeenCalledTimes(1);
  });

  it('calls onEdit and onDelete from the action buttons', async () => {
    const user = userEvent.setup();
    const props = renderCard();
    await user.click(screen.getByLabelText('Edit'));
    await user.click(screen.getByLabelText('Delete'));
    expect(props.onEdit).toHaveBeenCalledTimes(1);
    expect(props.onDelete).toHaveBeenCalledTimes(1);
  });
});
