import { test, expect } from '@playwright/test';
import { BASE_URL, ADMIN, loginAs } from '../helpers';

// The Games sidebar group only holds Report, Leaderboard and All Games;
// each game is reached from a card on the All Games page.

const GAME_CARDS = ['vk', 'pataasan', 'cg', 'mg', 'fk', 'pk', 'sw', 'pd'];

test.describe('WFBO All Games page', () => {

    test.beforeEach(async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
    });

    test('Games menu has only Report, Leaderboard and All Games', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo`);
        await page.click('[data-target="games-group-body"]');

        const links = page.locator('#games-group-body a');
        await expect(links).toHaveCount(3);
        await expect(page.locator('[data-testid="wfbo-nav-games-report"]')).toBeVisible();
        await expect(page.locator('[data-testid="wfbo-nav-games-leaderboard"]')).toBeVisible();
        await expect(page.locator('[data-testid="wfbo-nav-games-all"]')).toBeVisible();
        await expect(page.locator('[data-testid^="wfbo-nav-game-hub-"]')).toHaveCount(0);
    });

    test('All Games lists every game as a card', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo`);
        await page.click('[data-target="games-group-body"]');
        await page.click('[data-testid="wfbo-nav-games-all"]');

        await expect(page).toHaveURL(/\/wfbo\/games$/);
        await expect(page.locator('[data-testid="wfbo-page-title"]')).toContainText('Games');
        await expect(page.locator('[data-testid="wfbo-nav-games-all"]')).toHaveClass(/is-active/);
        for (const key of GAME_CARDS) {
            await expect(page.locator(`[data-testid="wfbo-games-card-${key}"]`)).toBeVisible();
        }
    });

    test('a game card opens its hub and All Games stays highlighted', async ({ page }) => {
        await page.goto(`${BASE_URL}/wfbo/games`);
        await page.click('[data-testid="wfbo-games-card-pd"]');

        await expect(page).toHaveURL(/\/wfbo\/games\/pusoy$/);
        await expect(page.locator('[data-testid="pd-hub-settings"]')).toBeVisible();
        await expect(page.locator('[data-testid="wfbo-nav-games-all"]')).toHaveClass(/is-active/);
        await expect(page.locator('[data-testid="wfbo-nav-games-report"]')).not.toHaveClass(/is-active/);
    });
});
