/**
 * SR-series (UI) — Fase 4.1
 *
 * Global search at /search. Debounced input (300ms) feeds
 * search_in_collective() and groups results by type (Lists / Tasks / Notes).
 */
import { test, expect } from '@playwright/test';
import { USERS } from '../fixtures/users.js';
import { loginAs } from '../fixtures/login.js';

test.describe('Global search', () => {
	test.beforeEach(async ({ page }) => {
		await loginAs(page, USERS.borja);
	});

	test('SR-01: searching "milk" surfaces the seeded shopping item', async ({ page }) => {
		await page.goto('/search');
		const input = page.getByPlaceholder(/search|buscar/i);
		await expect(input).toBeVisible({ timeout: 15_000 });
		await input.fill('Milk');

		// Wait beyond the 300ms debounce.
		await page.waitForTimeout(600);

		// The shopping item group exists and contains a row with "Milk".
		const group = page.getByTestId('search-group-shopping_item');
		await expect(group).toBeVisible({ timeout: 5_000 });
		await expect(group.getByText(/milk/i).first()).toBeVisible({ timeout: 5_000 });
	});

	test('SR-02: deactivating the Lists filter hides shopping items from results', async ({ page }) => {
		await page.goto('/search');
		const input = page.getByPlaceholder(/search|buscar/i);
		await expect(input).toBeVisible({ timeout: 15_000 });
		await input.fill('Milk');
		await page.waitForTimeout(600);

		// Seeded "Milk" lives in shopping_items — it must be visible before the filter toggle
		await expect(page.getByTestId('search-group-shopping_item')).toBeVisible({ timeout: 5_000 });

		// Deactivate the "Lists" chip (multi-toggle filter). The shopping_item group disappears.
		await page.getByRole('button', { name: /^lists$|^listas$/i }).click();
		await page.waitForTimeout(400);

		await expect(page.getByTestId('search-group-shopping_item')).toHaveCount(0, {
			timeout: 3_000
		});
	});
});
