import { describe, it, expect, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import { createVuetify } from 'vuetify';
import { createPinia, setActivePinia } from 'pinia';
import RegionsBulkDialog from '../../resources/js/components/projects/RegionsBulkDialog.vue';

const vuetify = createVuetify();

const mountDialog = (count = 5) =>
    mount(RegionsBulkDialog, {
        props: { modelValue: true, count },
        global: {
            plugins: [vuetify, createPinia()],
            stubs: { VDialog: { template: '<div><slot /></div>' } },
        },
    });

describe('RegionsBulkDialog', () => {
    beforeEach(() => setActivePinia(createPinia()));

    it('renders 8 federal-district chips for Add and Remove', () => {
        const wrapper = mountDialog();
        const addChips = wrapper.findAll('[data-testid^="region-add-"]');
        const removeChips = wrapper.findAll('[data-testid^="region-remove-"]');
        expect(addChips.length).toBe(8);
        expect(removeChips.length).toBe(8);
    });

    it('shows count from prop', () => {
        const wrapper = mountDialog(7);
        expect(wrapper.text()).toContain('7');
    });

    it('emits apply with computed bitmasks', async () => {
        const wrapper = mountDialog();
        // Toggle Центральный (bit 1) in Add
        await wrapper.find('[data-testid="region-add-1"]').trigger('click');
        // Toggle Сибирский (bit 64) in Remove
        await wrapper.find('[data-testid="region-remove-64"]').trigger('click');
        await wrapper.find('[data-testid="apply"]').trigger('click');

        expect(wrapper.emitted('apply')?.[0]).toEqual([{ add: 1, remove: 64 }]);
    });

    it('apply button disabled when both add and remove are 0', () => {
        const wrapper = mountDialog();
        const btn = wrapper.find('[data-testid="apply"]');
        expect(btn.attributes('disabled')).toBeDefined();
    });
});
