import { describe, it, expect, beforeEach, 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 { api } from '../lib/api';
import { useAuthStore } from '../stores/authStore';
import AppointmentSettingsPage from './AppointmentSettingsPage';

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

beforeEach(() => {
  get.mockReset();
  useAuthStore.setState({ primaryRole: 'doctor', doctorUuid: 'doc-1', dbUuid: 'doc-1' });
  get.mockImplementation((url: string) => {
    if (url.includes('/doctor/doc-1')) return Promise.resolve({ success: true, data: { data: { address: [] } } });
    if (url.includes('/weekly-schedule')) return Promise.resolve({ success: true, data: {} });
    return Promise.resolve({ success: true, data: {} });
  });
});

describe('AppointmentSettingsPage', () => {
  it('renders the weekly-schedule section inside the settings shell', async () => {
    renderWithProviders(<AppointmentSettingsPage />, { route: '/admin/appointment-settings' });
    // appears in the shell menu and as the page heading
    expect((await screen.findAllByText('مدیریت نوبت دهی')).length).toBeGreaterThan(1);
    expect(screen.getByText('خرید اشتراک')).toBeInTheDocument(); // shell menu
  });
});
