import { describe, expect, it } from 'vitest';
import { applyClientFilter } from './clientFilter';
import type { components } from '$lib/generated/api';

type Item = components['schemas']['ActivityFeedItemDTO'];

function makeItem(overrides: Partial<Item> = {}): Item {
	return {
		kind: 'FILE_UPLOADED',
		documentId: 'd1',
		documentTitle: 'Brief A',
		happenedAt: '2026-04-20T10:00:00Z',
		youMentioned: false,
		youParticipated: false,
		count: 1,
		actor: null,
		happenedAtUntil: null,
		...overrides
	};
}

describe('applyClientFilter', () => {
	describe('non-fuer-dich filters pass through unchanged', () => {
		it('alle returns all items', () => {
			const items = [makeItem(), makeItem({ kind: 'COMMENT_ADDED' })];
			expect(applyClientFilter(items, 'alle')).toHaveLength(2);
		});

		it('hochgeladen passes through (server already filtered by kinds)', () => {
			const items = [makeItem({ kind: 'FILE_UPLOADED' }), makeItem({ kind: 'COMMENT_ADDED' })];
			expect(applyClientFilter(items, 'hochgeladen')).toHaveLength(2);
		});

		it('transkription passes through (server already filtered by kinds)', () => {
			const items = [makeItem({ kind: 'TEXT_SAVED' })];
			expect(applyClientFilter(items, 'transkription')).toHaveLength(1);
		});

		it('kommentare passes through (server already filtered by kinds)', () => {
			const items = [makeItem({ kind: 'COMMENT_ADDED' })];
			expect(applyClientFilter(items, 'kommentare')).toHaveLength(1);
		});
	});

	describe('fuer-dich applies youMentioned || youParticipated predicate', () => {
		it('includes items where youMentioned is true', () => {
			const items = [makeItem({ youMentioned: true })];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(1);
		});

		it('includes items where youParticipated is true', () => {
			const items = [makeItem({ youParticipated: true })];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(1);
		});

		it('includes items where both flags are true', () => {
			const items = [makeItem({ youMentioned: true, youParticipated: true })];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(1);
		});

		it('excludes items where neither flag is set', () => {
			const items = [makeItem({ kind: 'COMMENT_ADDED' })];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(0);
		});

		it('MENTION_CREATED without youMentioned flag is excluded', () => {
			// youMentioned is set by the backend only when this event is directed at the current user.
			// Unlike the old feedFilters.ts, we no longer include MENTION_CREATED unconditionally —
			// that incorrectly showed mentions directed at OTHER users.
			const items = [
				makeItem({ kind: 'MENTION_CREATED', youMentioned: false, youParticipated: false })
			];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(0);
		});

		it('MENTION_CREATED with youMentioned true is included', () => {
			const items = [makeItem({ kind: 'MENTION_CREATED', youMentioned: true })];
			expect(applyClientFilter(items, 'fuer-dich')).toHaveLength(1);
		});

		it('filters mixed items to only personally relevant ones', () => {
			const items = [
				makeItem({ kind: 'FILE_UPLOADED' }),
				makeItem({ kind: 'COMMENT_ADDED', youParticipated: true }),
				makeItem({ kind: 'MENTION_CREATED', youMentioned: true }),
				makeItem({ kind: 'TEXT_SAVED' })
			];
			const result = applyClientFilter(items, 'fuer-dich');
			expect(result).toHaveLength(2);
			expect(result[0].kind).toBe('COMMENT_ADDED');
			expect(result[1].kind).toBe('MENTION_CREATED');
		});
	});
});
