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

vi.mock('axios');

const mountView = async () => {
    setActivePinia(createPinia());
    vi.mocked(axios.get).mockResolvedValue({
        data: {
            data: [
                {
                    id: 1,
                    name: 'A',
                    signal_type: 'site',
                    signal_identifier: 'a.ru',
                    daily_limit_target: 100,
                    delivered_today: 0,
                    is_active: true,
                    region_mask: 1,
                    region_mode: 'include',
                    delivery_days_mask: 31,
                    sync_status: 'ok',
                },
                {
                    id: 2,
                    name: 'B',
                    signal_type: 'site',
                    signal_identifier: 'b.ru',
                    daily_limit_target: 100,
                    delivered_today: 0,
                    is_active: true,
                    region_mask: 1,
                    region_mode: 'include',
                    delivery_days_mask: 31,
                    sync_status: 'ok',
                },
            ],
            meta: { total: 2 },
        },
    });
    const wrapper = mount(ProjectsView, {
        global: {
            plugins: [createVuetify()],
            stubs: { ProjectCard: true, BulkActionsBar: true, NewProjectDialog: true, EditProjectDialog: true },
        },
    });
    await new Promise((r) => setTimeout(r, 50)); // flush async fetch
    return wrapper;
};

describe('ProjectsView toolbar — select all', () => {
    it('renders select-all checkbox above grid', async () => {
        const wrapper = await mountView();
        expect(wrapper.find('[data-testid="select-all-toggle"]').exists()).toBe(true);
    });

    it('shows count "Выбрано: N из M"', async () => {
        const wrapper = await mountView();
        expect(wrapper.text()).toMatch(/Выбрано:\s*0\s*из\s*2/);
    });

    it('clicking select-all marks all visible projects', async () => {
        const wrapper = await mountView();
        const toggle = wrapper.find('[data-testid="select-all-toggle"] input');
        await toggle.setValue(true);

        const { useProjectsStore } = await import('../../resources/js/stores/projectsStore');
        const store = useProjectsStore();
        expect(store.selectAllByFilter).toBe(true);
        expect(store.selectedIds.size).toBe(2);
    });
});
