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 — 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/archive 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-archive"]').exists()).toBe(true);
    });
});
