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

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

import AppointmentTurnCard, { type AppointmentCardData } from './AppointmentTurnCard';

const base: AppointmentCardData = {
  uuid: 'a1', starts_at: 1754000000, status: 'confirmed', version: 1,
  doctor_name: 'دکتر ژیلا فتحی', service_name: null,
};

describe('AppointmentTurnCard', () => {
  it('renders title, date/time labels, doctor and the live status label', () => {
    renderWithProviders(<AppointmentTurnCard appointment={base} queryKey={['x']} />);
    expect(screen.getByText('نوبت')).toBeInTheDocument();       // no service → generic title
    expect(screen.getByText('تاریخ:')).toBeInTheDocument();
    expect(screen.getByText('ساعت:')).toBeInTheDocument();
    expect(screen.getByText('پرسنل:')).toBeInTheDocument();
    expect(screen.getByText('دکتر ژیلا فتحی')).toBeInTheDocument();
    expect(screen.getByText('قطعی شده')).toBeInTheDocument();    // confirmed via STATUS_META
  });

  it('falls back to «—» when the doctor is missing', () => {
    renderWithProviders(<AppointmentTurnCard appointment={{ ...base, doctor_name: null }} queryKey={['x']} />);
    expect(screen.getByText('—')).toBeInTheDocument();
  });

  it('prefers the service name as the title when present', () => {
    renderWithProviders(<AppointmentTurnCard appointment={{ ...base, service_name: 'لیزر' }} queryKey={['x']} />);
    expect(screen.getByText('لیزر')).toBeInTheDocument();
  });
});
