import { describe, it, expect, vi, beforeEach } from 'vitest';

// --- clinicApi (native fetch) ---
import { getClinicDoctors } from '@/services/clinicApi';

describe('getClinicDoctors', () => {
    beforeEach(() => vi.stubGlobal('fetch', vi.fn()));

    it('پاسخ double-nested → { data, page }', async () => {
        fetch.mockResolvedValue({
            ok: true,
            json: () =>
                Promise.resolve({
                    data: { data: [{ uuid: 'd1' }, { uuid: 'd2' }], meta: { totalPages: 3, currentPage: 1 } },
                }),
        });
        const out = await getClinicDoctors('my-clinic', { page: 1 });
        expect(out.data).toHaveLength(2);
        expect(out.page).toEqual({ total_pages: 3, current: 1 });
        const calledUrl = fetch.mock.calls[0][0];
        expect(calledUrl).toContain('/api/v1/clinic/doctor-list/my-clinic');
    });

    it('بدون meta → صفحه‌بندی پیش‌فرض', async () => {
        fetch.mockResolvedValue({ ok: true, json: () => Promise.resolve({ data: { data: [] } }) });
        const out = await getClinicDoctors('x');
        expect(out.page).toEqual({ total_pages: 1, current: 1 });
    });

    it('پاسخ ناموفق (ok=false) → shape خالی پیش‌فرض', async () => {
        fetch.mockResolvedValue({ ok: false, status: 500, text: () => Promise.resolve('err') });
        const out = await getClinicDoctors('x');
        expect(out).toEqual({ data: [], page: { total_pages: 1, current: 1 } });
    });

    it('خطای شبکه → shape خالی پیش‌فرض', async () => {
        fetch.mockRejectedValue(new Error('net'));
        const out = await getClinicDoctors('x');
        expect(out.data).toEqual([]);
    });
});

// --- response.js wrappers (mock axios layer) ---
vi.mock('@/services/api', () => ({
    default: {
        get: vi.fn(() => Promise.resolve({})),
        post: vi.fn(() => Promise.resolve({})),
        patch: vi.fn(() => Promise.resolve({})),
        delete: vi.fn(() => Promise.resolve({})),
    },
}));

import api from '@/services/api';
import { request } from '@/services/response';

describe('request wrappers — قرارداد به لایه‌ی API', () => {
    beforeEach(() => {
        api.get.mockClear();
        api.post.mockClear();
    });

    it('getUserProfile → GET مسیر درست با requireAuth', () => {
        request.getUserProfile('u1');
        expect(api.get).toHaveBeenCalledWith('/api/v1/user-profile/u1', { requireAuth: true });
    });

    it('getUserInfo → GET oauth/userinfo با requireAuth', () => {
        request.getUserInfo();
        expect(api.get).toHaveBeenCalledWith('oauth/userinfo', { requireAuth: true });
    });

    it('postAppointment → POST با body و requireAuth', () => {
        request.postAppointment({ slot: 5 });
        expect(api.post).toHaveBeenCalledWith('api/v1/appointment', { slot: 5 }, { requireAuth: true });
    });
});
