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 — manual queue section', () => {
    beforeEach(() => {
        vi.clearAllMocks();
        (axios.get as ReturnType<typeof vi.fn>).mockImplementation((url: string) => {
            if (url.endsWith('/manual-queue')) {
                return Promise.resolve({
                    data: {
                        queue: [
                            {
                                id: 1,
                                project_id: 42,
                                platform: 'B1',
                                operation: 'create',
                                external_id: null,
                                payload_snapshot: { limit: 10, signal_type: 'site', unique_key: 'foo.com' },
                                failure_reason: 'contract_break',
                                created_at: '2026-05-19T10:00:00Z',
                            },
                        ],
                    },
                });
            }
            return Promise.resolve({ data: { health: null, history: [] } });
        });
    });

    it('renders pending queue rows with payload + reason', async () => {
        const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
        await new Promise((r) => setTimeout(r, 50));

        const text = wrapper.text();
        expect(text).toContain('foo.com');
        expect(text).toContain('contract_break');
        expect(text).toContain('B1');
    });

    it('clicking «Отметить выполнено» calls resolve endpoint', async () => {
        (axios.post as ReturnType<typeof vi.fn>).mockResolvedValue({
            data: { resolved: true, external_id: 700123 },
        });
        vi.spyOn(window, 'confirm').mockReturnValue(true);

        const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
        await new Promise((r) => setTimeout(r, 50));

        const btn = wrapper.find('[data-testid="resolve-1"]');
        expect(btn.exists()).toBe(true);
        await btn.trigger('click');

        expect(axios.post).toHaveBeenCalledWith(expect.stringContaining('/manual-queue/1/resolve'));
    });
});
