import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } 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 {},
}));

import { api } from '../lib/api';
import { useAuthStore } from '../stores/authStore';
import AppointmentsPage from './AppointmentsPage';

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

beforeEach(() => {
  get.mockReset();
  useAuthStore.setState({ primaryRole: 'doctor', dbUuid: 'doc1' } as any);
  get.mockImplementation((url: string) => {
    if (url.includes('today-stats')) return Promise.resolve({ success: true, data: { total: 7, completed: 3, waiting: 2, cancelled: 1 } });
    if (url.includes('appointment-slots')) return Promise.resolve({ success: true, data: { sessions: [] } });
    if (url.includes('/my/appointments')) return Promise.resolve({ success: true, data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } });
    return Promise.resolve({ success: true, data: [] });
  });
});

describe('AppointmentsPage — طرح نوبت‌ها', () => {
  it('renders the title, the stats bar and the افزودن نوبت action', async () => {
    renderWithProviders(<AppointmentsPage />);
    expect(screen.getByText('نوبت‌ ها')).toBeInTheDocument();
    expect(screen.getByText('کل نوبت های امروز')).toBeInTheDocument();
    expect(await screen.findByText('۷')).toBeInTheDocument(); // total from stats
    expect(screen.getByText('افزودن نوبت')).toBeInTheDocument();
  });

  it('timeline view shows the holiday message when the doctor has no slots', async () => {
    renderWithProviders(<AppointmentsPage />);
    // نمای پیش‌فرض زمانبندی است و پزشک (نقش doctor) از قبل انتخاب شده
    expect(await screen.findByText('این روز تعطیل است')).toBeInTheDocument();
  });
});
