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

// Pusoy Dos beta players: while the game is limited to test accounts, admins can
// let chosen real players in from WFBO ▸ Pusoy Dos ▸ Settings ▸ Beta Players.

const SETTINGS = `${BASE_URL}/wfbo/pusoy/settings`;

test.describe.serial('WFBO — Pusoy Dos beta players', () => {
    test.afterAll(async ({ browser }) => {
        const page = await browser.newPage();
        await configurePusoy(page, { enabled: false, testUsersOnly: true, seats: '4' });
        await page.close();
    });

    test('a player added as a beta player can play while the game is test-accounts-only; removing them takes it away', async ({ page }) => {
        await configurePusoy(page, { enabled: true, testUsersOnly: true });
        const player = await registerAndLogin(page);
        await expect(page.locator('[data-testid="play-pusoy-card"]')).toHaveCount(0);

        // Added by mobile number, typed the local way (09...).
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(SETTINGS);
        await expect(page.locator('[data-testid="pd-beta-players"]')).toBeVisible();
        await page.fill('[data-testid="input-pd-beta-mobile"]', player.mobile.replace('+63', '0'));
        await page.click('[data-testid="btn-pd-beta-add"]');
        await expect(page.locator('[data-testid="flash-success"]')).toContainText('can now play Pusoy Dos');
        const row = page.locator('[data-testid="pd-beta-row"]').filter({ hasText: player.mobile });
        await expect(row).toBeVisible();

        await loginAs(page, player.mobile, player.password);
        await expect(page.locator('[data-testid="play-pusoy-card"]')).toBeVisible();
        await page.goto(`${BASE_URL}/play/pusoy`);
        await expect(page.locator('[data-testid="pd-lobby"]')).toBeVisible();

        // Removed: back to no access.
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(SETTINGS);
        page.once('dialog', d => d.accept());
        await row.locator('[data-testid^="btn-pd-beta-remove-"]').click();
        await expect(page.locator('[data-testid="flash-success"]')).toContainText('no longer a beta player');
        await expect(page.locator('[data-testid="pd-beta-row"]').filter({ hasText: player.mobile })).toHaveCount(0);

        await loginAs(page, player.mobile, player.password);
        await expect(page.locator('[data-testid="play-pusoy-card"]')).toHaveCount(0);
        await page.goto(`${BASE_URL}/play/pusoy`);
        await expect(page).toHaveURL(`${BASE_URL}/`);
    });

    test('only existing player accounts can be added', async ({ page }) => {
        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await page.goto(SETTINGS);

        await page.fill('[data-testid="input-pd-beta-mobile"]', '09999999999');
        await page.click('[data-testid="btn-pd-beta-add"]');
        await expect(page.locator('[data-testid="flash-error"]')).toContainText('No player with mobile number 09999999999');

        await page.fill('[data-testid="input-pd-beta-mobile"]', AGENT.mobile);
        await page.click('[data-testid="btn-pd-beta-add"]');
        await expect(page.locator('[data-testid="flash-error"]')).toContainText('Only player accounts can be beta players');
        await expect(page.locator('[data-testid="pd-beta-row"]').filter({ hasText: AGENT.mobile })).toHaveCount(0);
    });
});
