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

import BulkActionsBar from '../../resources/js/components/projects/BulkActionsBar.vue';
import { useProjectsStore } from '../../resources/js/stores/projectsStore';

vi.mock('axios');

const defaultStubs = {
    VDialog: { template: '<div><slot /></div>' },
    DevIndexBadge: true,
    RegionsBulkDialog: true,
    DaysBulkDialog: true,
    LimitBulkDialog: true,
};

const factory = () => {
    vi.mocked(axios.post).mockResolvedValue({ data: { updated: 0, skipped: [], warnings: [] } });
    vi.mocked(axios.get).mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
    return mount(BulkActionsBar, {
        global: {
            plugins: [createVuetify()],
            stubs: defaultStubs,
        },
    });
};

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

describe('BulkActionsBar', () => {
    it('shows count of selected', () => {
        const store = useProjectsStore();
        store.selectedIds.add(1);
        store.selectedIds.add(2);
        const wrapper = factory();
        expect(wrapper.text()).toContain('2');
    });

    it('clicking Pause triggers store.bulkUpdate("pause") after confirm', async () => {
        const store = useProjectsStore();
        store.selectedIds.add(1);
        const spy = vi.spyOn(store, 'bulkUpdate').mockResolvedValue({ updated: 1, skipped: [], warnings: [] });
        window.confirm = vi.fn(() => true);
        const wrapper = factory();
        await wrapper.find('[data-testid="bulk-pause"]').trigger('click');
        expect(spy).toHaveBeenCalledWith({ action: 'pause' });
    });

    it('cancel confirm — bulkUpdate NOT called', async () => {
        const store = useProjectsStore();
        store.selectedIds.add(1);
        const spy = vi.spyOn(store, 'bulkUpdate').mockResolvedValue({ updated: 0, skipped: [], warnings: [] });
        window.confirm = vi.fn(() => false);
        const wrapper = factory();
        await wrapper.find('[data-testid="bulk-pause"]').trigger('click');
        expect(spy).not.toHaveBeenCalled();
    });

    it('Clear-selection button calls store.clearSelection', async () => {
        const store = useProjectsStore();
        store.selectedIds.add(1);
        const wrapper = factory();
        await wrapper.find('[data-testid="bulk-clear"]').trigger('click');
        expect(store.selectedIds.size).toBe(0);
    });
});

describe('BulkActionsBar snackbar replacement (Sprint 1 C5)', () => {
    it('runBulk with skipped > 0 opens snackbar (not window.alert)', async () => {
        setActivePinia(createPinia());
        const store = useProjectsStore();
        store.selectedIds.add(1);
        vi.spyOn(store, 'bulkUpdate').mockResolvedValue({
            updated: 5,
            skipped: [{ id: 7 }, { id: 8 }],
            warnings: [],
        } as never);
        const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
        window.confirm = vi.fn(() => true);

        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-pause"]').trigger('click');
        // Wait for async confirmAndRun + runBulk + DOM update
        await new Promise((r) => setTimeout(r, 30));

        // window.alert MUST NOT be called
        expect(alertSpy).not.toHaveBeenCalled();
        // snackbar state should be open
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        expect((wrapper.vm as any).skipToastOpen).toBe(true);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        expect((wrapper.vm as any).skipToastText).toContain('Применено: 5');
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        expect((wrapper.vm as any).skipToastText).toContain('Пропущено: 2');
    });

    it('runBulk with supplier_snapshot_locked reason shows specific text', async () => {
        setActivePinia(createPinia());
        const store = useProjectsStore();
        store.selectedIds.add(1);
        vi.spyOn(store, 'bulkUpdate').mockResolvedValue({
            updated: 0,
            skipped: [
                { id: 7, reason: 'supplier_snapshot_locked' },
                { id: 8, reason: 'supplier_snapshot_locked' },
            ],
            warnings: [],
        } as never);
        window.confirm = vi.fn(() => true);

        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-delete"]').trigger('click');
        await new Promise((r) => setTimeout(r, 30));

        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const text = (wrapper.vm as any).skipToastText as string;
        expect(text).toContain('2');
        expect(text.toLowerCase()).toContain('сбор лидов');
    });

    it('runBulk with mixed reasons shows both groups', async () => {
        setActivePinia(createPinia());
        const store = useProjectsStore();
        store.selectedIds.add(1);
        vi.spyOn(store, 'bulkUpdate').mockResolvedValue({
            updated: 3,
            skipped: [
                { id: 7, reason: 'supplier_snapshot_locked' },
                { id: 8, reason: 'has_deals' },
            ],
            warnings: [],
        } as never);
        window.confirm = vi.fn(() => true);

        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-delete"]').trigger('click');
        await new Promise((r) => setTimeout(r, 30));

        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const text = (wrapper.vm as any).skipToastText as string;
        expect(text.toLowerCase()).toContain('сбор лидов'); // supplier_snapshot_locked
        expect(text.toLowerCase()).toContain('сделки'); // has_deals
    });

    it('runBulk with balance_insufficient reason shows balance text', async () => {
        setActivePinia(createPinia());
        const store = useProjectsStore();
        store.selectedIds.add(1);
        vi.spyOn(store, 'bulkUpdate').mockResolvedValue({
            updated: 0,
            skipped: [
                { id: 7, reason: 'balance_insufficient' },
                { id: 8, reason: 'balance_insufficient' },
            ],
            warnings: [],
        } as never);
        window.confirm = vi.fn(() => true);

        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-delete"]').trigger('click');
        await new Promise((r) => setTimeout(r, 30));

        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const text = (wrapper.vm as any).skipToastText as string;
        expect(text).toContain('2');
        expect(text.toLowerCase()).toContain('баланс');
        // не должно ошибочно объявлять причиной «доставленные лиды»
        expect(text.toLowerCase()).not.toContain('доставленны');
    });

    it('runBulk with skipped=0 does NOT open snackbar', async () => {
        setActivePinia(createPinia());
        const store = useProjectsStore();
        store.selectedIds.add(1);
        vi.spyOn(store, 'bulkUpdate').mockResolvedValue({
            updated: 3,
            skipped: [],
            warnings: [],
        } as never);
        window.confirm = vi.fn(() => true);

        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-pause"]').trigger('click');
        await new Promise((r) => setTimeout(r, 30));

        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        expect((wrapper.vm as any).skipToastOpen).toBe(false);
    });
});

describe('BulkActionsBar — extended', () => {
    it('renders 3 new dialog-open buttons (Regions/Days/Limit)', async () => {
        const { wrapper } = await (async () => {
            setActivePinia(createPinia());
            vi.mocked(axios.post).mockResolvedValue({ data: { updated: 1, skipped: [], warnings: [] } });
            vi.mocked(axios.get).mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
            const store = useProjectsStore();
            store.selectedIds.add(1);
            store.selectedIds.add(2);
            const wrapper = mount(BulkActionsBar, {
                global: {
                    plugins: [createVuetify()],
                    stubs: defaultStubs,
                },
            });
            return { wrapper, store };
        })();
        expect(wrapper.find('[data-testid="bulk-regions"]').exists()).toBe(true);
        expect(wrapper.find('[data-testid="bulk-days"]').exists()).toBe(true);
        expect(wrapper.find('[data-testid="bulk-limit"]').exists()).toBe(true);
    });

    it('opens RegionsBulkDialog on regions button click', async () => {
        setActivePinia(createPinia());
        vi.mocked(axios.post).mockResolvedValue({ data: { updated: 1, skipped: [], warnings: [] } });
        vi.mocked(axios.get).mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
        const store = useProjectsStore();
        store.selectedIds.add(1);
        store.selectedIds.add(2);
        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        await wrapper.find('[data-testid="bulk-regions"]').trigger('click');
        expect((wrapper.vm as unknown as { regionsOpen: boolean }).regionsOpen).toBe(true);
    });

    it('keeps existing pause/resume/delete buttons', async () => {
        setActivePinia(createPinia());
        vi.mocked(axios.post).mockResolvedValue({ data: { updated: 1, skipped: [], warnings: [] } });
        vi.mocked(axios.get).mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
        const store = useProjectsStore();
        store.selectedIds.add(1);
        store.selectedIds.add(2);
        const wrapper = mount(BulkActionsBar, {
            global: {
                plugins: [createVuetify()],
                stubs: defaultStubs,
            },
        });
        expect(wrapper.find('[data-testid="bulk-pause"]').exists()).toBe(true);
        expect(wrapper.find('[data-testid="bulk-resume"]').exists()).toBe(true);
        expect(wrapper.find('[data-testid="bulk-delete"]').exists()).toBe(true);
    });
});
