/**
 * Tests for the admin root page — the mobile entity picker.
 * On md+ viewports the page immediately redirects to /admin/users (tested
 * in e2e). Here we verify the mobile-only list of entity links.
 */
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import Page from './+page.svelte';

vi.mock('$app/navigation', () => ({ goto: vi.fn() }));

const fullData = {
	userCount: 4,
	groupCount: 3,
	tagCount: 7,
	inviteCount: 2,
	canManageUsers: true,
	canManageTags: true,
	canManagePermissions: true,
	canRunMaintenance: true
};

afterEach(cleanup);

describe('Admin root page – entity picker', () => {
	it('renders the admin heading', async () => {
		render(Page, { data: fullData });
		await expect.element(page.getByRole('heading')).toBeInTheDocument();
	});

	it('renders users link pointing to /admin/users', async () => {
		render(Page, { data: fullData });
		await expect
			.element(page.getByRole('link', { name: /benutzer/i }))
			.toHaveAttribute('href', '/admin/users');
	});

	it('renders groups link pointing to /admin/groups', async () => {
		render(Page, { data: fullData });
		await expect
			.element(page.getByRole('link', { name: /gruppen/i }))
			.toHaveAttribute('href', '/admin/groups');
	});

	it('renders tags link pointing to /admin/tags', async () => {
		render(Page, { data: fullData });
		await expect
			.element(page.getByRole('link', { name: /schlagworte/i }))
			.toHaveAttribute('href', '/admin/tags');
	});

	it('renders system link pointing to /admin/system', async () => {
		render(Page, { data: fullData });
		await expect
			.element(page.getByRole('link', { name: /system/i }))
			.toHaveAttribute('href', '/admin/system');
	});

	it('hides users link when canManageUsers is false', async () => {
		render(Page, { data: { ...fullData, canManageUsers: false } });
		await expect.element(page.getByRole('link', { name: /benutzer/i })).not.toBeInTheDocument();
	});

	it('hides system link when canRunMaintenance is false', async () => {
		render(Page, { data: { ...fullData, canRunMaintenance: false } });
		await expect.element(page.getByRole('link', { name: /system/i })).not.toBeInTheDocument();
	});

	it('shows user count', async () => {
		render(Page, { data: fullData });
		await expect.element(page.getByText('4')).toBeInTheDocument();
	});
});
