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

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

import SessionCard, { SessionCardProps } from './SessionCard';

const BASE: SessionCardProps = {
  sessionIndex: 1,
  scheduledDate: '2026-08-01',
  scheduledTimeStart: '09:00:00',
  scheduledTimeEnd: '13:00:00',
  status: 'scheduled',
};

function renderCard(props: Partial<SessionCardProps> = {}) {
  return render(
    <ThemeProvider>
      <SessionCard {...BASE} {...props} />
    </ThemeProvider>,
  );
}

describe('<SessionCard/> component', () => {
  it('renders the visit index and a per-session status chip', () => {
    const { container } = renderCard();
    expect(screen.getByText('session_index')).toBeInTheDocument();
    expect(screen.getByText('sstatus_scheduled')).toBeInTheDocument();
    expect(container.querySelector('[data-session-status="scheduled"]')).toBeInTheDocument();
  });

  it('shows no EVV controls for the customer view', () => {
    renderCard({ showEvvControls: false });
    expect(screen.queryByText('evv_check_in')).not.toBeInTheDocument();
  });

  it('offers check-in for a scheduled session in the nurse view and fires onCheckIn', () => {
    const onCheckIn = jest.fn();
    renderCard({ showEvvControls: true, onCheckIn });
    const button = screen.getByText('evv_check_in');
    fireEvent.click(button);
    expect(onCheckIn).toHaveBeenCalledTimes(1);
  });

  it('offers check-out once checked in', () => {
    renderCard({ showEvvControls: true, status: 'in_progress', evvStatus: 'checked_in', checkInAt: '2026-08-01T09:02:00.000Z' });
    expect(screen.getByText('evv_check_out')).toBeInTheDocument();
    // The in-range banner renders from the server check-in time (default match null → no-gps variant here).
    expect(screen.getByText('evv_banner_no_gps')).toBeInTheDocument();
  });

  it('shows the in-range EVV banner when the address matched', () => {
    renderCard({ status: 'in_progress', evvStatus: 'checked_in', checkInAt: '2026-08-01T09:02:00.000Z', checkInAddressMatch: true });
    expect(screen.getByText('evv_banner_in_range')).toBeInTheDocument();
  });

  it('shows the acquiring-location label while capturing GPS', () => {
    renderCard({ showEvvControls: true, acquiringLocation: true });
    expect(screen.getByText('evv_acquiring_location')).toBeInTheDocument();
  });
});
