import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount, flushPromises } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import { createVuetify } from 'vuetify';

vi.mock('../../resources/js/api/client', () => ({
    apiClient: {
        post: vi.fn().mockResolvedValue({ data: {} }),
        patch: vi.fn().mockResolvedValue({ data: {} }),
    },
    ensureCsrfCookie: vi.fn().mockResolvedValue(undefined),
    extractErrorMessage: vi.fn(() => 'Произошла ошибка.'),
}));

import { apiClient } from '../../resources/js/api/client';
import EditProjectDialog from '../../resources/js/views/projects/EditProjectDialog.vue';

const sampleProject = {
    id: 1,
    name: 'X',
    signal_type: 'site' as const,
    signal_identifier: 'x.ru',
    daily_limit_target: 10,
    delivered_today: 0,
    is_active: true,
    archived_at: null,
    sync_status: 'ok' as const,
    region_mask: 0,
    region_mode: 'include',
    delivery_days_mask: 127,
};

// VDialog в JSDOM не рендерит через teleport — стаб делает <slot/> доступным
// для wrapper.text() / find(). Паттерн из NewProjectDialog.spec.ts.
const factory = (props: { modelValue: boolean; project: typeof sampleProject }) =>
    mount(EditProjectDialog, {
        props,
        global: {
            plugins: [createVuetify()],
            stubs: {
                VDialog: {
                    template: '<div class="dialog-stub" v-if="modelValue"><slot /></div>',
                    props: ['modelValue'],
                },
            },
        },
    });

beforeEach(() => {
    setActivePinia(createPinia());
    vi.clearAllMocks();
});

describe('EditProjectDialog', () => {
    it('prefills form from project prop', async () => {
        const wrapper = factory({ modelValue: true, project: sampleProject });
        await flushPromises();
        expect(wrapper.text()).toContain('Редактирование');
    });

    it('PATCH on submit', async () => {
        const wrapper = factory({ modelValue: true, project: sampleProject });
        await flushPromises();
        await wrapper.find('[data-testid="submit-btn"]').trigger('click');
        await flushPromises();
        expect(apiClient.patch).toHaveBeenCalled();
    });

    it('signal_type tabs disabled in edit mode', async () => {
        const wrapper = factory({ modelValue: true, project: sampleProject });
        await flushPromises();
        expect(wrapper.findComponent({ name: 'VTabs' }).props('disabled')).toBe(true);
    });
});
