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';
import type { Project } from '../../resources/js/stores/projectsStore';

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,
    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: Project }) =>
    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);
    });

    // --- Эпик 3 (редизайн): источник редактируем + объявление + подтверждение ---

    it('source identifier editable in edit mode (НЕ readonly)', async () => {
        const locked = { ...sampleProject, source_locked: true, source_unlock_at: '2026-06-27T21:00:00+03:00' };
        const wrapper = factory({ modelValue: true, project: locked });
        await flushPromises();
        const input = wrapper.find('[data-testid="np-source-identifier"] input');
        expect(input.exists()).toBe(true);
        expect((input.element as HTMLInputElement).readOnly).toBe(false);
    });

    it('показывает объявление о вступлении при правке лимита на залоченном проекте', async () => {
        const locked = { ...sampleProject, source_locked: true, source_unlock_at: '2026-06-27T21:00:00+03:00' };
        const wrapper = factory({ modelValue: true, project: locked });
        await flushPromises();
        await wrapper.find('[data-testid="np-limit"] input').setValue('20');
        expect(wrapper.find('[data-testid="np-applies-from-banner"]').exists()).toBe(true);
    });

    it('смена источника на залоченном проекте требует подтверждения (PATCH после confirm)', async () => {
        const locked = { ...sampleProject, source_locked: true, source_unlock_at: '2026-06-27T21:00:00+03:00' };
        const wrapper = factory({ modelValue: true, project: locked });
        await flushPromises();

        await wrapper.find('[data-testid="np-source-identifier"] input').setValue('novyy-sayt.ru');
        await wrapper.find('[data-testid="submit-btn"]').trigger('click');
        await flushPromises();
        // Диалог подтверждения открыт, PATCH ещё нет.
        expect(wrapper.find('[data-testid="np-source-change-confirm"]').exists()).toBe(true);
        expect(apiClient.patch).not.toHaveBeenCalled();

        await wrapper.find('[data-testid="np-source-confirm-yes"]').trigger('click');
        await flushPromises();
        expect(apiClient.patch).toHaveBeenCalled();
    });

    it('смена источника на НЕзалоченном проекте сохраняется без подтверждения', async () => {
        const wrapper = factory({ modelValue: true, project: { ...sampleProject, source_locked: false } });
        await flushPromises();
        await wrapper.find('[data-testid="np-source-identifier"] input').setValue('novyy-sayt.ru');
        await wrapper.find('[data-testid="submit-btn"]').trigger('click');
        await flushPromises();
        expect(wrapper.find('[data-testid="np-source-change-confirm"]').exists()).toBe(false);
        expect(apiClient.patch).toHaveBeenCalled();
    });
});
