import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import { describe, expect, it } from 'vitest';
import DashboardGroupedTable from '@/components/dashboard/DashboardGroupedTable.vue';

describe('DashboardGroupedTable', () => {
    it('adapts dashboard presentation data to the shared typed pivot contract', () => {
        const wrapper = mount(DashboardGroupedTable, {
            props: {
                id: 'dashboard-test-pivot',
                tableLabel: 'Commitment',
                columns: [
                    { key: 'date', label: 'Date', type: 'date' },
                    {
                        key: 'commitment',
                        label: 'Commitment (EUR m)',
                        type: 'scaled-number',
                        numeric: true,
                        separator: true,
                    },
                ],
                groups: [{
                    id: 'investor-1',
                    label: 'Atlas Pension Trust',
                    rows: [{
                        id: 'investment-1',
                        href: '/investments/investment-1',
                        investor: { label: 'Atlas Pension Trust', href: '/investors/investor-1' },
                        fund: { label: 'Alpha Growth IV', href: '/funds/fund-1' },
                        cells: { date: '30 Jun 2026', commitment: '12.5' },
                    }],
                    subtotal: { commitment: '12.5' },
                }],
                subtotalLabelColspan: 3,
            },
        });

        expect(wrapper.get('.erp-pivot-table').attributes('id')).toBe('dashboard-test-pivot');
        expect(wrapper.get('.erp-pivot-table__group-copy').text()).toContain('1 investment');
        expect(wrapper.findAll('.erp-cell-link').map((link) => link.text())).toEqual(['Alpha Growth IV']);
        expect(wrapper.find('th[aria-label="Sort Investor ascending"]').exists()).toBe(false);
        expect(wrapper.get('th.erp-pivot-table__summary-label').attributes('colspan')).toBe('3');
        expect(wrapper.get('th.erp-pivot-table__summary-label').text()).toBe('Subtotal — Atlas Pension Trust');
        expect(wrapper.get('th.erp-pivot-table__divider-before').text()).toContain('Commitment');
    });

    it('maps the adaptive subtotal config to a blended collapsed group row', async () => {
        const wrapper = mount(DashboardGroupedTable, {
            props: {
                id: 'dashboard-adaptive-pivot',
                tableLabel: 'Commitment',
                columns: [{
                    key: 'commitment',
                    label: 'Commitment (EUR m)',
                    type: 'scaled-number',
                    numeric: true,
                    separator: true,
                }],
                groups: [{
                    id: 'investor-1',
                    label: 'Atlas Pension Trust',
                    rows: [{
                        id: 'investment-1',
                        href: '/investments/investment-1',
                        investor: { label: 'Atlas Pension Trust', href: '/investors/investor-1' },
                        fund: { label: 'Alpha Growth IV', href: '/funds/fund-1' },
                        cells: { commitment: '12.5' },
                    }],
                    subtotal: { commitment: '12.5' },
                }],
                subtotalPosition: 'adaptive',
                subtotalLabelColspan: 1,
            },
        });

        expect(wrapper.get('.erp-pivot-table__summary-row').text()).toContain('Subtotal — Atlas Pension Trust');

        await wrapper.get('.erp-pivot-table__group-control').trigger('click');
        await nextTick();

        expect(wrapper.find('.erp-pivot-table__summary-row').exists()).toBe(false);
        expect(wrapper.get('.erp-pivot-table__group-row--adaptive-summary').text()).toContain('Atlas Pension Trust');
        expect(wrapper.get('.erp-pivot-table__group-row--adaptive-summary').text()).toContain('12.5');
        expect(wrapper.get('.erp-pivot-table__group-row--adaptive-summary').text()).not.toContain('Subtotal —');
    });
});
