/**
 * L-series: /settings → leave collective (Fase 10.1, CU-H06).
 *
 * Borja (member of the seed collective) abandons it. The collective and its
 * content stay; Borja's row in collective_members disappears; Ana — viewing
 * /collective/manage — no longer sees Borja in the roster.
 *
 * Borja is then a no-collective user and is auto-redirected to /onboarding.
 *
 * Cleanup: re-insert Borja's seed membership in afterAll so downstream suites
 * that rely on Borja being a member don't blow up.
 */
import { test, expect } from '@playwright/test';
import { USERS } from '../fixtures/users.js';
import { loginAs } from '../fixtures/login.js';
import { closePool, sql, COLLECTIVE_ID } from '@colectivo/test-utils';

test.describe.configure({ mode: 'serial' });

test.describe('Leave collective (CU-H06)', () => {
	test.afterAll(async () => {
		// Restore Borja's seed membership so other suites pass.
		await sql(
			`INSERT INTO public.collective_members (collective_id, user_id, role)
			 VALUES ($1, $2, 'member')
			 ON CONFLICT (collective_id, user_id) DO UPDATE SET role = EXCLUDED.role`,
			[COLLECTIVE_ID, USERS.borja.id]
		);
		await closePool();
	});

	test('L-01: Borja leaves the seed collective → row gone, lands on onboarding', async ({
		page
	}) => {
		await loginAs(page, USERS.borja);
		await page.goto('/settings');

		// Settings page renders the collectives section; Borja's seed collective
		// has a leave button next to it.
		const leaveButton = page.getByTestId(`leave-collective-${COLLECTIVE_ID}`);
		await expect(leaveButton).toBeVisible({ timeout: 15_000 });
		await leaveButton.click();

		// Confirm in the modal.
		await page.getByTestId('confirm-leave-collective').click();

		// Borja had only one collective; he gets bounced to /onboarding.
		await expect(page).toHaveURL(/\/onboarding$/, { timeout: 15_000 });

		// DB-side: membership row is gone, but the collective row + content survive.
		const m = await sql(
			'SELECT count(*)::int AS n FROM public.collective_members WHERE collective_id = $1 AND user_id = $2',
			[COLLECTIVE_ID, USERS.borja.id]
		);
		expect(m.rows[0].n).toBe(0);

		const c = await sql('SELECT count(*)::int AS n FROM public.collectives WHERE id = $1', [
			COLLECTIVE_ID
		]);
		expect(c.rows[0].n).toBe(1);
	});
});
