import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen, waitFor } from '@testing-library/react';
import { Routes, Route } from 'react-router-dom';
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 {},
}));

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

const get = api.get as ReturnType<typeof vi.fn>;
// 2024-06-01 12:00 محلی
const slotStart = Math.floor(new Date('2024-06-01T12:00:00').getTime() / 1000);

beforeEach(() => {
  get.mockReset();
  get.mockResolvedValue({
    success: true,
    data: {
      uuid: 'ap1', patient_name: 'ساغر', patient_mobile: '09120000000',
      slot_start: slotStart, slot_end: slotStart + 1800, status: 'confirmed', version: 1, created_at: slotStart,
    },
  });
});

describe('AppointmentDetailPage — بازگشت به همان روز', () => {
  it('the «نوبت‌ها» breadcrumb points back to the appointment day', async () => {
    renderWithProviders(
      <Routes>
        <Route path="/admin/appointments/:uuid" element={<AppointmentDetailPage />} />
      </Routes>,
      { route: '/admin/appointments/ap1' },
    );
    // پس از بارگذاری نوبت، لینک «نوبت‌ها» باید به روزِ نوبت اشاره کند
    await waitFor(() => {
      const link = screen.getByText('نوبت‌ها').closest('a');
      expect(link?.getAttribute('href')).toContain('date=2024-06-01');
    });
  });
});
