import { mount } from '@vue/test-utils';
import { describe, expect, it } from 'vitest';
import { KviewPerformanceBadge } from '@kview/visualization/vue';
import ActivityFeed from '@/components/activity/ActivityFeed.vue';
import AppLink from '@/components/application/AppLink.vue';
import type {
    ActivityFilter,
    ActivityItem,
} from '@/types/activity';

const filters: ActivityFilter[] = [
    {
        key: 'all',
        label: 'Tout',
        count: 2,
        active: false,
        href: '/activity?activity=all',
        linkProps: { inertia: true },
    },
    {
        key: 'added',
        label: 'Ajouté',
        count: 1,
        active: true,
        href: '/activity?activity=added',
        linkProps: { inertia: true },
    },
];

const item: ActivityItem = {
    id: 'added:gp:1',
    title: 'Northstar Partners',
    description: 'General Partner',
    icon: 'building',
    tone: 'positive',
    status: 'added',
    statusLabel: 'Ajouté',
    href: '/gps/1',
    linkProps: { inertia: true },
    time: {
        label: '2 minutes ago',
        datetime: '2035-01-15T12:00:00+01:00',
    },
};

describe('ActivityFeed', () => {
    it('renders an accessible feed with same-root filters and Inertia record links', () => {
        const wrapper = mount(ActivityFeed, {
            props: {
                filters,
                items: [item],
            },
        });
        const section = wrapper.get('section');
        const heading = wrapper.get('h2');
        const links = wrapper.findAllComponents(AppLink);

        expect(section.attributes('aria-labelledby')).toBe(heading.attributes('id'));
        expect(wrapper.get('nav').attributes('aria-label')).toBe('Filtres d’activité');
        expect(wrapper.get('[aria-current="true"]').text()).toContain('Ajouté');
        expect(links[0].props()).toMatchObject({
            inertia: true,
            preserveState: true,
            preserveScroll: true,
        });
        expect(links[1].props()).toMatchObject({
            inertia: true,
            preserveState: true,
            preserveScroll: true,
        });
        expect(links[2].props('href')).toBe('/gps/1');
        expect(links[2].props('inertia')).toBe(true);
        expect(wrapper.getComponent(KviewPerformanceBadge).props()).toMatchObject({
            label: 'Ajouté',
            tone: 'positive',
            icon: 'none',
        });
        expect(wrapper.get('.ui-performance-badge').text()).toBe('Ajouté');
        expect(wrapper.get('time').attributes('datetime')).toBe('2035-01-15T12:00:00+01:00');
        expect(wrapper.get('time').text()).toBe('2 minutes ago');
    });

    it('supports an optional title, subtitle and same-shell action link for compact hosts', () => {
        const wrapper = mount(ActivityFeed, {
            props: {
                filters,
                items: [item],
                title: 'Activité récente',
                subtitle: 'Derniers items ajoutés, modifiés ou consultés',
                actionLabel: 'Tout voir',
                actionHref: '/activity',
            },
        });

        expect(wrapper.get('h2').text()).toBe('Activité récente');
        expect(wrapper.get('p').text()).toBe('Derniers items ajoutés, modifiés ou consultés');

        const action = wrapper.findAllComponents(AppLink)[0];

        expect(action.props('href')).toBe('/activity');
        expect(action.props('inertia')).toBe(true);
        expect(action.text()).toContain('Tout voir');
    });

    it('omits the action link when no label/href pair is supplied', () => {
        const wrapper = mount(ActivityFeed, {
            props: {
                filters,
                items: [item],
            },
        });

        expect(wrapper.get('h2').text()).toBe('Historique récent');
        expect(wrapper.findAllComponents(AppLink)).toHaveLength(3);
    });

    it('uses the shared compact empty state without an empty row list', () => {
        const wrapper = mount(ActivityFeed, {
            props: {
                filters,
                items: [],
            },
        });

        expect(wrapper.find('.ui-update-list').exists()).toBe(false);
        expect(wrapper.get('.ui-empty-state--sm').text()).toContain('Aucune activité');
        expect(wrapper.text()).toContain('Aucun item ne correspond à ce filtre.');
    });
});
