import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen, waitFor } from '@testing-library/react';
import { renderWithProviders } from '../test/utils';

vi.mock('../lib/api', () => ({
  api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
  ApiError: class extends Error {},
}));

vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));

import { api } from '../lib/api';
import AppointmentSegmentsCard from './AppointmentSegmentsCard';

const get = api.get as ReturnType<typeof vi.fn>;

const segment = (over: Record<string, unknown>) => ({
  sequence: 1,
  name: 'ویزیت',
  starts_at: 1_800_000_000,
  ends_at: 1_800_001_200,
  duration_minutes: 20,
  patient_present: true,
  ...over,
});

describe('AppointmentSegmentsCard', () => {
  beforeEach(() => vi.clearAllMocks());

  it('sums the recorded segments rather than the service duration', async () => {
    get.mockResolvedValue({
      data: [
        segment({ sequence: 1, duration_minutes: 20 }),
        segment({ sequence: 2, name: 'انتظار', duration_minutes: 40, patient_present: false }),
      ],
    });

    renderWithProviders(<AppointmentSegmentsCard appointmentUuid="a-1" />);

    await waitFor(() => expect(screen.getByText('60 دقیقه')).toBeInTheDocument());
  });

  /** ⭐ مدتی که بیمار روی صندلی نیست باید دیده شود، وگرنه «۹۰ دقیقه» گمراه‌کننده است. */
  it('marks the segments the patient is not present for', async () => {
    get.mockResolvedValue({
      data: [segment({ sequence: 1, name: 'انتظار', patient_present: false })],
    });

    renderWithProviders(<AppointmentSegmentsCard appointmentUuid="a-1" />);

    await waitFor(() => expect(screen.getByText('بدون حضور بیمار')).toBeInTheDocument());
  });

  /** نوبت اسلاتی بخشی ندارد؛ کارتِ خالی یعنی «چیزی خراب است». */
  it('renders nothing for an appointment with no segments', async () => {
    get.mockResolvedValue({ data: [] });

    const { container } = renderWithProviders(<AppointmentSegmentsCard appointmentUuid="a-1" />);

    await waitFor(() => expect(get).toHaveBeenCalled());
    expect(container.textContent).toBe('');
  });
});
