import { test, expect } from '@playwright/test';
import {
    BASE_URL,
    ADMIN,
    AUDITOR,
    loginAs,
    registerAndLogin,
    ensureLiveRacingEnabled,
    createArena,
    createEventShowUrl,
    addRace,
    addHorses,
    offerSingleBetTypes,
    publishEvent,
    placeWizardBet,
    adjustWalletAsAuditor,
    readWalletBalance,
} from '../helpers';

// A player put on hold in WFBO still sees Live Racing's normal betting
// wizard; a bet they place leaves the loading spinner running with no label
// and no popup - nothing tells them they are on hold.

test.describe('Live Racing — on-hold player', () => {
    test.setTimeout(180_000);

    test('sees the betting form; placing a bet leaves a silent spinner running and takes nothing', async ({ page }, testInfo) => {
        const player = await registerAndLogin(page);
        await loginAs(page, AUDITOR.mobile, AUDITOR.password);
        await adjustWalletAsAuditor(page, player.mobile, '100', 'On-hold LR e2e');

        await loginAs(page, ADMIN.mobile, ADMIN.password);
        await ensureLiveRacingEnabled(page);
        const arena = await createArena(page, 'Hold', { win: '10' });
        const { eventUrl, eventId } = await createEventShowUrl(page, arena);
        const race = await addRace(page, eventUrl, 1);
        const horses = await addHorses(page, race.raceUrl, 2);
        await offerSingleBetTypes(page, eventUrl, [{ key: 'win', raceId: race.raceId }]);
        await publishEvent(page, eventUrl);

        // Put the player on hold (WFBO ▸ Users ▸ Hold User).
        await page.goto(`${BASE_URL}/wfbo/users?search=${encodeURIComponent(player.mobile)}`);
        const row = page.locator('[data-testid^="user-row-"]').filter({ hasText: player.mobile });
        const userId = (await row.getAttribute('data-testid'))!.replace('user-row-', '');
        await page.locator(`[data-testid="form-hold-${userId}"]`).evaluate((f: HTMLFormElement) => f.submit());
        await expect(page.locator(`[data-testid="badge-on-hold-${userId}"]`)).toBeVisible();

        // The player: no browser alert ever, the wizard is there as usual.
        await loginAs(page, player.mobile, player.password);
        const dialogs: string[] = [];
        page.on('dialog', d => { dialogs.push(d.message()); void d.dismiss(); });
        expect(await readWalletBalance(page)).toBe(100);

        const overlay = page.locator('#loading-overlay');
        const spinner = page.locator('[data-testid="loading-spinner"]');
        const popup = page.locator('[data-testid="lr-bet-error"]');
        await expect(overlay).toBeHidden();
        await expect(spinner).toBeHidden();

        // Fill the wizard and submit; the helper stops as soon as the spinner is up.
        await placeWizardBet(page, {
            eventId,
            raceId: race.raceId,
            cardLabel: 'Win',
            positions: { 1: [horses[0]] },
            amount: '10',
            expectSilence: true,
        });

        // Spinner stays up, with no label at all.
        await expect(overlay).toBeVisible();
        await expect(overlay.locator('.loading-overlay__label')).toHaveText('');
        await expect(spinner).toBeVisible();
        await expect(popup).toBeHidden();

        // Still spinning a while later - no popup, no dialog, nothing about the hold.
        await page.waitForTimeout(6_000);
        await expect(overlay).toBeVisible();
        await expect(overlay.locator('.loading-overlay__label')).toHaveText('');
        await expect(spinner).toBeVisible();
        await expect(popup).toBeHidden();
        await expect(page.locator('body')).not.toContainText(/on hold/i);
        await page.screenshot({ path: testInfo.outputPath('lr-on-hold-silent-spinner.png') });
        expect(dialogs).toEqual([]);

        // Nothing left the wallet: reload to drop the stuck spinner.
        await page.reload();
        expect(await readWalletBalance(page)).toBe(100);
    });
});
