import type { ActivityItem } from '@/types/admin';
import { render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import ActivityFeed from '../ActivityFeed';

const items: ActivityItem[] = [
    {
        type: 'signup',
        text: 'Kilimo SACCO joined on Professional',
        time: '2 min ago',
    },
    {
        type: 'upgrade',
        text: 'Fahari Finance upgraded to Enterprise',
        time: '38 min ago',
    },
    {
        type: 'expired',
        text: 'Mapinduzi Credit trial expired',
        time: '1 hr ago',
    },
    { type: 'payment', text: 'Invoice INV-004821 paid', time: '2 hrs ago' },
    { type: 'suspended', text: 'Ndugu Bank suspended', time: '3 hrs ago' },
];

describe('ActivityFeed', () => {
    it('renders the "Recent Activity" heading', () => {
        render(<ActivityFeed items={items} />);
        expect(screen.getByText('Recent Activity')).toBeInTheDocument();
    });

    it('renders the text for every activity item', () => {
        render(<ActivityFeed items={items} />);
        items.forEach((item) => {
            expect(screen.getByText(item.text)).toBeInTheDocument();
        });
    });

    it('renders the time for every activity item', () => {
        render(<ActivityFeed items={items} />);
        items.forEach((item) => {
            expect(screen.getByText(item.time)).toBeInTheDocument();
        });
    });

    it('renders a "View audit log" link', () => {
        render(<ActivityFeed items={items} />);
        expect(screen.getByText('View audit log')).toBeInTheDocument();
    });

    it('defaults the "View audit log" link to /admin/audit', () => {
        render(<ActivityFeed items={items} />);
        const link = screen.getByText('View audit log').closest('a');
        expect(link).toHaveAttribute('href', '/admin/audit');
    });

    it('uses a custom viewAllHref when provided', () => {
        render(<ActivityFeed items={items} viewAllHref="/custom/logs" />);
        const link = screen.getByText('View audit log').closest('a');
        expect(link).toHaveAttribute('href', '/custom/logs');
    });

    it('renders an empty list without errors', () => {
        render(<ActivityFeed items={[]} />);
        expect(screen.getByText('Recent Activity')).toBeInTheDocument();
        expect(screen.queryByText('min ago')).not.toBeInTheDocument();
    });

    it('wraps everything in a pf-glass container', () => {
        const { container } = render(<ActivityFeed items={items} />);
        expect(container.firstChild).toHaveClass('pf-glass');
    });
});
