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

vi.mock('axios');

const vuetify = createVuetify();

// VDialog телепортит контент в body → стаб рендерит слот инлайн (квирк: VDialog
// teleport стаб для поиска confirm-кнопки внутри диалога).
const mountView = () =>
    mount(AdminSupplierProjectsView, {
        global: {
            plugins: [vuetify],
            stubs: { VDialog: { template: '<div><slot /></div>' } },
        },
    });

describe('AdminSupplierProjectsView (Plan 4 Task 3)', () => {
    beforeEach(() => {
        vi.clearAllMocks();
        (axios.get as ReturnType<typeof vi.fn>).mockResolvedValue({
            data: {
                projects: [
                    {
                        id: 1,
                        platform: 'B1',
                        signal_type: 'site',
                        unique_key: 'okna.ru',
                        subject_code: 82,
                        subject_name: 'Москва',
                        current_limit: 5,
                        supplier_external_id: '777',
                        orderers: ['ООО Ромашка'],
                        last_delivery_at: '2026-05-19T10:00:00Z',
                    },
                ],
            },
        });
        (axios.post as ReturnType<typeof vi.fn>).mockResolvedValue({
            data: { deleted: 1, failures: [] },
        });
    });

    it('GETs list on mount and renders rows (source, region, orderers)', async () => {
        const wrapper = mountView();
        await flushPromises();

        expect(axios.get).toHaveBeenCalledWith('/api/admin/supplier-integration/projects');
        const text = wrapper.text();
        expect(text).toContain('okna.ru');
        expect(text).toContain('Москва');
        expect(text).toContain('ООО Ромашка');
    });

    it('bulk-deletes selected rows after confirm', async () => {
        const wrapper = mountView();
        await flushPromises();

        await wrapper.find('[data-testid="row-checkbox-1"] input').setValue(true);
        await wrapper.find('[data-testid="bulk-delete-btn"]').trigger('click');
        await flushPromises();
        await wrapper.find('[data-testid="confirm-delete-btn"]').trigger('click');
        await flushPromises();

        expect(axios.post).toHaveBeenCalledWith('/api/admin/supplier-integration/projects/delete', { ids: [1] });
    });

    it('bulk-delete button is disabled when nothing selected', async () => {
        const wrapper = mountView();
        await flushPromises();

        const btn = wrapper.find('[data-testid="bulk-delete-btn"]');
        expect(btn.attributes('disabled')).toBeDefined();
    });
});
