import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createVuetify } from 'vuetify';
import axios from 'axios';
import AdminSupplierIntegrationView from '../../resources/js/views/admin/AdminSupplierIntegrationView.vue';

vi.mock('axios');

const vuetify = createVuetify();

describe('AdminSupplierIntegrationView — export-mode toggle (Plan 4 Task 1)', () => {
    beforeEach(() => {
        vi.clearAllMocks();
        (axios.get as ReturnType<typeof vi.fn>).mockImplementation((url: string) => {
            if (url.endsWith('/export-mode')) {
                return Promise.resolve({ data: { mode: 'batch' } });
            }
            if (url.endsWith('/manual-queue')) {
                return Promise.resolve({ data: { queue: [] } });
            }
            return Promise.resolve({ data: { health: null, history: [] } });
        });
        (axios.post as ReturnType<typeof vi.fn>).mockResolvedValue({ data: { mode: 'online' } });
    });

    it('GETs current mode on mount and renders the toggle with current label', async () => {
        const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
        await new Promise((r) => setTimeout(r, 50));

        expect(axios.get).toHaveBeenCalledWith('/api/admin/supplier-integration/export-mode');
        const toggle = wrapper.find('[data-testid="export-mode-toggle"]');
        expect(toggle.exists()).toBe(true);
        expect(wrapper.text()).toContain('Режим экспорта проектов');
        expect(wrapper.text()).toContain('Пакетный');
    });

    it('switching to online POSTs the new value', async () => {
        const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
        await new Promise((r) => setTimeout(r, 50));

        const onlineBtn = wrapper.find('[data-testid="export-mode-online"]');
        expect(onlineBtn.exists()).toBe(true);
        await onlineBtn.trigger('click');
        await new Promise((r) => setTimeout(r, 20));

        expect(axios.post).toHaveBeenCalledWith('/api/admin/supplier-integration/export-mode', { mode: 'online' });
    });
});
